Fixing error after finishing command in VBA

Jovinchious

New Member
Joined
Oct 12, 2022
Messages
17
Office Version
  1. 2007
Platform
  1. Windows
Hi all,

I am trying to erase all rows which do not include my date (for example 17.10.2022) in column A.

Sub DELETEDATE()
Application.ScreenUpdating = False
Dim x As Long
For x = [a1].SpecialCells(xlCellTypeLastCell).Row To 1 Step -1
Debug.Print Cells(x, "A").Value
If CDate(Cells(x, "A")) <> CDate("17/10/2022") Then
Cells(x, "A").EntireRow.Delete
End If
Next x
End Sub


...i use this type of code, and it works completely fine. It just gives an error after finish. Anyone knows how to do it without popping error at the end?
I need error-free code because I have more macro commands that I want to run at the same time, so this is slowing me down.
Tnx!
 

Attachments

  • Capture1.PNG
    Capture1.PNG
    5.5 KB · Views: 4

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
It sounds like you may have some values in column A that cannot be converted into Dates.
Are they already dates or do you have any non-date entries?
Can you show us a sampling of what your entries in column look like?
 
Upvote 0
It just gives an error after finish.

It sounds like you may have some values in column A that cannot be converted into Dates.
I think Joe is on the money, it just needs to be taken a little further.
Your For loop is running all the way to Row 1, which I expect is your column header and obviously not a date.
Change the loop to finish at Row 2

Rich (BB code):
For x = [a1].SpecialCells(xlCellTypeLastCell).Row To 2 Step -1
 
Upvote 0
Solution
I think Joe is on the money, it just needs to be taken a little further.
Your For loop is running all the way to Row 1, which I expect is your column header and obviously not a date.
Change the loop to finish at Row 2

Rich (BB code):
For x = [a1].SpecialCells(xlCellTypeLastCell).Row To 2 Step -1
the answer was right in front of my nose. Thank you!!!
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
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