How to redo this single Macro comand over every single row (e.g. from row 2 to row 500)

62 Views Asked by At

I'm new programming over VBA; for the moment I have created a single macro for a single row-column; in this particular case for Row "2" and columns "M2:BF2" but I need this MACRO- command to run over the next 320 rows and I do not know how to do it. this is my command:

enter image description here

I will appreciate any advice or guidance,

Best for everybody,

Jorge

2

There are 2 best solutions below

1
The King On

I suggest you do this using Excel IF(condition, then, else) Formula.. Then once you complete one row, you can copy paste the same formula to all the rows and viola you have the answers.

Incase you insist on doing this using VBA, then you have to use a for loop.

  For i = 2 to 320
     If Range("N" & i).value = 0 then Range("N" & i).value = Range("M" & i).value
     '.... and so on...
  Next i
0
John Coleman On

Nested for loops are the way to go:

Sub test()
    Dim i As Long, j As Long
    For i = 2 To 320
        For j = 14 To 58 Step 2
            If Cells(i, j).Value = 0 Then Cells(i, j).Value = Cells(i, j - 1).Value
        Next j
    Next i
End Sub

See this for information about loops in VBA.