remove cells with minus value and adjacent cells

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,602
Office Version
  1. 365
Platform
  1. Windows
Can someone help with code that will look down range B1:B50 and when a minus sum is found, delete that cell and the adjacent value in column A, then remove those blank cells. Not remove entire row - just blank cells in columns A:B


example


Test 1200
Test 2-300
Test 31345

would result after code...

Test 1200
Test 31345

Thanks in advnce
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try this:

Code:
Dim sh As Worksheet, lr As Long, rng As Range, c As Range

Set sh = Sheets("Sheet1")
lr = sh.Range("B" & sh.Rows.Count).End(xlUp).Row

For Each c In sh.Range("B1:B" & lr)
    If c < 0 Then
        If Not rng Is Nothing Then
            Set rng = Union(rng, c)
        Else
            Set rng = c
        End If
    End If
Next
If Not rng Is Nothing Then
    Union(rng, rng.Offset(, -1)).Delete Shift:=xlUp
End If
 
Upvote 0
How about this?

Code:
Option ExplicitDim RowLoop As Long
Dim LastRowNo As Long


Sub Macro1()
'
LastRowNo = ActiveSheet.Range("B65536").End(xlUp).Row 'set range to 1048576 if your sheet can go that high


For RowLoop = LastRowNo To 1 Step -1
    If ActiveSheet.Range("B" & RowLoop).Value < 0 Then
        Range("A" & RowLoop & ":B" & RowLoop).Delete Shift:=xlUp
    End If
Next RowLoop
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,550
Messages
6,114,265
Members
448,558
Latest member
aivin

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