'Loading Excel row with data using ListObject
I am attempting to load rows in an Excel worksheet, "List of Golfers", from worksheet "Players". On the Players worksheet I have a table named "GolfTrip". On the table I have columns named LastName, FirstName and Yes_No.
My code sets the objPlayers and cycles through each row in GolfTrip determining who is a "Yes".
I want to load the variables LName and FName with the current row. Once loaded I would like to put the values of LName and FName to column A starting on row A5 in "List of Golfers" worksheet.
Sub getPlayersCommitted()
Dim objPlayers As ListObject
Dim YN As Range
Dim LName As String
Dim FName As String
Dim cnt As Integer
cnt = 0
Set objPlayers = Sheets("Players").ListObjects("GolfTrip")
For Each YN In objPlayers.ListColumns("Yes_No").DataBodyRange
Select Case YN.Value
Case "Yes"
cnt = cnt + 1
'Below I was attempting to load LastName and FirstName fields from the current row equalling Yes from the GolfTrip table.
'LName = objPlayers.ListColumns("LastName").DataBodyRange
'FName = objPlayers.ListColumns("FirstName").DataBodyRange
'Then want to add LName, FName to column A on worksheet List of Golfers starting on row A5 within worksheet List of Golfers.
End Select
Next
MsgBox "Count of Players: " & cnt
cnt = 0
End Sub
Solution 1:[1]
Here's how you can read the row values:
Sub Tester()
Dim rw As ListRow, lo As ListObject
Dim LName As String
Dim FName As String
Set lo = ActiveSheet.ListObjects("GolfTrip")
For Each rw In lo.ListRows
If rw.Range.Cells(lo.ListColumns("Yes_No").Index).Value = "Yes" Then
LName = rw.Range.Cells(lo.ListColumns("LastName").Index).Value
FName = rw.Range.Cells(lo.ListColumns("FirstName").Index).Value
Debug.Print LName, FName
End If
Next rw
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tim Williams |