Delete row if field contains Apostrophe

Peterfc2

Active Member
Joined
Jan 2, 2004
Messages
404
Office Version
  1. 2013
Platform
  1. Windows
Text in column A. This nearly is what I need but as it removes the rows with an apostrophe. But it also deletes my top row that has hasn't got one.

VBA Code:
Sub DeleteRows()
Dim Lastrow  As Long
Dim LastCol As Long
Dim MyRg As Range
    If (ActiveSheet.AutoFilterMode) Then ActiveSheet.AutoFilterMode = False '  REMOVE  AUTOFILTER  IF  EXIST
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    Set MyRg = Range(Range("A1"), Cells(Lastrow, LastCol))
    MyRg.AutoFilter Field:=1, Criteria1:="=*'*", Operator:=xlAnd
    MyRg.EntireRow.Delete
    If (ActiveSheet.AutoFilterMode) Then ActiveSheet.AutoFilterMode = False '  REMOVE  AUTOFILTER  IF  EXIST
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Text in column A. This nearly is what I need but as it removes the rows with an apostrophe. But it also deletes my top row that has hasn't got one.

VBA Code:
Sub DeleteRows()
Dim Lastrow  As Long
Dim LastCol As Long
Dim MyRg As Range
    If (ActiveSheet.AutoFilterMode) Then ActiveSheet.AutoFilterMode = False '  REMOVE  AUTOFILTER  IF  EXIST
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    Set MyRg = Range(Range("A1"), Cells(Lastrow, LastCol))
    MyRg.AutoFilter Field:=1, Criteria1:="=*'*", Operator:=xlAnd
    MyRg.EntireRow.Delete
    If (ActiveSheet.AutoFilterMode) Then ActiveSheet.AutoFilterMode = False '  REMOVE  AUTOFILTER  IF  EXIST
End Sub
Does this work.

VBA Code:
Public Sub subDeleteRows()

  ActiveWorkbook.Save
 
  Application.ScreenUpdating = False
  
  With ActiveSheet
  
    If .AutoFilterMode Then
      .AutoFilterMode = False
    End If
  
    With .Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
        
      .AutoFilter 1, "*-*"
          
      On Error Resume Next
      .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
      On Error GoTo 0
          
      .AutoFilter
       
    End With
    
    .AutoFilterMode = False
  
  End With

  Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
Make some changes in your code.
VBA Code:
    MyRg.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    '    MyRg.EntireRow.Delete
 
Upvote 0
Does this work.

VBA Code:
Public Sub subDeleteRows()

  ActiveWorkbook.Save
 
  Application.ScreenUpdating = False
 
  With ActiveSheet
 
    If .AutoFilterMode Then
      .AutoFilterMode = False
    End If
 
    With .Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
       
      .AutoFilter 1, "*-*"
         
      On Error Resume Next
      .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
      On Error GoTo 0
         
      .AutoFilter
      
    End With
   
    .AutoFilterMode = False
 
  End With

  Application.ScreenUpdating = True

End Sub
"*-*" changed to"*'*" and its works fine thank you
 
Upvote 0

Forum statistics

Threads
1,223,577
Messages
6,173,164
Members
452,504
Latest member
frankkeith2233

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