VBA columns delete causing #REF error

cajsoft

New Member
Joined
Feb 22, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi Experts,
I have a spreadsheet will dynamic columns all with numerical data.. at the very end column there's a Total column that SUM's all the figures.

I wrote a VBA function to delete the columns before the Total column and then Copy and insert figures before the Total column from another Sheet. So weekly this VBA function is ran to update the live figures.

However, when the columns are deleted the total column displays a #REF error. Is there anyway around this progmatically?

Capture.PNG
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Welcome to the Board!

If you delete the column that is being referenced in your formula, that will cause those #REF errors.
However, if you are using VBA already to delete columns, why not add to your existing VBA code to populate the SUM formulas you need AFTER the column deletion?

If you need help with that, please post your current VBA code.
Please see this link on how to post VBA code: How to Post Your VBA Code
 
Upvote 0
Thanks for your help.. the issue is that the columns are dynamic so I would need to insert a new total column when finished inserting the live data, how would I handle formatting (ie headers, spacing etc) as there is more data below (not shown in image above) that the total column is used for.
 
Upvote 0
You are welcome.
Out of curiosity, which method did you go with?
 
Upvote 0
strangely enough, I found that if I just insert a blank column before the data it seemed to work... strange.
 
Upvote 0
strangely enough, I found that if I just insert a blank column before the data it seemed to work... strange.
That makes sense. The reason is because you only will get that error when you specifically delete the column the formula is referencing.
For example, if your formula was:
Excel Formula:
=SUM(A2:M2)
you would only get that error if you deleted columns specifically mentioned in your formula (the "endpoints"), i.e. "A" or "M".
If you delete any other column, it will not cause that reference error.

So if you include a blank column just before your SUM formula, and use that in your formula and never delete it, your formula won't ever run into that situation.
So you have successfully created a litte "hack" to get around the issue!
 
Upvote 0
yes.. seems to do the trick... while I'm on the same subject.. is it possible to sort horizontally across the columns..? from the image above we need the columns sorted (left to right) by the first date value (ie 08/03/2023 - 23/03/2023) p.s. I know its sorted in the image above but there is extra columns to the left not captured on the image where dates are jumble up.
 
Upvote 0
yes.. seems to do the trick... while I'm on the same subject.. is it possible to sort horizontally across the columns..? from the image above we need the columns sorted (left to right) by the first date value (ie 08/03/2023 - 23/03/2023) p.s. I know its sorted in the image above but there is extra columns to the left not captured on the image where dates are jumble up.
That is really a brand new and totally different question, and as such, should be posted in a new thread.
However, see if this helps: How to Sort Horizontally in Excel [Columns Sorting]
If it does not solve your issue, please post the question to a new thread.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

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