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
 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,232
Office Version
  1. 365
Platform
  1. Windows
Simon

I agree with HOTPEPPER.

Why can't you use SUM?
 

ackey

Board Regular
Joined
Sep 24, 2004
Messages
75
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
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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?
 

Watch MrExcel Video

Forum statistics

Threads
1,123,396
Messages
5,601,420
Members
414,449
Latest member
Pashtun

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
Top