Loop to Selection.Delete Shift:=xlUp

Leandroarb

Board Regular
Joined
Oct 7, 2014
Messages
157
Hi,
Please, a need delete cells equal "*".
I try use this code:

Code:
Sub DelTry()
On Error Resume Next
Do While ActiveCell <> ""
    If ActiveCell.Value <> "*" Then
    ActiveCell.Offset(1, 0).Select
    Else
    Selection.Delete Shift:=xlUp
    End If
Loop
End Sub

But... suceless :(

The datas are in columns

[TABLE="width: 83"]
<tbody>[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]03/11/2018[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]04/11/2018[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]15/11/2018[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]02/11/2018[/TD]
[/TR]
[TR]
[TD]10/11/2018[/TD]
[/TR]
[TR]
[TD]11/11/2018[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]17/11/2018[/TD]
[/TR]
[TR]
[TD]18/11/2018[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]24/11/2018[/TD]
[/TR]
[TR]
[TD]25/11/2018[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
[TR]
[TD]*[/TD]
[/TR]
</tbody>[/TABLE]

Every form of help is appreciated.
Thank you.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I think part of the problem is "*" can be confused with the wildcard.
The "*" is ASCII character 42. So one way of doing this is to check for a length of one, and the ASCII code of that character be 42.
Also, you code will be much faster if you avoid using SELECT statements (which are not necessary).
Try this:

Code:
Sub DelTry()
    
    Dim lrow As Long
    Dim r As Long
    Dim c As Long
    
    Application.ScreenUpdating = False
    
'   Find column of active cell
    c = ActiveCell.Column
    
'   Find last row with data in active column
    lrow = Cells(Rows.Count, c).End(xlUp).Row
    
'   Loop through all rows from bottom to top
    For r = lrow To 1 Step -1
        If (Len(Cells(r, c)) = 1) And (Asc(Left(Cells(r, c), 1)) = 42) Then
            Rows(r).Delete
        End If
    Next r
                
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
This will do what I think you want to do. Please test on a COPY though the first time through.

Code:
Sub test()
Dim LastRow As Long
Dim i As Long

LastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = LastRow To 1 Step -1
    If Range("A" & i).Value = "*" Then
        Range("A" & i).EntireRow.Delete
    End If
Next i



End Sub
 
Upvote 0
Thank you Joe,
The problem is that it is deleting the whole line, and there are dozens of columns next to it. Only the cell has to be deleted.
Thank you very much for your help.
 
Upvote 0
The problem is that it is deleting the whole line, and there are dozens of columns next to it. Only the cell has to be deleted.
Sorry, I missed that.
Code:
That is an easy fix:
Sub DelTry()
    
    Dim lrow As Long
    Dim r As Long
    Dim c As Long
    
    Application.ScreenUpdating = False
    
'   Find column of active cell
    c = ActiveCell.Column
    
'   Find last row with data in active column
    lrow = Cells(Rows.Count, c).End(xlUp).Row
    
'   Loop through all rows from bottom to top
    For r = lrow To 1 Step -1
        If (Len(Cells(r, c)) = 1) And (Asc(Left(Cells(r, c), 1)) = 42) Then
            Cells(r, c).Delete Shift:=xlUp
        End If
    Next r
                
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Wowwwwwww Joe, thanks very mutch in absolute!
Great master!


Sorry, I missed that.
Code:
That is an easy fix:
Sub DelTry()
    
    Dim lrow As Long
    Dim r As Long
    Dim c As Long
    
    Application.ScreenUpdating = False
    
'   Find column of active cell
    c = ActiveCell.Column
    
'   Find last row with data in active column
    lrow = Cells(Rows.Count, c).End(xlUp).Row
    
'   Loop through all rows from bottom to top
    For r = lrow To 1 Step -1
        If (Len(Cells(r, c)) = 1) And (Asc(Left(Cells(r, c), 1)) = 42) Then
            Cells(r, c).Delete Shift:=xlUp
        End If
    Next r
                
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
OK, so apparently I was wrong in that it would treat the asterisk as a wild-card, and the IF statement that jproffer shows would work also (and is a bit simpler).
So, using that logic he used, my solution can be simplified to:
Code:
Sub DelTry()
    
    Dim lrow As Long
    Dim r As Long
    Dim c As Long
    
    Application.ScreenUpdating = False
    
'   Find column of active cell
    c = ActiveCell.Column
    
'   Find last row with data in active column
    lrow = Cells(Rows.Count, c).End(xlUp).Row
    
'   Loop through all rows from bottom to top
    For r = lrow To 1 Step -1
        If Cells(r, c).Value = "*" Then
            Cells(r, c).Delete Shift:=xlUp
        End If
    Next r
                
    Application.ScreenUpdating = True
    
End Sub
The other main difference between our codes is that jproffer's always looks at column A, and mine runs against whatever column the ActiveCell is in.
If you are always looking in one particular pre-defined column, I recommend taking his approach (fixing up the delete line).
If you could be in different columns and want it to run against the active one, then use mine.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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