flexible range

Tody03

New Member
Joined
Jun 21, 2014
Messages
48
I have a table with one column A with several lines. The number of lines in column A varies from time to time.
I wrote a macro (see below) that will write a formula in B1 and drag it down from B1 until the last used cell in column A.
In the attached macro the range is limited until B6. How can i make the range (B1:B6) flexible so that the last cell in B will match to the last cell in column A.

Range("B1").Select
ActiveCell.FormulaR1C1 = "=RC[1]+2"
Range("B1").Select
Selection.AutoFill Destination:=Range("B1:B6")
Range("B1:B6").Select
End Sub

thanks
tody
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
You can replace all that code with
Code:
   Range("B1", Range("A" & Rows.Count).End(xlUp).Offset(, 1)).FormulaR1C1 = "=rc[1]+2"
 
Upvote 0
Try this:
Code:
Dim lr as Long

'Find last row in column A
lr = Cells(Rows.Count,"A").End(xlUp).Row
'Apply formula to column B
Range("B1:B" & lr).[COLOR=#333333]FormulaR1C1 = "=RC[1]+2"[/COLOR]
 
Upvote 0
Already answered.
 
Last edited:
Upvote 0
Thanks for your quick response. It is working.
I have another question how to write a formula instead rc[1]+2 i want to fill the column with "=sum(b2:z2)"
Thanks
 
Upvote 0
Hello Joe4
Thanks for your quick response. Although I do not understand the syntax - It is working.
I have another question how to write a formula instead rc[1]+2 i want to fill the column with "=sum(b2:z2)"
Thanks
 
Upvote 0
Assuming you want the formula to increment then...
Code:
Range("B1:B" & lr).Formula = "=Sum(B2:Z2)"
If you don't want it to increment then
Code:
Range("B1:B" & lr).Formula = "=Sum($B$2:$Z$2)"

Expect a circular reference error in the 2nd row of the non-incrementing formula.
 
Last edited:
Upvote 0
Hello mark858. i feel stupid to ask simple questions but you helped me. thanks tody
 
Last edited:
Upvote 0
Also note, that in order to get formulas in the "R1C1" format, all you have to do is to turn on the Macro Recorder and record yourself entering the formula into ones of the cells.
Then, stop the recorder and view the code it recorded. It will give you the formula you need in R1C1 format.
 
Upvote 0
Also note, that in order to get formulas in the "R1C1" format, all you have to do is to turn on the Macro Recorder and record yourself entering the formula into ones of the cells.
Then, stop the recorder and view the code it recorded. It will give you the formula you need in R1C1 format.


Thanks i will follow your instructions
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,793
Members
451,589
Latest member
Harold14

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