Double Loop
The short answer was already given in the comments by IAmNerd2000 and Eleshar: You are missing the Exit For
line. Below is an optimized, over commented version of your code which you might find instructive.
Tips
- It is customary not to use underscores "_" in macro titles, but
to use capital letters:
HinnanKorotusVälilehti
, HinKorVäl
or
HKV
instead of Hinnan_korotus_välilehti. And shorter titles are
preferred (Makro
is really not necessary in the title).
- Always use
Option Explicit
for the compiler to find variables that
are not declared (nimike
).
- Start variables with lower case letters and introduce uppercase
letters to divide words e.g.
lukuRivi
or tulostusRivi
.
- Long vs
Integer
- Remove unused variables (
lukurivi_tausta
).
- Do not add unnecessary variables e.g. NOT
A = B: C = A
but C = B
(uusihinta
).
- Do not use
Select
or Activate
if not necessary, because they slow
down your code.
- Add variable descriptions or titles for you (after a while) and others
to better understand the code.
- Use object variables (
ws
) to make use of Intellisense.
- Use the
With
statement to divide codes of different objects (not quite a demonstration here).
- When data (rows) will be added the hard coded last row (
400
) will
always have to be changed. Therefore it is better to calculate it
every time using the End
property or the Find
method.
The Code
Option Explicit
Sub HinnanKorotusVälilehti()
Dim ws As Worksheet ' Worksheet Variable for worksheet "Taul1"
Dim lukuRivi As Long ' Worksheet "Nimikkeitä" Row Counter
Dim tulostusRivi As Long ' Worksheet "Taul1" Row Counter
Dim nimike As String ' Product Title
' Create a reference to worksheet "Taul1" (Everything starting with
' "ws." is referring to workhsheet "Taul1").
Set ws = ThisWorkbook.Worksheets("Taul1")
' In Worksheet "Nimikkeitä" (Everything starting with a "." is referring
' to worksheet "Nimikkeitä".)
With ThisWorkbook.Worksheets("Nimikkeitä")
' Loop through rows of worksheet "Nimikkeitä".
For lukuRivi = 75 To 400
' Check if the cell at the intersection of the current row
' and column "F" contains "Hawle".
If .Range("F" & lukuRivi) = "Hawle" Then ' IS "Hawle"
' Assign the value of the cell at the intersection of the
' current row and column "A" to variable "nimike".
nimike = .Range("A" & lukuRivi)
' (In Worksheet Taul1) Loop through rows of Worksheet Taul1.
For tulostusRivi = 2 To 400
' Check if the cell at the intersection of the current row
' and column "A" contains (the value of) "nimike".
If ws.Range("A" & tulostusRivi) = nimike Then ' IS "nimike".
' Write the value of the cell at the intersection of
' the current row (tulostusRivi) and the "F" column
' in worksheet "Taul1" to the cell at the intersection
' of the current row (lukuRivi) and the "E" column in
' worksheet "Nimikkeitä".
.Range("E" & lukuRivi) = ws.Range("F" & tulostusRivi)
' Since the value has been found, stop searching and
' go to next row in worksheet "Nimikkeitä".
Exit For
'Else ' NOT "nimike".
End If
Next ' Next row in worksheet "Taul1" (tulostusRivi).
'Else ' NOT "Hawle".
End If
Next ' Next row in worksheet "Nimikkeitä" (lukuRivi).
End With
End Sub