loop through rows with blanks.

tourless

Board Regular
Joined
Feb 8, 2007
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi Folks.

Working through another simple problem... I need to know how to turn this...
In J3 I have =IF(I3="","",F3-I3)
...into a macro that will either skip the blank rows or just insert the formula as is until it reached my last row of data. I already know my last row via lRow = .Cells(.Rows.Count, "A").End(xlUp).Row

Basically, starting at I3, test if there is a value in I, if so add the formula to J3, if not, move on to the next row until the end of the data is reached.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
It's not pretty but it works :)
VBA Code:
ActiveSheet.Range("$A$2:$J$" & lRow).AutoFilter Field:=9, Criteria1:="<>"
    
    Range("J3:J" & lRow).Formula2 = "=If(I3="""","""",F3-I3)"
    
    With ActiveSheet
        .AutoFilterMode = False
    End With
 
Upvote 0
An example approach that leaves no formulas on the sheet and a macro that doesn't require filters or loops:
VBA Code:
Sub Test()
'
    Dim lRow    As Long
'
    With Sheets("Sheet1")
        lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'
        With .Range("J3:J" & lRow)
            .Value = Sheets("Sheet1").Evaluate("If(" & .Offset(0, -1).Address & "="""",""""," & .Offset(0, -4).Address & "-" & .Offset(0, -1).Address & ")")
        End With
    End With
End Sub
 
Upvote 0
That's a good one JohnnyL, I appreciate the input.
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

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