Using table headers rather than column number …

adambc

Active Member
Joined
Jan 13, 2020
Messages
412
Office Version
  1. 365
Platform
  1. Windows
I thought that I had posted on this subject a while ago but I can’t find the post - either way, I never found a solution but desperately need to now!

I have a UserForm that captures data via a number of different controls and when cmdSave is selected, writes the data using eg …

Cells(NEXTROW, 1).value = TextBox1 (NB I have already calculated NEXTROW earlier in the routine)
Cells(NEXTROW, 2).value = TextBox2
Cells(NEXTROW, 3).value = ComboBox1
Etc

The problem I now have is that following a business reorganisation (!), I have been asked to add a number of new fields/corresponding columns, but the new columns need to be inserted in the underlying table rather than added to the end of the table - this, of course!, throws all column references out, requiring me to go through and manually change them (and make sure they’re all working as intended without impacting the existing data!) …

And I already know similar requests will follow for a number of other Workbooks …

What I would like to do is reference columns by the table header which is on row 2 in the Worksheet (row 1 has a number of buttons/macros) eg …

Cells(NEXTROW, Header1).value = TextBox1

… which allows me to move the Header1 column without needing to edit the save statement …

Anyone got any ideas?

Thanks …
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I thought that I had posted on this subject a while ago but I can’t find the post - either way, I never found a solution but desperately need to now!

I have a UserForm that captures data via a number of different controls and when cmdSave is selected, writes the data using eg …

Cells(NEXTROW, 1).value = TextBox1 (NB I have already calculated NEXTROW earlier in the routine)
Cells(NEXTROW, 2).value = TextBox2
Cells(NEXTROW, 3).value = ComboBox1
Etc

The problem I now have is that following a business reorganisation (!), I have been asked to add a number of new fields/corresponding columns, but the new columns need to be inserted in the underlying table rather than added to the end of the table - this, of course!, throws all column references out, requiring me to go through and manually change them (and make sure they’re all working as intended without impacting the existing data!) …

And I already know similar requests will follow for a number of other Workbooks …

What I would like to do is reference columns by the table header which is on row 2 in the Worksheet (row 1 has a number of buttons/macros) eg …

Cells(NEXTROW, Header1).value = TextBox1

… which allows me to move the Header1 column without needing to edit the save statement …

Anyone got any ideas?

Thanks …
I have solved my own post!!!

For ...

Cells(NEXTROW, 1).value = TextBox1

Read ...

Cells(NEXTROW, Rows(2).Find("Header1", , xlValues, xlWhole).Column).value = TextBox1

Works a treat!!!

Any coding improvements gratefully received ...
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,777
Members
452,942
Latest member
VijayNewtoExcel

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