remove cells with minus value and adjacent cells

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,604
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


[table="width: 150, class: grid"]
[tr]
[td]Test 1[/td]
[td]200[/td]
[/tr]
[tr]
[td]Test 2[/td]
[td]-300[/td]
[/tr]
[tr]
[td]Test 3[/td]
[td]1345[/td]
[/tr]
[/table]

would result after code...

[table="width: 150, class: grid"]
[tr]
[td]Test 1[/td]
[td]200[/td]
[/tr]
[tr]
[td]Test 3[/td]
[td]1345[/td]
[/tr]
[/table]

Thanks in advnce
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
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,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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