Delete Cells based on value of one cell

beginner-excel

New Member
Joined
Jun 30, 2008
Messages
36
Hello,

I have three cells with data (Subtotal; (blank) means there's no data in the cell):

Column H | Column I | Column J | Column K | Column L
----------------------------------------------------
Subtotal | (blank) | 0.0 | 0.0 | (blank)
-----------------------------------------------------
(blank row)
-----------------------------------------------------
Total | (blank) | 3.0 | 1.0 | (blank)

I want to find Subtotal and if it has 0.0 and 0.0 in columns J and K, then delete those cells (not the entire row only the cells), and move cells up so that the result is:

Column H | Column I | Column J | Column K | Column L
----------------------------------------------------
(blank cells)
-----------------------------------------------------
Total | (blank) | 3.0 | 1.0 | (blank)

If possible, I'd like to delete the blank cells as well.

Help! I tried to do a search for deleting cells based on the value of one cell but only saw deleting rows based on the value of one cell.:eek:
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Upvote 0
Dear B-E

This is lengthy, but it does the job by using the built in excel sort instead of shift cells up. Basically, read the incoming ranges, turn all 0's into blanks, then sort each column in the range one column at a time from toprow to bottomrow of the selection. Selection can be a single set of columns/rows (contiguous) or split columns/rows (non contiguous)

Copy the code below to a module and run as a macro or put a command button on the sheet. If code on the sheet and not in module, get rid of keyword Public.

Robert Phillips
Westlake, Ohio


Public Sub test()
Dim AreaCount, CellCount, DollarPos, i As Integer
Dim LeftCol, RightCol, BottomRow, TopRow As Long
Dim Col$, Row1$, Row2$
Dim Cell As Range
Dim NewSelection As Range
Dim LeftAddr, RightAddr As Range
Dim RangeAddr As Variant

AreaCount = Selection.Areas.Count

If AreaCount <= 1 Then
'ONE CONTIGUOUS SELECTION AREA
'CONTIGUOUS COLUMNS,ROWS, NO SEPARATE RANGE AREAS

Set NewSelection = Range(Selection.Address)
CellCount = NewSelection.Count

'ONLY PROCESS A RANGE WITH MORE THAN 1 CELL
If CellCount > 1 Then
RangeAddr = Split(NewSelection.Address, ":")

Set LeftAddr = Range(RangeAddr(0)) 'draw out first column/row
LeftCol = LeftAddr.Column
TopRow = LeftAddr.Row

Set RightAddr = Range(RangeAddr(1)) 'draw out last column/row
RightCol = RightAddr.Column
BottomRow = RightAddr.Row

'LIMITED TO COLS A-Z FOR NOW UNLESS YOU WANT THE REST
If Selection.Columns.Count > 1 Then
Row1$ = CStr(TopRow)
Row2$ = CStr(BottomRow)
'Process All Columns
For i = LeftCol To RightCol
Col$ = Chr$(i + 64)
Set NewSelection = Range(Col$ & Row1$ & ":" & Col$ & Row2$)
GoSub BlankAndSort
Next
Else
'Single Column
GoSub BlankAndSort
End If
End If
Else
'MULTIPLE AREAS SELECTED
For i = 1 To AreaCount
Set NewSelection = Selection.Areas(i)
CellCount = NewSelection.Count
'ONLY PROCESS A RANGE WITH MORE THAN 1 CELL
If CellCount > 1 Then
GoSub BlankAndSort
End If
Next
End If

Exit Sub

BlankAndSort:
For Each Cell In NewSelection
If Cell.Value = 0 Then
Cell.Value = ""
End If
Next
RangeAddr = Split(NewSelection.Address, ":")
Set LeftAddr = Range(RangeAddr(0)) 'draw out top cell for sort key
NewSelection.Sort Key1:=Range(LeftAddr.Address), Order1:=xlAscending, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Return

End Sub
 
Upvote 0
Forgot, with the subroutine i supplied, it's expected that the selection was already made before the routine was invoked.

RLP
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,142
Members
448,551
Latest member
Sienna de Souza

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