Share via

Left & Delete VBA Code

Weaver, Lillie 1 Reputation point
2022-09-14T17:25:57.93+00:00

I'm trying to delete any rows that do not start with " PDT" on all of my tabs. I'm having trouble with the VBA code and am trying to combine an if, left, and delete functions.

Dim ws As Worksheet, wb As Workbook
Dim frg As Range
For Each ws In Worksheets
ws.Activate
Set frg = Cells.Find(What:=" PDT", LookAt:=xlWhole)
If frg.Row <> 1 Then Row("A1").EntireRow.Delete
Next ws

Does someone know how to fix this?

Developer technologies | Visual Basic for Applications
0 comments No comments

1 answer

Sort by: Most helpful
  1. Michael Taylor 61,226 Reputation points
    2022-09-14T18:01:15.123+00:00

    The algorithm

       For Each worksheet  
          For Each Row  
             If First Column starts with 'PDT' Then  
                Delete Row  
    

    Maybe something like this.

       Public Sub DeletePdtRows()  
          Dim sheet As Worksheet  
          Dim row As Range  
            
          For Each sheet In Me.Worksheets  
             For Index = 1 To sheet.Rows.Count Step 0  
                Dim cellValue As Range  
                Set cellValue = sheet.Cells(Index, 1)  
                  
                If (StrComp(Left(cellValue.Value, 3), "PDT", VbCompareMethod.vbTextCompare) = 0) Then  
                   sheet(Index).Delete (xlShiftUp)  
                Else  
                   Index = Index + 1  
                End If  
             Next  
          Next  
       End Sub  
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.