Selecting specific cells in a table using vba

ttratl

Board Regular
Joined
Dec 21, 2004
Messages
168
Selecting specific cells in a table row using vba, where user has manually selected the row.

User will select a particular row in a table, and I need to take the data from 8 cells in that row, and put them on a different sheet.

I need to use structured formatting (which I believe is using the column labels) within the vba code,
as the Table may have additional columns added in the future, and I don't want to re-adjust the cell references each time.

I have cobbled together this code from various sources, which actually works, but I think it is possibly overkill?
Is there simpler code that I should be using?
Code:
Sub Populate_JI_Sheet()

 Dim csd As Range 'Course Start Date
 Dim v As Range 'Venue
 Dim st As Range 'Start Time
 Dim d As Range 'Duration
 Dim a As Range 'Activity
 Dim e As Range 'Email
 Dim fn As Range 'First name
 Dim ln As Range 'Last name
 
    
    Set csd = Evaluate("Table1[[#This Row], [Course Start Date]]")
        Sheets("JI_Sheet").Range("C8").Value = csd
    
    Set v = Evaluate("Table1[[#This Row], [Venue]]")
        Sheets("JI_Sheet").Range("C9").Value = v
        
    Set st = Evaluate("Table1[[#This Row], [Start Time]]")
        Sheets("JI_Sheet").Range("C10").Value = st
    
    Set d = Evaluate("Table1[[#This Row], [Duration]]")
        Sheets("JI_Sheet").Range("C11").Value = d
        
    Set a = Evaluate("Table1[[#This Row], [Activity]]")
        Sheets("JI_Sheet").Range("C7").Value = a
        
    Set e = Evaluate("Table1[[#This Row], [Contact Details]]")
        Sheets("JI_Sheet").Range("J3").Value = e
        
    Set fn = Evaluate("Table1[[#This Row], [First Name]]")
    Set ln = Evaluate("Table1[[#This Row], [Last Name]]")
    
    Sheets("JI_Sheet").Range("J4").Value = fn & " " & ln

End Sub

Any pointers would be appreciated...
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Ok - so through trial and error I've found
Code:
Sheets("JI_Sheet").Range("C8").Value = Evaluate("Table1[[#This Row], [Course Start Date]]")
seems to get around the need for DIMs. Makes it simpler.
 
Last edited:
Upvote 0
Perhaps you will be bether of with the Cells(RowIndex, ColumnIndex) property

Code:
Dim rngRow As Integer, rngColumn as integer

For rngRow = 1 To 6
    for rngColumn = 1 to 6
        Cells(rngRow, rngColumn).Value = 100
    next rngColumn
Next rngRow
 
Upvote 0
Perhaps you will be bether of with the Cells(RowIndex, ColumnIndex) property

Code:
Dim rngRow As Integer, rngColumn as integer

For rngRow = 1 To 6
    for rngColumn = 1 to 6
        Cells(rngRow, rngColumn).Value = 100
    next rngColumn
Next rngRow

Thanks Strooman. I can't quite understand how your code would help me (I'm not great on VBA).
I have to use the table column labels - but not to worry - I've got it to work for me now. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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