Question : Excel VBA Sum Contiguous Cells

Hello, I'm looking for a way to sum a range of contiguous cells from two separate worksheets labeled "Commercial", and "Consumer".  I need only the bottommost rows from each worksheet to be included into one sum and to use the values from colum D from each worksheet.  I then want the total to be appended to cell H2 on the Consumer worksheet.

Answer : Excel VBA Sum Contiguous Cells

I made this udf..which does what you want...enclosed is your workbook..after the formula...all you need to do is..

=add(your range to check,your 2nd range to check)

Saurabh...

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
Function add(rng As Range, rng1 As Range)
    Dim cell As Range, i As Long, y As Long, r As Long, r1 As Long, u As Long, ws As Worksheet
 
    Set ws = Sheets(rng1.Parent.Name)
 
    r = rng.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    r1 = rng1.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    u = 1
 
    Do Until u > r
        If Cells(u, rng.Column).Value = "" Then
            i = 0
 
        Else
            If IsNumeric(Cells(u, rng.Column).Value) = True Then
                i = Application.Evaluate("Sum(" & i & "," & Cells(u, rng.Column).Value & ")")
 
            End If
        End If
        u = u + 1
    Loop
 
    u = 1
    Do Until u > r1
        If ws.Cells(u, rng1.Column).Value = "" Then
            y = 0
 
        Else
            If IsNumeric(ws.Cells(u, rng1.Column).Value) = True Then
                y = Application.Evaluate("Sum(" & y & "," & ws.Cells(u, rng1.Column).Value & ")")
 
            End If
 
        End If
        u = u + 1
    Loop
 
 
    add = i + y
 
 
End Function
Open in New Window Select All
Random Solutions  
 
programming4us programming4us