Excel Macro to sum rows based on the range prescribed using hyphen and comma's

Lekha mohanty

New Member
Joined
Oct 26, 2021
Messages
12
Office Version
  1. 2010
Platform
  1. Windows
  2. Web
I have a excel sheet, where in B column I have range of information, In D column I want the sum of that range of that sheet, In A column I have the sheet name. and in each sheet I have information in C column. So for the first row the formula is =SUM(A!C1:C69)+SUM(A!C75:C76)+SUM(A!C80:C80), is it possible by any macro ?? Please help.


Book1ed.xlsx
ABCD
1A1-69,75-76,80720
2B70-74,77-11039
3C111-174,177-189,194-198,208-22196
4D175-176,190-193,199-207,222-228,23223
5E229-231,233-25324
6F1-18,21,25-3934
7G57-8529
8H19-20,22-24,40-56,86-127,141-208132
9I128-14013
10J1-8181
11K111,116-16955
12L82-110,112-11533
13M1-31,45-48,52-56,62-67,114-122,124,129-150,152,158,162-163,177-181,183-191,198-206,212-228122
14N32-33,49-51,57-61,123,126-128,151,153-157,175-176,192-197,207-21133
15O34-4411
16P78,89-113,159-161,164-169,182,230-232,246-25549
17Q68-77,79-88,125,170-174,229,233-24540
18R1-13,52,54-62,86-99,123-125,127-130,135-136,142-147,21853
19S77-85,100-12232
20T63-76,126,131-134,137-14124
21U14-50,158-164,166-167,169,174-75,177-194,207,213-217,219-225,233-24189
22V51,53,148-157,165,168,170-173,176,195-206,208-212,226-23243
Data
Cell Formulas
RangeFormula
D1D1=SUM(A!C1:C69)+SUM(A!C75:C76)+SUM(A!C80:C80)
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Something like this will work:

VBA Code:
Sub subCreateColDformulas()
Dim rng As Range
Set rng = ActiveSheet.UsedRange
Dim nLoop As Long
Dim sSheet As String
Dim sBstring As String
For nLoop = 1 To Split(rng.Address, "$")(4)
    sSheet = Cells(nLoop, 1)
    sBstring = "=sum(" & sSheet & "!C" & Cells(nLoop, 2) & ")"
    sBstring = Replace(sBstring, "-", ":C")
    sBstring = Replace(sBstring, ",", "," & sSheet & "!C")
    Range("D" & nLoop).Formula = sBstring
Next nLoop
End Sub

I tried it on a few sample sheets A, B and C with 1s, 2s and 3s in cols C of each and it did the job:
Book2
ABCD
1A1-69,75-76,807272
2B70-74,77-1103978
3C111-174,177-189,194-198,208-22196288
Sheet1
Cell Formulas
RangeFormula
D1D1=SUM(A!C1:C69,A!C75:C76,A!C80)
D2D2=SUM(B!C70:C74,B!C77:C110)
D3D3=SUM('C'!C111:C174,'C'!C177:C189,'C'!C194:C198,'C'!C208:C221)
 
Upvote 0
Solution

Forum statistics

Threads
1,223,952
Messages
6,175,593
Members
452,654
Latest member
mememe101

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