Forum Discussion

moemaya's avatar
moemaya
Copper Contributor
Feb 11, 2019
Solved

Repeating Macro until I get an empty row

Hi totally new to macros, but i figured out how to record one and it works.

Now I need to make it repeat for several rows until it gets to an empty row.

see below

Sub Macro5()
Range("P9").GoalSeek Goal:=0.25001, ChangingCell:=Range("G9")
End Sub

 

I would like this to repeat for each row, for example: row 10,11,12...until it gets to an empty cell.

can someone help? Thanks.

  • Hi,

     

    You can use the For Each statement to loop through each cell in the selection and apply the code to it.

    But you have to select the range of cells that you want to change before you run the code.

    Sub Macro5()

    For Each cell In Selection
    cell.GoalSeek Goal:=0.25001, ChangingCell:=cell.Offset(0, -9)
    Next

    End Sub

     

    Hope that help

  • Haytham Amairah's avatar
    Haytham Amairah
    Silver Contributor

    Hi,

     

    You can use the For Each statement to loop through each cell in the selection and apply the code to it.

    But you have to select the range of cells that you want to change before you run the code.

    Sub Macro5()

    For Each cell In Selection
    cell.GoalSeek Goal:=0.25001, ChangingCell:=cell.Offset(0, -9)
    Next

    End Sub

     

    Hope that help

    • moemaya's avatar
      moemaya
      Copper Contributor

      It worked!! Thank you Haytham!

Resources