Correct language for referencing table

duteberta

Board Regular
Joined
Jun 14, 2009
Messages
83
Office Version
  1. 365
Platform
  1. MacOS
I'm creating a data entry form that will automatically add new table records when clicking an "Add" button.

In writing the code, instead of referring to

myRow.Range("3") = Range("$C$3")

I need to reference the target table column with the correct syntax. So not "3" but instead something like "MASTER[Status]". It's not working for me.

What language would I use to map correctly? Once I get the correct language I will map all the rest of my fields.

VBA Code:
Sub RoundedRectangle1_Click()

    Dim myRow As ListRow
    Dim intRows As Integer
    
    intRows = ActiveWorkbook.Worksheets("MASTER").ListObjects("MASTER").ListRows.Count
    Set myRow = ActiveWorkbook.Worksheets("MASTER").ListObjects("MASTER").ListRows.Add(intRows)
    
        myRow.Range("3") = Range("$C$3")
        


End Sub

Screenshot 2024-03-24 at 11.29.45 AM.png
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
How about
VBA Code:
myRow.Range("MASTER[Status]") = Range("$C$3")
 
Upvote 0
I get an error with

myRow.Range("MASTER[Status]") = Range("$C$3")
 
Upvote 0
This is probably what you want:
VBA Code:
Dim c As Range
Set c = ActiveWorkbook.Worksheets("MASTER").ListObjects("MASTER").ListColumns(Range("C3")).Range

If that doesn't work, could you answer these questions :
1. What do you select in C3? the header?

I need to reference the target table column with the correct syntax. So not "3" but instead something like "MASTER[Status]".
2. Could you explain this using an example?
 
Upvote 0
This reference works for me
?sheets("Sheet1").listobjects("Table3").range(8,1)
Walmart

EDIT (sort of): I thought I hit "Post Reply" but it seems I did not, so this is a bit old now.
 
Upvote 0
Maybe
VBA Code:
    Dim myRow As ListRow
    With ActiveWorkbook.Worksheets("MASTER").ListObjects("MASTER")
        Set myRow = .ListRows.Add
        myRow.Range(.ListColumns("status").Index) = Range("$C$3")
    End With

I'm using the list column index because you want to use the name of the column, but you could use the index directly. Let's say status is column 2, then you would do this:
VBA Code:
    Dim myRow As ListRow
    With ActiveWorkbook.Worksheets("MASTER").ListObjects("MASTER")
        Set myRow = .ListRows.Add
        myRow.Range(2) = Range("$C$3")
    End With

Although nothing stops you from using a less verbose approach like:
VBA Code:
    With Sheets("MASTER").ListObjects("MASTER").ListRows.Add
        .Range(2).Value = Range("$C$3")
    End With

Or even this, which would work because you're in a module:
VBA Code:
    With Range("MASTER").ListObject.ListRows.Add
        .Range(2) = Range("$C$3")
    End With

As well as others.........
 
Upvote 0
Once I get the correct language I will map all the rest of my fields.
Based on this a variation of what Edgar had might work better for you.
PS: You are adding a row before the last row is that really the intention ?
Typically you would just add a row to the bottom of the table ie just .ListRows.Add

VBA Code:
Sub RoundedRectangle1_Click()

    Dim myRow As ListRow
    Dim intRows As Integer
    Dim lo As ListObject
    
    Set lo = Range("Master").ListObject
    
    With lo
        intRows = .ListRows.Count
        Set myRow = .ListRows.Add(intRows)
    
        ' Map columns
        .ListColumns("Status").DataBodyRange(myRow.Index) = Range("$C$3")
    End With
        
End Sub
 
Upvote 0
My problem is correctly identifying the TABLE COLUMN where the data will be inserted. Those table columns may move positions if I change that column order in the future - therefore I want table column references (not numbers that may fluctuate). I will try the above solutions to see
 
Upvote 0
For context I have a very wide table that data is being inserted into (247 columns wide). This table is named "MASTER". Because it is so wide I want to created an DATA INPUT FORM to make it easier to enter records.

Here is a pic of my DATA INPUT FORM that is a protected sheet. All cells are locked except where data is entered.

Screenshot 2024-03-25 at 7.08.00 AM.png


Here is the table called "MASTER" where data is input when clicking "ADD".

Screenshot 2024-03-25 at 7.11.48 AM.png


Once I figure out the correct VBA syntax to add records into the table using table structured references- then I can add the rest of the fields.

Then the next task will be programming the "ADD" button to"

1. Add the data to "MASTER"
2. Clear the data in the INPUT FORM
3. Hide the INPUT sheet
4. Show the MASTER sheet
 
Upvote 0
Based on this a variation of what Edgar had might work better for you.
PS: You are adding a row before the last row is that really the intention ?
Typically you would just add a row to the bottom of the table ie just .ListRows.Add

VBA Code:
Sub RoundedRectangle1_Click()

    Dim myRow As ListRow
    Dim intRows As Integer
    Dim lo As ListObject
   
    Set lo = Range("Master").ListObject
   
    With lo
        intRows = .ListRows.Count
        Set myRow = .ListRows.Add(intRows)
   
        ' Map columns
        .ListColumns("Status").DataBodyRange(myRow.Index) = Range("$C$3")
    End With
       
End Sub
Yes you are correct- I mistakenly was adding the 2nd to last row. I will change- thanks
 
Upvote 0

Forum statistics

Threads
1,215,098
Messages
6,123,082
Members
449,094
Latest member
mystic19

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