How to update VBA cell references when deleting columns?

stubarny

New Member
Joined
Apr 7, 2011
Messages
41
Hello,

I've noticed that when I delete columns from my worksheet the cell references in my VBA code don't update, so the macros stop working.

Please could someone tell me if there is a way of updating these references when I delete columns, or tell me if there is a workaround?

Thanks,

Stu
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You can but depends on how you code your macro, can you post it please?
 
Upvote 0
Perhaps you should be using defined names for your ranges and then refer to the names in your code.

For instance, say Column X in Sheet1 is 'Amount'. You could set-up a name 'Amount', referring to Sheet1!$X:$X, in name manager. Then in your code you refer to the name:

Code:
Sheets("Sheet1").Range("Amount")

This way if you delete any column between A:W, Amount will move relatively to column W and your macro will continue to work.
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,802
Members
452,943
Latest member
Newbie4296

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