syntax to assign table cell values to variables... losing my mind!

jbaich

Board Regular
Joined
Nov 2, 2011
Messages
140
Hi all, I have been reading the internet all day trying to figure out how to properly reference tables and i have tried everything i could find but so far am at a loss...

I've recently begun using Power Query a lot, so I figured I'd better learn how to refer to tables in VBA... it seems like it should be super simple and intuitive, and maybe for others it is... but I am at my wits end trying to understand how to correctly loop through a table and assign the value of the cell in the active row of the desired column. God I hope someone out there can put this into words that will make sense... I swear i've tried every combination of every post that comes up in the first 5 google search pages for every search combination of VBA, Table, Variable, Excel, Syntax, Loop, Reference, etc....

myTable
[TABLE="width: 500"]
<tbody>[TR]
[TD]Primary ID (PrID variable)[/TD]
[TD]Name[/TD]
[/TR]
[TR]
[TD]1564654[/TD]
[TD]Jim[/TD]
[/TR]
[TR]
[TD]7578445[/TD]
[TD]Jane[/TD]
[/TR]
[TR]
[TD]1983587[/TD]
[TD]Elvis[/TD]
[/TR]
</tbody>[/TABLE]

Code:
Sub Test()


Dim myTable As ListObject
Dim Master As Workbook
Dim Listsht As Worksheets


Set Master = ThisWorkbook
Set Listsht = Master.Sheets("Summary List")
Set myTable = Listsht.ListObjects("Summary_Listing")


  For Each rw In myTable.ListRows  '//Loop through each row\\


    PrID = ???????????????? I have tried everything here and keep getting object missmatch or some other  error, wrong number of arguments, invalid property assignment... the list goes on...

shouldn't it be something simple like[INDENT]PrID = myTable([@[Primary ID]])  or 
PrID = myTable("[Primary ID](rw)")

[/INDENT]
Next rw


End Sub

So first loop the variable PrID would be 1564654, 2nd iteration would be 7578445, 3rd would be 1983587 and so on...

Eventually I'd like to be able to use the variable for things like
Code:
ApplicationFunction.Match(PrID, Master.sheets("sheet1").Range("A:A"), 0)
etc...

Please, please, please put me out of my misery!

Thanks in advance
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
What you probably want to do is loop through the rows in the Primary ID column.

In the following code that's what's happening in the first loop, the second loop is through the ListRows and, perhaps, shows how it's not as intuitive as looping through the column.
Code:
Sub Test()
Dim Master As Workbook
Dim Listsht As Worksheet
Dim prID As Range
Dim myTable As ListObject
Dim rw As ListRow

    Set Master = ThisWorkbook
    Set Listsht = Master.Sheets("Summary List")
    Set myTable = Listsht.ListObjects("Summary_Listing")

    For Each prID In myTable.ListColumns("Primary ID").DataBodyRange
        MsgBox prID.Value
    Next prID


    For Each rw In myTable.ListRows
        MsgBox rw.Range(1, 1).Value
    Next rw
    
End Sub
 
Upvote 0
Another variation, which avoids hardcoding column indexes:

Code:
Sub Test()

Dim myTable As ListObject
Dim Master As Workbook
Dim Listsht As Worksheet
Dim r As Long, IDix As Long

Set Master = ThisWorkbook
Set Listsht = Master.Sheets("Summary List")
Set myTable = Listsht.ListObjects("Summary_Listing")


  IDix = myTable.ListColumns("Primary ID").Index

  For r = 1 To myTable.ListRows.Count                   '//Loop through each row\\
    prid = myTable.DataBodyRange(r, IDix)
    MsgBox prid
  Next r

End Sub
 
Upvote 0
Thanks so much! so just to clarify... in most instances, in order to utilize column names in your code, you either need to use the rather long .ListColumns("Column Name").DataBodyRange syntax, or define your columns as variables? nothing short and sweet?

Many thanks!
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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