Macro or Formula to count row for which a sum is taken

harry2908

Board Regular
Joined
May 7, 2010
Messages
87
I am using a macro (mentioned below) which totals figures in a sheet according to days. I need a macro or a formula in a specific cell of another sheet which tells the number of rows for which that sum is taken.

Code:
Dim LR As Long
    Dim Area As Range
    With ActiveSheet
        LR = .Range("J" & .Rows.Count).End(xlUp).Row
        If LR = 2 Then
            .Range("J4").Formula = "=SUM(J2)"
        Else
            For Each Area In .Range("J2:J" & LR).SpecialCells(xlCellTypeConstants).Areas
                With Area.Resize(1).Offset(Area.Rows.Count + 1)
                    .Formula = "=SUM(" & Area.Address & ")"



Can someone please help...!!!

Thanks in Advance..!!!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Well, if LR = 2 then you already know the answer is 1.

That leaves your areas to consider. You can amend your code like so:



Else
Dim xRowsArea as Long
xRowsArea = 0

For Each Area In .Range("J2:J" & LR).SpecialCells(xlCellTypeConstants).Areas
With Area.Resize(1).Offset(Area.Rows.Count + 1)
.Formula = "=SUM(" & Area.Address & ")"
xRowsArea = xRowsArea + Area.rows.count


At the end of your loop, the xRowsArea variable will tell you how many rows. I hedge a bit on this because I don't undertsand why you are resizing your areas by increasing their row count by 1, so maybe you need
xRowsArea = xRowsArea + Area.rows.count + 1

but you get the idea.
 
Upvote 0
Hi Tom,

Thanks for your response..

I have tried doing what you mentioned above. The point is i m not so familier with VBA so i am facing trouble. I have mentioned a part of the macro above, please find the full macro below.

it would be really helpful if you can make changes in the code below & repost it...

Thanks..

Code:
Sub d_CellColor_and_Total_in_all_sheets()
Dim i As Long, j As Long
Dim k As Integer, l As Integer
Dim LASTROW As Long
Dim ws As Object
For Each ws In Worksheets
ws.Select
LASTROW = Cells(Rows.Count, 1).End(xlUp).Row
Range("B1:B" & LASTROW).Select
Selection.Sort Key1:=Range("B1"), Order1:=xlDescending, HEADER:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
For i = LASTROW To 1 Step -1
If Range("B" & i).Value >= 0 And Range("B" & i).Value <= 9 Then Range(("A" & i), Range("P" & i)).Interior.ColorIndex = 36
If Range("B" & i).Value >= 10 And Range("B" & i).Value <= 14 Then Range(("A" & i), Range("P" & i)).Interior.ColorIndex = 4
If Range("B" & i).Value >= 15 And Range("B" & i).Value < 20 Then Range(("A" & i), Range("P" & i)).Interior.ColorIndex = 44
If Range("B" & i).Value >= 20 And Range("B" & i).Value < 100 Then Range(("A" & i), Range("P" & i)).Interior.ColorIndex = 3
Range("B1:B" & LASTROW).Select
Selection.Sort Key1:=Range("B1"), Order1:=xlDescending, HEADER:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Next i
k = 3
For j = LASTROW To 1 Step -1
If Range("B" & j).Interior.Color <> Range("B" & j + 1).Interior.Color Then
Range("B" & j + 1).Select
For l = 1 To k
    Selection.EntireRow.Insert
    Selection.EntireRow.Clear
    
Next l
End If
Next j
    Range("2:4").Select
    Selection.Delete Shift:=xlToup
    
    Dim LR As Long
    Dim Area As Range
    With ActiveSheet
        LR = .Range("J" & .Rows.Count).End(xlUp).Row
        If LR = 2 Then
            .Range("J4").Formula = "=SUM(J2)"
        Else
            For Each Area In .Range("J2:J" & LR).SpecialCells(xlCellTypeConstants).Areas
                With Area.Resize(1).Offset(Area.Rows.Count + 1)
                    .Formula = "=SUM(" & Area.Address & ")"
                    
                End With
            Next Area
        End If
    End With
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,510
Members
452,918
Latest member
Davion615

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