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
 
how i change your code to be aplicable to columns.
i have the following table.

[TABLE="width: 570"]
<colgroup><col><col span="6"><col span="3"></colgroup><tbody>[TR]
[TD][/TD]
[TD]a[/TD]
[TD]b[/TD]
[TD]c[/TD]
[TD]d[/TD]
[TD]e[/TD]
[TD]f[/TD]
[TD]g[/TD]
[TD]h[/TD]
[TD]i[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD="align: left"]a[/TD]
[TD="align: right"]$126[/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: right"]$118[/TD]
[TD="align: right"]$142[/TD]
[TD="align: right"]$141[/TD]
[TD="align: right"]$114[/TD]
[TD="align: right"]$94[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD="align: left"]b[/TD]
[TD="align: right"]$226[/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: right"]$177[/TD]
[TD="align: right"]$179[/TD]
[TD="align: right"]$218[/TD]
[TD="align: right"]$165[/TD]
[TD="align: right"]$165[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD="align: left"]c[/TD]
[TD="align: right"]$248[/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: right"]$260[/TD]
[TD="align: right"]$240[/TD]
[TD="align: right"]$260[/TD]
[TD="align: right"]$196[/TD]
[TD="align: right"]$196[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD="align: left"]d[/TD]
[TD="align: right"]$184[/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: right"]$182[/TD]
[TD="align: right"]$182[/TD]
[TD="align: right"]$243[/TD]
[TD="align: right"]$206[/TD]
[TD="align: right"]$206
[/TD]
[/TR]
</tbody>[/TABLE]

I do not know how many lines will be in the table, i do not know how many columns will be in the table.
I want to write a formula at the end of column b. i want to copy the same formula, on the same line, to column c and all the way to the right till the end of the table.
I tried as you suggested to record a macro but i get fix line number and columns.
Thanks
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
There is a line of code in my post up in reply 2 that shows you how to find the last row with data, looking at a particular column.

To find the last column with data, when looking at a particular row, you can do it like this:
Code:
'Find last column with data in row [COLOR=#ff0000]1[/COLOR]
lCol = Cells([COLOR=#ff0000]1[/COLOR], Columns.Count).End(xlToLeft).Column
 
Upvote 0

Forum statistics

Threads
1,224,836
Messages
6,181,251
Members
453,027
Latest member
Lost_in_spreadsheets

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