Add Auto sum in selected blank cells VBA

Sufiyan97

Well-known Member
Joined
Apr 12, 2019
Messages
1,614
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
I am using below code, I just want to add Auto sum at the end when blank cells are selected

This is how data looks in column I

and after adding a sum if the result is negative (<0) then move sum total to column H and if the sum is positive (>0) then move sum total to column G

1718020070118.png



VBA Code:
Sub SortAndAddBlankRowsAndSelectBlankCellsInColumnI()
    Dim ws As Worksheet
    Dim lastRowJ As Long
    Dim lastRowI As Long
    Dim i As Long
    Dim blankRange As Range
   
    ' Set the worksheet where your data is located
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your actual sheet name
   
    ' Find the last row in column J
    lastRowJ = ws.Cells(ws.Rows.Count, "J").End(xlUp).Row
   
    ' Sort the range J2 to the last used row
    ws.Range("J2:J" & lastRowJ).Sort key1:=ws.Range("J2"), _
                                      order1:=xlAscending, _
                                      Header:=xlNo
   
    ' Loop through each cell in column J starting from the last row and moving upwards
    For i = lastRowJ To 3 Step -1
        ' Check if the property name changes
        If ws.Cells(i, "J").Value <> ws.Cells(i - 1, "J").Value Then
            ' Insert a blank row below the current row
            ws.Cells(i, "J").EntireRow.Insert
        End If
    Next i
   
    ' Find the last row in column I
    lastRowI = ws.Cells(ws.Rows.Count, "I").End(xlUp).Row
   
    ' Define the range of blank cells in column I
    On Error Resume Next
    Set blankRange = ws.Range("I2:I" & lastRowI).SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
   
    ' Select the blank cells in column I
    If Not blankRange Is Nothing Then
        blankRange.Select
    Else
        MsgBox "No blank cells found in column I.", vbInformation
    End If
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
How about some simple formulas?

Book4
GHI
1Beginning Balance:1,245.00
2Additions to cashSubtractions from cashBalance
3240.001,005.00
4225.00780.00
5220.00560.00
6125.00300.00385.00
7385.00 
845.00340.00
91,500.00-1,160.00
10612.00-548.00
11100.00-448.00
12200.00-248.00
13 -248.00
Sheet2
Cell Formulas
RangeFormula
I3:I6,I8:I12I3=IF(ISNUMBER(I2)=FALSE,I1,I2)+G3-H3
G7,G13G7=IF(AND(I7="",I6>=0),I6,"")
H7,H13H7=IF(AND(I7="",I6<0),I6,"")
 
Upvote 0
Another approach:

Subtotals1.xlsm
EFGHIJK
1Subtractions from cashBalance
2240.00240.00
3225.00225.00
4220.00220.00
5300.00300.00
6985
745.0045.00
8225.00225.00
91143.751143.75
101087.501087.50
111012.501012.50
12900.00900.00
134413.75
14975.00975.00
15975.00975.00
16956.25956.25
17120.28120.28
183026.53
19
Sheet1



VBA Code:
Sub GetSubs2()

    Application.ScreenUpdating = False
    Dim subt As Double
    Dim TopofSection As Long, lastrow As Long

    lastrow = Cells(Rows.Count, "I").End(xlUp).Row
    Do Until lastrow < 2
        TopofSection = Cells(lastrow, "I").End(xlUp).Row
        If Cells(lastrow - 1, 9) = "" Then TopofSection = lastrow
        subt = WorksheetFunction.Sum(Range("I" & TopofSection, "I" & lastrow))
        If subt < 0 Then
            Range("H" & lastrow).Offset(1, 0).Value = subt
        End If
        If subt > 0 Then
            Range("G" & lastrow).Offset(1, 0).Value = subt
        End If
        lastrow = Cells(TopofSection, 9).End(xlUp).Row
    Loop
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
How about some simple formulas?

Book4
GHI
1Beginning Balance:1,245.00
2Additions to cashSubtractions from cashBalance
3240.001,005.00
4225.00780.00
5220.00560.00
6125.00300.00385.00
7385.00 
845.00340.00
91,500.00-1,160.00
10612.00-548.00
11100.00-448.00
12200.00-248.00
13 -248.00
Sheet2
Cell Formulas
RangeFormula
I3:I6,I8:I12I3=IF(ISNUMBER(I2)=FALSE,I1,I2)+G3-H3
G7,G13G7=IF(AND(I7="",I6>=0),I6,"")
H7,H13H7=IF(AND(I7="",I6<0),I6,"")

Thanks for the solution but I want a VBA solution.
 
Upvote 0
Another approach:

Subtotals1.xlsm
EFGHIJK
1Subtractions from cashBalance
2240.00240.00
3225.00225.00
4220.00220.00
5300.00300.00
6985
745.0045.00
8225.00225.00
91143.751143.75
101087.501087.50
111012.501012.50
12900.00900.00
134413.75
14975.00975.00
15975.00975.00
16956.25956.25
17120.28120.28
183026.53
19
Sheet1



VBA Code:
Sub GetSubs2()

    Application.ScreenUpdating = False
    Dim subt As Double
    Dim TopofSection As Long, lastrow As Long

    lastrow = Cells(Rows.Count, "I").End(xlUp).Row
    Do Until lastrow < 2
        TopofSection = Cells(lastrow, "I").End(xlUp).Row
        If Cells(lastrow - 1, 9) = "" Then TopofSection = lastrow
        subt = WorksheetFunction.Sum(Range("I" & TopofSection, "I" & lastrow))
        If subt < 0 Then
            Range("H" & lastrow).Offset(1, 0).Value = subt
        End If
        If subt > 0 Then
            Range("G" & lastrow).Offset(1, 0).Value = subt
        End If
        lastrow = Cells(TopofSection, 9).End(xlUp).Row
    Loop
    Application.ScreenUpdating = True
   
End Sub

Thank you very much, I want just a single code combined, this and the one I posted.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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