VBA - Target Column by Name (not number)

dellmech

New Member
Joined
Apr 22, 2018
Messages
14
Right now, I have VBA code that only applies to the entire column B. So my VBA code says Target.Column = 2. I re-named column B "safe". I want my VBA code to always work even if columns are inserted between A and B. Otherwise, the code as it is will always just read column B and not follow the data when new columns get inserted. Any ideas?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Assuming your header is in row 1 try
Code:
If Cells(1, Target.Column) = "Safe" Then
 
Upvote 0
I'm not sure how you are using Target right now, but in general, if you wanted to refer to a "named" column as a range, it would look like this:

Code:
Sheets("sheetname").Range("safe")
 
Last edited:
Upvote 0
Please start a thread of your own giving full details of the error & include you code.
thanks
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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