Continue macro after encountering a blank row

gomes123

New Member
Joined
Jun 16, 2021
Messages
35
Office Version
  1. 2007
Platform
  1. Windows
In Column O, starting from Cell O3, the value is =H3/$D$3. I then drag and drop the formula downwards. So Cell O4's value is =H4/$D$3 and so on etc..

But whenever I encounter a blank row, for example if Row 6 is a blank row, I would like the formula in Column O to "reset". So for Cell O7's value, I would like it to be =H7/$D$7, (instead of H7/$D$3) and then drag and drop the values until there is another blank row, then "resetting the value" in Column O again.

The problem with the excel macro below, is that it doesnt lock the Cell D values. So the output would be =H3/$D$3, =H4/$D$4, =H5/$D$5 etc.

How would I fix the macro so the output would be =H3/$D$3, =H4/$D$3, =H5/$D$3 etc. ?

Thanks.

VBA Code:
Sub ResetFormulaOnBlankRow()

    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim denominator As Double
    
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change the sheet name as needed
    lastRow = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row
    
    For i = 3 To lastRow
        If ws.Cells(i, 1).Value = "" Then
            denominator = ws.Cells(i, 4).Value
        Else
            ws.Cells(i, 15).Formula = "=" & "H" & i & "/" & "$D$" & i
        End If
    Next i

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Perhaps something like this.
VBA Code:
Sub ResetFormulaOnBlankRow()

    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim denominator As Double
    Dim FRow As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")            ' Change the sheet name as needed
    lastRow = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row

    FRow = 3
    For i = 3 To lastRow
        If ws.Cells(i, 1).Value = "" Then
            denominator = ws.Cells(i, 4).Value
            FRow = i + 1
        Else
            ws.Cells(i, 15).Formula = "=" & "H" & i & "/" & "$D$" & FRow
            ws.Cells(i, 16).Value = "'=" & "H" & i & "/" & "$D$" & FRow
        End If
    Next i
End Sub
 
Upvote 1
Solution
Thanks
Perhaps something like this.
VBA Code:
Sub ResetFormulaOnBlankRow()

    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim denominator As Double
    Dim FRow As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")            ' Change the sheet name as needed
    lastRow = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row

    FRow = 3
    For i = 3 To lastRow
        If ws.Cells(i, 1).Value = "" Then
            denominator = ws.Cells(i, 4).Value
            FRow = i + 1
        Else
            ws.Cells(i, 15).Formula = "=" & "H" & i & "/" & "$D$" & FRow
            ws.Cells(i, 16).Value = "'=" & "H" & i & "/" & "$D$" & FRow
        End If
    Next i
End Sub
Thanks, most appreciated! It works perfectly, in addition to the extra column (P) that you provided!

I tried modifying the code above to do some extra tasks, and there seems to be a problem.
So in Column O, the cell O3 starts as =H3/$D$3, and dragging down it works perfectly.
I tried modifying the code to do the following:
Column P, cell P4 value to be =H4/$D$4, and drag it down (until there is a blank row)
Column Q, cell Q5 value to be =H5/$D$5, and drag it down (until there is a blank row)
And subsequently the same in Column R (and subsequent columns), Cell R6 = H6/$D$6, until there is a blank row.

So I modified the code
VBA Code:
 ws.Cells(i + 1, 16).Formula = "=" & "H" & i + 1 & "/" & "$D+1$" & FRow
But it doesn't seem to work (seems to be a problem with $D+1$), and it continues down even when there is a blank row.

Most appreciated for any help. Thanks!
 
Upvote 0
Actually the line of code for col P was just something I put in for myself to write the formulas as text so I could more easily see if the col O formula was being created correctly. I just forgot to take it out when I posted it :). I don't understand what you are trying to accomplish with your modification. Pehaps you can provide a visual example of the result you want, using XL2BB as I have done here.

Book1
ABCDEFGHIJKLMNOPQR
2Data Col 1Data Col 2Data Col 3Data Col 4Data Col 5Data Col 6Data Col 7Data Col 8Data Col 9Data Col 10Data Col 11Data Col 12Data Col 13Data Col 14Data Col 15Data Col 16Data Col 17Data Col 18
33426627014773275976538819285404992131676721.84696017657645551
46641349239436152272004915182667824559944651.0293501896976630
59128593584216874863715928974869667039215561.24109015540415353
6445343534912802513613280576619890907648882132720109
76711617644143861232035181127139367174132621.25120773379826859
8522555370183993473484239702291187890946917243332905
9288403411920647403884913298607788685186188573673160
103319257974107664968538808938192981284727862.14634146991487913
114352066857722239779645951222206017657196161.45121951295663914
Sheet1
Cell Formulas
RangeFormula
O3:O5O3=H3/$D$3
O7O7=H7/$D$7
O10:O11O10=H10/$D$10
 
Upvote 1
Actually the line of code for col P was just something I put in for myself to write the formulas as text so I could more easily see if the col O formula was being created correctly. I just forgot to take it out when I posted it :). I don't understand what you are trying to accomplish with your modification. Pehaps you can provide a visual example of the result you want, using XL2BB as I have done here.

I see, thanks anyways! Yeah, I initially thought you put it out there so it'd be easier for me to visualise and see what is going on with the coding.

Thanks, this is basically what I'm trying to do. The macro you posted earlier works fine for Column O, now I'm just trying to get it to work for Column P, Q, R, S, etc.. and beyond.
Column P, cell P4 value starts at =H4/$D$4, and drag it down (until there is a blank row)
Column Q, cell Q5 value starts at =H5/$D$5, and drag it down (until there is a blank row)

If there is a blank row (see attached image), the macro "resets" and starts again from O, this time cell O8 would be =H8/$D$8, and drag it down.
Cell P9 = H9/$D$9 and drag down etc.

Most appreciated, thanks!
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    213.9 KB · Views: 9
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,167
Members
452,615
Latest member
bogeys2birdies

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