VBA Removing and shifting up cells based on leftmost value

KasperC

New Member
Joined
May 11, 2023
Messages
49
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello again,

From what I can see I've copied codes that should work from different posts like this one, but I still can't get it to remove the rows in question.

I want to remove rows that has a number in the A column that either starts with a "2000" or "29" ex.: "20004576" or "2998372819" and then shift all the data up.
Right now it runs without issiues, the filter works, but it doesnt remove anything from the sheet.

VBA Code:
Private Sub Tspar()
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim adato As String
    Dim LastRow As Long
   
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Inn")
    LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    adato = Range("L1").Value
   
    Application.ScreenUpdating = False
   
    ws.Range("C1").AutoFilter Field:=3, Criteria1:=adato
        Range("L1").Value = ""
   
    LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
   
        For i = LastRow To 2 Step -1
            If Left(Cells(i, 1), 1) = "2000" _
            Or Left(Cells(i, 1), 1) = "29" _
            Then
                Cells(i, 1).EntireRow.Delete Shift:=xlUp
            End If
        Next
 
    Application.ScreenUpdating = True
End Sub

Any ideas?

Thank you,

Regards,
Kasper C

*edit: just wording correction
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
There seems to be errors in the second argument of your LEFT functions:
Rich (BB code):
            If Left(Cells(i, 1), 1) = "2000" _
            Or Left(Cells(i, 1), 1) = "29" _

It should be:
Rich (BB code):
            If Left(Cells(i, 1), 4) = "2000" _
            Or Left(Cells(i, 1), 2) = "29" _
 
Upvote 1
Solution
There seems to be errors in the second argument of your LEFT functions:
Rich (BB code):
            If Left(Cells(i, 1), 1) = "2000" _
            Or Left(Cells(i, 1), 1) = "29" _

It should be:
Rich (BB code):
            If Left(Cells(i, 1), 4) = "2000" _
            Or Left(Cells(i, 1), 2) = "29" _

Oha, so one has to specify how many characters one wants to look. Interesting.

Again, thank you so much!
 
Upvote 0

Forum statistics

Threads
1,215,038
Messages
6,122,798
Members
449,095
Latest member
m_smith_solihull

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