Changing from cell reference to using column name reference in table

wbstadeli

Board Regular
Joined
Mar 11, 2016
Messages
153
Office Version
  1. 365
Platform
  1. Windows
Hi!

Im working on a worksheet where i plan on making a lot of userforms that reference into a table. I want to make this more "dynamic" so that my code goes by the column name instead of a hard column number incase i need to add more columns in the future. This is the code i have so far, it adds a new row to the bottom of the table and fills in some information. Can anyone help to change this to use my column names to determine where this info gets input? Here is the pertinent info, im okay if there is a better way to do this without using the code i started, if you have better ideas!.. :)

Sheet name: Order List
Table name: Table1
Column name for referencing: Product:
Column name for referencing: Vendor:
Column name for referencing: Manufacturer:

Current code:
Code:
Sub Fill_In_Info()


Dim the_sheet As Worksheet
Dim the_table As ListObject
Dim table_object_row As ListRow
Dim last_row As Long
Set the_sheet = ActiveSheet
Set the_table = the_sheet.ListObjects("Table1")
Set table_object_row = the_table.ListRows.Add
table_object_row.Range.Cells(1, 4).Value = "Ballmill" [COLOR=#ff0000][B](i want this to use reference column name "Product:" instead of "4") [/B][/COLOR]
table_object_row.Range.Cells(1, 5).Value = Vendor.Text [COLOR=#ff0000][B](i want this to use reference column name "Vendor:" instead of "5") [/B][/COLOR]
table_object_row.Range.Cells(1, 7).Value = Manufacturer.Text [COLOR=#ff0000][B](i want this to use reference column name "Manufacturer:" instead of "7") [/B][/COLOR]


End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
How about
Code:
With the_table
   .ListColumns("Product").DataBodyRange.Cells(.DataBodyRange.Rows.Count, 1).Value = "Ballmill"
   .ListColumns("Vendor").DataBodyRange.Cells(.DataBodyRange.Rows.Count, 1).Value = "vendor"
End With
 
Upvote 0

Forum statistics

Threads
1,214,402
Messages
6,119,301
Members
448,885
Latest member
LokiSonic

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