Macro to Fill Down to last row of data

whitey1987

New Member
Joined
Jun 11, 2011
Messages
7
Hi everyone,

Hoping someone can help me with this. I've created a Macro to input an IF formula into my spreadsheet. The number of rows in the spreadsheet changes on a regular basis. So I would like to adapt my macro so that the formula fills to the last row of data each time it is run.

Can anyone please adapt the code I have below?


Sub Macro1()
'
' Macro1 Macro
' Inserts If formula.
'

'
Columns("D:D").Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("D2").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC[-2]=""FGH"",RC[1]=""123""),RC[-2]&RC[1],IF(AND(RC[-2]=""FGH"",RC[1]<>""123""),RC[-2],IF(AND(RC[1]=""123"",OR(RC[-2]=""IJK"",RC[-2]=""ABC"",RC[-2]=""CDE"")),RC[-2]&RC[1],RC[-2]&RC[-1])))"
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D880")
Range("D2:D880").Select
Range("A1").Select
End Sub

Thank you in advance for any help you can give.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Maybe this

Code:
Sub Macro1()
'
' Macro1 Macro
' Inserts If formula.
'

'
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Columns("D").Insert
Range("D2:D" & LR).FormulaR1C1 = _
"=IF(AND(RC[-2]=""FGH"",RC[1]=""123""),RC[-2]&RC[1],IF(AND(RC[-2]=""FGH"",RC[1]<>""123""),RC[-2],IF(AND(RC[1]=""123"",OR(RC[-2]=""IJK"",RC[-2]=""ABC"",RC[-2]=""CDE"")),RC[-2]&RC[1],RC[-2]&RC[-1])))"
End Sub
 
Upvote 0
Hi,

Thanks for the reply. That works brilliantly. Could you talk me through how this code works? It will help me progress with my macro making :).

Thanks again, much appreciated.
 
Upvote 0
Basically LR gets the last filled row on the sheet then the formula is entered in D2:D & LR
 
Upvote 0
Thanks VoG. This will come in handy on some other macros.

Another quick question whist I'm here....

Can you force a macro to only run within a particular worksheet? If so, how would you adapt the code above to achieve this...

Am I right in thinking it would be something like;

If isheet.Name = "SheetExample" Then ..... code here?
 
Upvote 0
Basically LR gets the last filled row on the sheet then the formula is entered in D2:D & LR


Hi..Thank you for your code. However, my "D1" field gets displaced. Could you please guide me on this??

My coding:

Sub CHK()
'
' CHK Macro
'


'
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Columns("D").Insert
Range("D2:D" & LR).FormulaR1C1 = _
"=(RC[-2]/RC[-1])*100"

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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