Deleting row when condition is met

Gokuba

New Member
Joined
Aug 6, 2018
Messages
19
HI,

I have a row of data and want to delete a row if the condition is true. What is wrong with my code?




Sub deltotal()
Lastr = Cells(rows.Count, "A").End(xlUp).row
ctes = Range(x, 1)
For x = 1 To Lastr Step 1
If Application.WorksheetFunction.Left(ctes, 18) = "Total for Customer" Then
row.Delete




Next x
Else
MsgBox ("NONON")








End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi,

When building your VBA code I recommend using indentation so that you make sure your IF Statement is properly structured:

Code:
Sub deltotal()
    Dim Lastr               As Long
    Dim x                   As Long


    Lastr = Cells(Rows.Count, "A").End(xlUp).Row


    For x = Lastr To 1 Step -1
        If Left(Range("A" & x).Value, 18) = "Total for Customer" Then
            Range("A" & x).Rows.Delete
        Else
            MsgBox ("NONON")
        End If
    Next x


End Sub

Also, because you are going to delete your rows, it's better to start your For...Next statement from the last row and then use Step -1.

Hope it helps!
 
Upvote 0
Try this

Code:
Sub deltotal()
    Dim lastR, ctes As Range, x As Long
    With ActiveSheet
        lastR = .Cells(Rows.Count, "A").End(xlUp).Row
        For x = lastR To 1 Step -1
            Set ctes = .Cells(x, 1)
            If Left(ctes, 18) = "Total for Customer" Then
                ctes.EntireRow.Delete
            Else
                'do nothing
            End If
        Next x
    End With
End Sub

What is wrong with my code?
A few pointers...

Dim lastR, ctes As Range, x As Long
always declare your variables
- some of your errors become easier to spot when code stops due to variable type being wrong etc

ctes = Range(x, 1)
use (x,1) notation with Cells not Range
also, a Range must be set
Code:
Either
Set ctes = Cells(x, 1)   
or 
Set ctes = Range("A" & x)

row.Delete
must tell VBA which row
Code:
ctes.EntireRow.Delete

IF Application.WorksheetFunction.Left(ctes, 18) =
Some things are using wotksheet functions others are VBA functions in their own right
Sometimes names differ (CHAR in Excel vs CHR in VBA) , but sometimes (eg here!) the names are the same
You have to learn which is which
Code:
IF Left(ctes, 18) = "Total for Customer" Then

For x = lastR To 1 Step -1
start at bottom and count backwards when deleting rows
(otherwise row numbers are changing ahead of the code)
 
Last edited:
Upvote 0
Thank you Both, it solved all my code problems and taught new things. Problem solved.
THANK YOU SO MUCH ( and Dziekuje Justyna ;) )
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,241
Members
449,075
Latest member
staticfluids

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