Autofill Multiple Columns - VBA

Bobilly

New Member
Joined
Mar 29, 2019
Messages
16
[MENTION]Hi newbie to VBA here,

I am trying to create a macro that takes a formula created in Cell G3 drags it to M3, and then auto fills to the bottom of the last row. The way I was working on it was previously recording and making some adjustments, I cannot find a way to have it auto fill to the last row of the data. Data set rows change frequently, this is what I have so far. (Note you can ignore the G2 portion - that is for a separate autofill part of the Macro)

Range("G2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:M2"), Type:=xlFillDefault
Range("G2:M2").Select
Range("G3").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-6]="""",R[-1]C,RC[-6])"
Range("G3").Select
Selection.AutoFill Destination:=Range("G3:M3"), Type:=xlFillDefault
Range("G3:M3").Select
Selection.AutoFill Destination:=Range("G3:M67")
Range("G3:M67").Select
Range("A1:V67").Select
Range("G3").Activate

Thank you for any help![/MENTION]
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi,

You could test following

Code:
Sub CopyRng()
  Range("G3").FormulaR1C1 = "=IF(RC[-6]="""",R[-1]C,RC[-6])"
  Range("G3").Copy Destination:=Range("G3:M3")
  Range("G3:M3").Copy Destination:=Range("G4:G67")
End Sub

Hope this will help
 
Upvote 0
Hi James,

Thanks for following up, the test did pull through but my problem is that I would like autofill destination to copy to the last row of the data. (ex: this set of data goes to row 2870 not just 67) and it varies from data pull. Thanks for any input!
 
Upvote 0
How about
Code:
Sub Bobbilly()
   Range("G2:M2").FormulaR1C1 = "=RC[-6]"
   Range("G3", Range("A" & Rows.Count).End(xlUp).Offset(, 12)).FormulaR1C1 = "=IF(RC[-6]="""",R[-1]C,RC[-6])"
End Sub
This uses column A to determine the last row, but than can be changed if needed.
 
Upvote 0
Fluff, thanks for providing that, it helps clean things up. What would be the portion of VBA text needed to autofill the contents in (G3:M3) Down to the last row?

Thank you both for your help!
 
Upvote 0
It already does that based on col A
 
Upvote 0
Fluff,

When I run that I get the functions filling from the added 6 rows G to L, but it is throwing in a function in the already set data in column M as well as adding functions to S:X.

It isn't copying and dropping down the data to the last row in columns G:L (the new 6 rows created.)

Current State in VBA.

Sub DataCleanUp()
'Unmerge and unwrap text & create 6 columns to the right of F
ActiveSheet.Cells.UnMerge
ActiveSheet.Cells.WrapText = False
Columns("G:L").Select
Selection.insert Shift:=xlToRight

Range("G3").FormulaR1C1 = "=IF(RC[-6]="""",R[-1]C,RC[-6])"
Range("G2:L2").FormulaR1C1 = "=RC[-6]"
Range("G3", Range("A" & Rows.Count).End(xlUp).Offset(, 12)).FormulaR1C1 = "=IF(RC[-6]="""",R[-1]C,RC[-6])"
Range("G3").Copy Destination:=Range("G3:L3")
Range("G3:L3").Copy Destination:=Range("G" & Rows.Count).End(xlUp).Offset(, 12)

End Sub
 
Upvote 0
It needs to be
Code:
Sub DataCleanUp()
'Unmerge and unwrap text & create 6 columns to the right of F
ActiveSheet.Cells.UnMerge
ActiveSheet.Cells.WrapText = False
Columns("G:L").Select
Selection.insert Shift:=xlToRight
    

    Range("G2:L2").FormulaR1C1 = "=RC[-6]"
    Range("G3", Range("A" & Rows.Count).End(xlUp).Offset(, 11)).FormulaR1C1 = "=IF(RC[-6]="""",R[-1]C,RC[-6])"

    
End Sub
 
Upvote 0
Perfect, thank you so much. I really appreciate your help, learned a lot from it. Hope you have a great day :)
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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