Delete row if Column C contains a number

guypt86

New Member
Joined
Jun 25, 2021
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
I'm using this code to delete rows if column C contains any number on sheet9 only.
But it works fine only when I'm on the same sheet (sheet9).

When I run the code when I'm on another sheet - I get an error: 'Type mismatch' pointing at row: "For I = 1 To UBound(V)"

If someone has a better code option for that, I'll be grateful : )



VBA Code:
Option Explicit
Sub delNumRows()
    Dim V As Variant
    Dim COL As Collection
    Dim I As Long
    Dim R As Range

V = Range(Cells(1, 3), Cells(Rows.Count, 3).End(xlUp))

Set COL = New Collection
For I = 1 To UBound(V)
    If V(I, 1) Like "*[1-9]*" Then COL.Add I
Next I

For Each V In COL
    If R Is Nothing Then
        Set R = Cells(V, 1).EntireRow
    Else
        Set R = Union(R, Cells(V, 1).EntireRow)
    End If
Next V

R.Delete

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

cortexnotion

Board Regular
Joined
Jan 22, 2020
Messages
146
Office Version
  1. 2013
Platform
  1. Windows
Hi @guypt86

Your range does not specify the sheet, so your code will look to the active sheet. Go with this:

V = ThisWorkbook.Sheets("Sheets9").Range(Cells(1, 3), Cells(Rows.Count, 3).End(xlUp))

You could definitely write something a lot shorter using visible cells only for example if you wanted to.
 

guypt86

New Member
Joined
Jun 25, 2021
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Hi @guypt86

Your range does not specify the sheet, so your code will look to the active sheet. Go with this:

V = ThisWorkbook.Sheets("Sheets9").Range(Cells(1, 3), Cells(Rows.Count, 3).End(xlUp))

You could definitely write something a lot shorter using visible cells only for example if you wanted to.

Thanks that make sense (y)

But for some reason I have a different error: " Application -defined or object-defined error" when I'm not at sheet9
 

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
1,234
Office Version
  1. 2010
Platform
  1. Windows
Hi,​
another way is to just move the original VBA procedure as it is to the worksheet module …​
 

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,783

ADVERTISEMENT

When I run the code when I'm on another sheet - I get an error: 'Type mismatch' pointing at row: "For I = 1 To UBound(V)"

I'd say there is no data in Col. C.

I have made a number of changes to your code in the following all of which I put a comment next to which should see the code now run without error on the active sheet:

VBA Code:
Option Explicit
Sub delNumRows()
    
    Dim V As Variant
    Dim COL As Collection
    Dim I As Long
    Dim R As Range
    
    Application.ScreenUpdating = False 'Turn screen updating off for more efficient processing.
    
    V = Range(Cells(1, 3), Cells(Rows.Count, 3).End(xlUp))
    
    If Not IsEmpty(V) Then 'Only try and delete rows if variant V has something in it.
        Set COL = New Collection
        For I = 1 To UBound(V)
            If IsError(V(I, 1)) = False Then 'Skip over any cell in error like #N/A.
                If V(I, 1) Like "*[1-9]*" Then
                    COL.Add I
                End If
            End If
        Next I
        For Each V In COL
            If R Is Nothing Then
                Set R = Cells(V, 1).EntireRow
            Else
                Set R = Union(R, Cells(V, 1).EntireRow)
            End If
        Next V
        If Not R Is Nothing Then 'Only try and delete rows if range R has something in it.
            R.Delete
        End If
    End If
    
    Application.ScreenUpdating = True 'Turn screen updating back on.

End Sub

Regards,

Robert
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,874
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
I might be missing something here, but why not this in the sheet module
VBA Code:
Sub MM1()
Dim r As Long
For r = Cells(Rows.Count, "C").End(xlUp).Row To 1 Step -1
    If IsNumeric(Cells(r, 3)) Then Rows(r).Delete
Next r
End Sub
 

vcoolio

Well-known Member
Joined
Jun 29, 2014
Messages
1,163
Hello Guypt86,

I'm with Michael on this one but, with basically one line, you could also simplify it further:-

VBA Code:
Sub Test()

On Error Resume Next
      Sheet9.Range("C2", Sheet9.Range("C" & Sheet9.Rows.Count).End(xlUp)).SpecialCells(2, 1).EntireRow.Delete
On Error GoTo 0

End Sub

I hope that this helps.

Cheerio,
vcoolio.
 

Forum statistics

Threads
1,140,925
Messages
5,703,205
Members
421,280
Latest member
Jaycee01

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
Top