Variable Table Range Definition in VBA

londonparisrome

New Member
Joined
Aug 1, 2019
Messages
25
I am self-taught in VBA, so my apologies if this is an easy question. I am trying to reset data in the first column of a table to "Y". The current table location is rows 16-246, but I would like the user to be able to expand that, as needed, so I need the row numbers to be variable. I was able to create the variable range on the spreadsheet in cell X2, so it contains "(B16:B246)", and it will grow or shrink based on the rows in the table. Is there a way to reference cell X2 in my code for the range, or am I going about it the wrong way?

Private Sub Reset_Click()
' Replace column B with Y
ActiveSheet.Range("B16:B246").Value = "Y"
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Are you dealing with an actual table?

If you are there are various ways you can refer to a column in the table.
VBA Code:
Sheets("Sheet1").ListObjects("Table1").ListColumns("Field1").DataBodyRange.Value = "Y"
 
Upvote 0
Are you dealing with an actual table?

If you are there are various ways you can refer to a column in the table.
VBA Code:
Sheets("Sheet1").ListObjects("Table1").ListColumns("Field1").DataBodyRange.Value = "Y"
Yes. It's a table

I must be doing something wrong in the syntax. I keep getting an error. I have tried the following options. I am in Sheet 10, and the table name is combined:

ActiveSheet.ListObjects("Combined").ListColumns(2).DataBodyRange.Value = "Y"

ActiveSheet.ListObjects("Combined").ListColumns("Field2").DataBodyRange.Value = "Y"

Sheets("Sheet10").ListObjects("Combined").ListColumns("Field2").DataBodyRange.Value = "Y"
 
Upvote 0
What's the error, where are you putting the code and what's the column name?
 
Upvote 0
Thanks for your help. I received the following error:

1605742638014.png

1605742664793.png
 
Upvote 0
That usually means that either there is no table named 'Combined' on the active sheet, or in that table there is no column named 'Field2'.

In the code I posted I used generic/default names for the table/column, you would need to change those to reflect the actual table/column names you are working with.
 
Upvote 0
That usually means that either there is no table named 'Combined' on the active sheet, or in that table there is no column named 'Field2'.

In the code I posted I used generic/default names for the table/column, you would need to change those to reflect the actual table/column names you are working with.
Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,568
Messages
6,120,278
Members
448,953
Latest member
Dutchie_1

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