Problem with macro

Jeeremy7

Board Regular
Joined
May 13, 2020
Messages
110
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a macro running but i think some informations are in double so the macro doesn't work..
If somoene could help it would be really nice :)

Here's the code
VBA Code:
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    For r = lr To 1 Step -1
        If (Cells(r, "B") Like "") Then
            If Rng Is Nothing Then Set Rng = Rows(r) Else Set Rng = Union(Rng, Rows(r))
        End If

    lr = Cells(Rows.Count, "A").End(xlUp).Row
        If Not (Cells(r, 1) Like "##-####") Then
            If Rng Is Nothing Then Set Rng = Rows(r) Else Set Rng = Union(Rng, Rows(r))
        End If
    Next r
    If Not Rng Is Nothing Then Rng.Delete
    
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.EnableEvents = True

Thanks ! :)
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
What should be left afterwards?

Just this information

Copy of Copy of TEMPLATE - Lump Sum Direct cost.xlsm
AB
101-01023835
201-0121495
301-013757.58
401-52015
502-7403425
606-1101125.84
708-11011743
Direct Cost
 
Upvote 0
Ok, try
VBA Code:
lr = Cells(Rows.Count, "A").End(xlUp).Row
    For r = lr To 2 Step -1
        If Cells(r, "B") = "" Then
            If Rng Is Nothing Then Set Rng = Rows(r) Else Set Rng = Union(Rng, Rows(r))
        End If
       
        If IsError(Cells(r, 1)) Then
            If Rng Is Nothing Then Set Rng = Rows(r) Else Set Rng = Union(Rng, Rows(r))
        ElseIf Not Cells(r, 1) Like "##-####" Then
            If Rng Is Nothing Then Set Rng = Rows(r) Else Set Rng = Union(Rng, Rows(r))
        End If
    Next r
    If Not Rng Is Nothing Then Rng.Delete
 
Upvote 0
Almost perfect :biggrin:
I just have a value that stayed i'm not sure why (row1)

Copy of Copy of TEMPLATE - Lump Sum Direct cost.xlsm
ABC
1-Amount P.O.#
201-01023835
301-0121495
401-013757.58
501-52015
602-7403425
706-1101125.84
808-11011743
908-1102303.75
1008-7002149.11
1109-200313900
1209-650311100
1309-91034600
1421-13031294.44
1523-300311314
1626-60039658.6
Direct Cost
 
Upvote 0
It's ok I pulled differents data and this row always stays I'll just create a .delete for A

Thanks a lot for you help Fluff it's not the first time and it's always answering the question !
 
Upvote 0
I assumed that was a header row & should be left, change this
Rich (BB code):
For r = lr To 1 Step -1
 
Upvote 0
I think it could be simplified a bit like this:

VBA Code:
Sub test()
  Dim lr As Long, r As Long, rng As Range
  lr = Cells(Rows.Count, "A").End(xlUp).Row
  Set rng = Rows(lr + 1)
  For r = lr To 1 Step -1
    If IsError(Cells(r, 1)) Then
      Set rng = Union(rng, Rows(r))
    ElseIf Cells(r, "B") = "" Or IsError(Cells(r, 1)) Or Not Cells(r, 1) Like "##-####" Then
      Set rng = Union(rng, Rows(r))
    End If
  Next r
  rng.Delete
End Sub
 
Upvote 0
I think it could be simplified a bit like this:

VBA Code:
Sub test()
  Dim lr As Long, r As Long, rng As Range
  lr = Cells(Rows.Count, "A").End(xlUp).Row
  Set rng = Rows(lr + 1)
  For r = lr To 1 Step -1
    If IsError(Cells(r, 1)) Then
      Set rng = Union(rng, Rows(r))
    ElseIf Cells(r, "B") = "" Or IsError(Cells(r, 1)) Or Not Cells(r, 1) Like "##-####" Then
      Set rng = Union(rng, Rows(r))
    End If
  Next r
  rng.Delete
End Sub
That's working too thanks ! :)
 
Upvote 0

Forum statistics

Threads
1,214,551
Messages
6,120,156
Members
448,948
Latest member
spamiki

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