PDA

View Full Version : Nested For Loop With Step Funtion Not Working



cokingtins1
06-05-2017, 09:36 AM
When I run the code, the loop does not iterate to the outer 2 loops ("jrownumber" and "jrow"). Rather, it keeps iterating through "jrowprice" endlessly. I don't know if the "Step" keyword has anything to do with it but could someone please help?


Sub PriceMatch()

Dim LastRow As Long
Dim sht As Worksheet
Dim jrow As Integer
Dim jrowprice As Integer
Dim jrowNumber As Integer
Dim DirectingFileName As String
Dim a As Integer
Dim b As Integer
Dim c As Integer


Set sht = ThisWorkbook.Worksheets("Pivot Tables")
DirectingFileName = ActiveWorkbook.Name
LastRow = sht.Cells(sht.Rows.Count, "C").End(xlUp).Row

For jrow = 2 To 5000
For jrowNumber = 3 To LastRow Step 2
For jrowprice = 2 To LastRow Step 2

If Workbooks(DirectingFileName).Sheets("Stock Net").Cells(jrow, 5).Value = Workbooks(DirectingFileName).Sheets("Pivot Tables").Cells(jrowNumber, 3).Value Then
Workbooks(DirectingFileName).Sheets("Stock Net").Cells(jrow, 8).Value = Workbooks(DirectingFileName).Sheets("Pivot Tables").Cells(jrowprice, 3).Value

Else

Workbooks(DirectingFileName).Sheets("Stock Net").Cells(jrow, 8).Value = ""


End If


Next jrowprice
Next jrowNumber
Next jrow

End Sub

Bob Phillips
06-05-2017, 11:18 AM
It looks fine. It loops through the inner loop (although Cells(jrow, 8) could be cleared over and over again) with jrowNumber =3, then it does it again for jrowNumber = 5, and so on.

cokingtins1
06-05-2017, 11:33 AM
When I add jrowPrice, jrowNumber, and jrow into a Watch Window, the values of jrowNumber and jrow do not change (the do not increment because they are not being iterated by the loop. Any ideas?

cokingtins1
06-05-2017, 11:55 AM
Could you take a look at the spreadsheet? I have removed any extraneous information to make it as clear as possible. Thanks!

19394

Paul_Hossler
06-05-2017, 12:13 PM
When I add jrowPrice, jrowNumber, and jrow into a Watch Window, the values of jrowNumber and jrow do not change (the do not increment because they are not being iterated by the loop. Any ideas?

They seem to change, but with 2 to 5000 it'll take awhile


19395


What is it you're trying to accomplish?

cokingtins1
06-05-2017, 12:22 PM
The "Output" column on the "Stock Net" sheet should populate a price that is matched by its respective part number in column 5 of the "Stock Net" sheet and the part number in the pivot table in the "Pivot Table" sheet.

Paul_Hossler
06-05-2017, 12:56 PM
If you redesign the format of the PT, you can use a VLOOKUP and won't need any VBA

cokingtins1
06-05-2017, 01:07 PM
I knew you could have done that but I just gave up on it. Thank you so much sir! I really appreciate the help!