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: 8

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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: 3
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,214,875
Messages
6,122,046
Members
449,063
Latest member
ak94

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