Excel macro formula fill down for block of data

banjoflanjo

New Member
Joined
Mar 20, 2008
Messages
44
Hi,

I'm using a macro to insert formulae into a spreadsheet and want to be able to fill them down to the bottom of the block of data.

The user inserts 2 columns to the left of the block of data, selects the cell of the first row in the first inserted column and then I'm using the following code to insert the formulae:

Code:
ActiveCell.FormulaR1C1 = _ 
"=IF(LEFT(RC[2],2)="" "",R[-1]C,TRIM(LEFT(RC[2],12)))" 
ActiveCell.Offset(0, 1).Select 
ActiveCell.FormulaR1C1 = _ 
"=IF(LEFT(RC[1],2)="" "",TRIM(LEFT(RC[1],12)),"""")"


<!-- BEGIN TEMPLATE: bbcode_code --><!-- END TEMPLATE: bbcode_code -->I now need to be able to fill these 2 formulae down for the full block of data which is to the right.

I would have usually used something like:

Code:
Dim rng As Range 
Set rng = ActiveSheet.Range("B3") 
If Range("B1048576").End(xlUp).Row > 3 Then 
    rng.AutoFill Range(rng, rng.Offset(0, -1).End(xlDown).Offset(0, 1))

in both columns B and A (the two inserted columns), but I dont want to fix the range to B3 & A3, I want to perform the action based on the active cell that the user starts the macro on as it may not always start on row 3.

Can anyone help?

Thanks

Banjoflanjo
 

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
Managed to sort this using:

Code:
Dim n As Long
     
    n = Cells(Rows.Count, ActiveCell.Offset(, 2).Column).End(xlUp).Row - ActiveCell.Row + 1
     
    With ActiveCell
        .Resize(n).FormulaR1C1 = "=IF(LEFT(RC[2],2)=""  "",R[-1]C,TRIM(LEFT(RC[2],12)))"
        .Offset(0, 1).Resize(n).FormulaR1C1 = "=IF(LEFT(RC[1],2)=""  "",TRIM(LEFT(RC[1],12)),"""")"
    End With

Thanks

Banjoflanjo
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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