Table Reference (Round 2)

bryanworkman

New Member
Joined
Oct 29, 2015
Messages
6
I am very novice to VBA and table references are new to me. I have a macro that needs to go loop through every row of a table and determine the value of each cell in a specific column. That column is subject to change depending on other factors in the macro. I have a variable which captures the name of the column header that needs to be searched (colHeader) but I get an error everytime I try to use that variable in the code below. When I replace the concatenation with just the name of the column ([Tbl_BIS[Column 2]].Column) it works just fine so I'm assuming I'm concatenating wrong or this just can't work this way. Is there a simple mistake here, or is there another way to do this?

Dim rowNum as Long
Dim colHeader as String
Dim cellValue as string

rowNum = 1
colHeader = "Column 2"

cellValue = [Tbl_BIS].Cells(rowNum, "[Tbl_BIS[" & orgLevel & "]].Column")
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Another approach

Code:
Sub test2()
  Dim rowNum As Long, colHeader As String, cellValue As String
  [COLOR=#0000ff]rowNum [/COLOR]= 1
  [COLOR=#ff0000]colHeader [/COLOR]= "Column 2"
  cellValue = ActiveSheet.ListObjects("Tbl_BIS").ListColumns([COLOR=#ff0000]colHeader[/COLOR]).DataBodyRange([COLOR=#0000ff]rowNum[/COLOR])
End Sub
 
Upvote 0
Another approach

Code:
Sub test2()
  Dim rowNum As Long, colHeader As String, cellValue As String
  [COLOR=#0000ff]rowNum [/COLOR]= 1
  [COLOR=#ff0000]colHeader [/COLOR]= "Column 2"
  cellValue = ActiveSheet.ListObjects("Tbl_BIS").ListColumns([COLOR=#ff0000]colHeader[/COLOR]).DataBodyRange([COLOR=#0000ff]rowNum[/COLOR])
End Sub


This worked perfectly! I tried a version of this format and was getting nowhere. Thanks!
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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