Search code examples
excelvisual-studio-macrosvba

for each loop in excel macros vba jumping alternate rows


I have the below code in vba. The rows in sheet5 from columns a5 to a20 are:

a5=Sweden

a6=Spain

a7=Russia

a8=Italy

a9=Germany

a10=Finland

a11=Norway

a12=Switzerland

a13=France

a14=Belgium

Set fillcolumnrange = Sheet5.Range("A5:A20")
i = 1
For Each Row In fillcolumnrange.Rows
 If Not Sheet5.Range("A" & i + 4) = "" Then
    MsgBox Row(i)
 End If
 i = i + 1
Next Row

But this code is prompting only alternate values ie.

Sweden

Russia

Germany

Norway

France

Can anyone please help me out find the bug in the code


Solution

  • You were looping through the rows in your range and also advancing the variable i within your loop. You can reference each variable that you are looping through.

    Try this

    Set fillcolumnrange = Sheet1.Range("A5:A20")
    For Each cell In fillcolumnrange.Cells
     If Not cell = "" Then
        MsgBox cell
     End If
    Next cell