VBA - Help!

ChrisFoster

Active Member
Joined
Jun 21, 2019
Messages
256
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the below code which looks for any dates from current year in column B and deletes those rows. Every time I run this code it will be on data from a specific month but could be different years (so always November but could be November 2000 all the way to November 2019). It seems to work fine when using any month that isn't the current month.

Code:
Dim c As Range
    With Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row)
    Set c = .Find(What:=Year(Date), LookIn:=xlFormulas, lookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If Not c Is Nothing Then
        .AutoFilter Field:=2, Criteria1:=xlFilterThisYear, Operator:=xlFilterDynamic
        .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
    End If
    End With

I'm trying to use the code for data from October and it debugs at the below stage. Please can someone help?

Code:
.AutoFilter Field:=2, Criteria1:=xlFilterThisYear, Operator:=xlFilterDynamic

Regards,

Chris
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You dont have a field 2 in a single column range.

My VBA skills are really quite limited, so apologies in advance, I got this code from searching the web and from help on here. What do you mean by your above statement?

The strange thing though is that it works perfectly fine if I manually delete any rows that contain '2019' before running the code. And works fine for any data that is for another month other than current month.
 
Upvote 0
Really? Far as i can tell that can never work as supplied regardless of the data. You are using B1:B whatever. There is only a field 1 as there is only one column being filtered. Change the Field:=2 to a 1.
 
Upvote 0
Really? Far as i can tell that can never work as supplied regardless of the data. You are using B1:B whatever. There is only a field 1 as there is only one column being filtered. Change the Field:=2 to a 1.

Brill! That's sorted it, thanks so much.
Also, thanks for the info, it makes more sense to me now.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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