Sufiyan97
Well-known Member
- Joined
- Apr 12, 2019
- Messages
- 1,585
- Office Version
- 365
- 2013
- Platform
- 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
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
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