Referencing column headings

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
Currently I have a form which posts data to a worksheet like

Code:
txtFirstName.Value = Sheets("Sheet1").Range("B" & cboUniqueID.ListIndex +3)

or

Code:
txtFirstName.Value = Sheets("Sheet1").Range("A" & cboUniqueID + 3).Offset(, 1)

but what I want to do is use the column headings in row 1 of the worksheet to reference the column, this is because other columns may be added to the worksheet in the future.

all column headings are related to their textbox/combobox like ???Columnheading e.g. the column heading "FirstName" would have a related textbox with the name "txtFirstName".

Any ideas on how to use this to reference the column?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
First thing that occurs to me is to give each column heading a 'name'. Look into Excel's named ranges as it will allow you to refer to those names in your formulas.
 
Upvote 0
Would it not be possible to derive the column name from the name of the controls

something like

Code:
Dim ColumnHeading As String
Set ColumnHeading = Right(UserForm1.Controls.Name, (Len(UserForm1.Controls.Name) - 3)

except that .Controls.Name isn't the way to reference the name of a control (I don't know how to do this)

and then use a Find function to look for the ColumnHeading in Row1 of the worksheet
 
Upvote 0
Sorry, my bad, missed that you were working with a form.

The following should get you started.

Code:
Dim ctl As Control
    For Each ctl In Controls
        MsgBox ctl.Name
    Next ctl
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

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