For next loop is not working

Melkor

New Member
Joined
Oct 27, 2016
Messages
7
Hi guys,

I'm trying to use for next loop, on button click it should check the value from the cell R2, and according to the value of the cell to show the next value from the list in column O in label. Bellow you can find the loop which I used, but it's showing the value from the list in column O. Also when the loop reach the last value from the list it should start from the first value from the list again.

Can you please help me with this?

VBA Code:
Private Sub nextButton_Click()
Dim brojPolja As Long

Worksheets("Sheet1").Cells(2, 18).Value = imeLabel.Caption
brojPolja = Sheet1.Cells(Rows.Count, 15).End(xlUp).Row
For i = 2 To brojPolja
   imeLabel.Caption = Worksheets("Sheet1").Cells(i, "O").Value
Next

End Sub
 

Attachments

  • Capture.PNG
    Capture.PNG
    18.9 KB · Views: 17

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I think I have understood what you want so try this:
VBA Code:
Private Sub nextButton_Click()
Dim brojPolja As Long

Worksheets("Sheet1").Cells(2, 18).Value = imeLabel.Caption
brojPolja = Sheet1.Cells(Rows.Count, 15).End(xlUp).Row
For i = 2 To brojPolja
 If Worksheets("Sheet1").Cells(i, "O").Value = imeLabel.Caption Then   ' check if the label is the same as O row
   If i < brojPolja Then   ' check if at the end of list
   imeLabel.Caption = Worksheets("Sheet1").Cells(i + 1, "O").Value
   Else
   imeLabel.Caption = Worksheets("Sheet1").Cells(2, "O").Value   ' start again at beggining
   End If
 End If
Next

End Sub
 
Upvote 0
Hi, thank you for the answer, I have tried that but it's showing just the first result from the column, and it's stuck there :/
 

Attachments

  • Capture.JPG
    Capture.JPG
    69.8 KB · Views: 14
Upvote 0
Sorry my mistake I left one line out!!
try this:
VBA Code:
Private Sub nextButton_Click()
Dim brojPolja As Long

Worksheets("Sheet1").Cells(2, 18).Value = imeLabel.Caption
brojPolja = Sheet1.Cells(Rows.Count, 15).End(xlUp).Row
For i = 2 To brojPolja
 If Worksheets("Sheet1").Cells(i, "O").Value = imeLabel.Caption Then   ' check if the label is the same as O row
   If i < brojPolja Then   ' check if at the end of list
   imeLabel.Caption = Worksheets("Sheet1").Cells(i + 1, "O").Value
   Else
   imeLabel.Caption = Worksheets("Sheet1").Cells(2, "O").Value   ' start again at beggining
   End If
   Exit For   ' add this line
 End If
Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,812
Messages
6,181,099
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top