VB formulae that adds variable number of cells

ackey

Board Regular
Joined
Sep 24, 2004
Messages
75
Afternoon all

I have a problem with inserting a formula using VBA, which is part of a bigger project

If I was to do this manually, the spreadsheet would look like the following

A1 B1 C1 D1 E1
=b1+c1+d1+e1 Data1 Data2 Data3 Data4 etc

The issue I have is that the number of columns of data is variable. I cannot use SUM as later on in the code I will insert extra columns. I have no problem writing code to calculate the number of columns that would need to be added (and assign that to a variable), however, I am unable to work out how to write the simple excel formula within VB that only adds up the actual number of columns that have data

Thanks in advance

Simon
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I understand you are inserting columns, but why can't you use SUM.

If your are summing B1:E1 and then insert 2 columns after column B, wouldn't you then want it to sum B1:G1?

Please clarify.
 
Upvote 0
Simon

I agree with HOTPEPPER.

Why can't you use SUM?
 
Upvote 0
I cannot use SUM as the extra columns I am inserting are inbetween B1 and E1 in this example, and I do not want to include them in the calculation
 
Upvote 0
Do you actually need to insert a formula in the cell or could your VBA code write the result to the cell before it inserts the columns?
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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