code filter data based on month & name doesn't work

MKLAQ

Board Regular
Joined
Jan 30, 2021
Messages
108
Office Version
  1. 2016
Platform
  1. Windows
hi
I have this data in sheet source it should copy filter data based on two condition
if h1= name then filtering it's ok but if I select only i1 = month and if I select h1,i1 together the code doesn't work
datenameinv nocasepaidremainedalanoctober
10/01/2021alanas123paid10002200
10/02/2021johnas124unpaid-1232
10/03/2021alanasd123paid1000264
10/04/2021johnasd124unpaid-264
10/05/2021roebertoasd125paid1000264
10/06/2021saraasd126unpaid-264
10/07/2021caliasd127paid1000264
10/08/2021caroonasd128unpaid-264
10/09/2021caninasd129paid1000264

VBA Code:
'maniacb
Private Sub Worksheet_Change(ByVal Target As Range)
Dim month As String
Dim name As String
Dim lr, i As Long
Dim Rng As Range
Dim k As Integer
Dim source As Worksheet
Dim targetforecastmonth As Worksheet
Application.ScreenUpdating = False
Application.EnableEvents = False
If Not Intersect(Range("H1:I1"), Target) Is Nothing Then

    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Set source = Worksheets("source")
    Set targetforecastmonth = Worksheets("result")
    month = source.Range("I1")
    name = source.Range("H1")
    For i = 1 To lr
        source.Range("G" & i) = source.Range("A" & i)
        source.Range("G" & i).NumberFormat = "mmm"
        source.Range("G" & i) = StrConv(source.Range("G" & i).Text, vbLowerCase)
    Next i

    targetforecastmonth.Cells(2, 1).CurrentRegion.Clear
    Set Rng = source.Range(Cells(1, 1), Cells(lr, 7))
    With Rng
        .AutoFilter
        If name <> "" Then
            .AutoFilter 2, Criteria1:=name
        End If
        If month <> "" Then
            .AutoFilter 7, Criteria1:=month
        End If
        .SpecialCells(xlCellTypeVisible).Copy
        targetforecastmonth.[A1].PasteSpecial xlPasteAll
        .AutoFilter
    End With

    source.Range("G1:G" & lr).Value = ""
    targetforecastmonth.Range("G1:G" & lr).Value = ""
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,090
Office Version
  1. 2016
Platform
  1. Windows
.AutoFilter 7 search in column 7, but column 7 is empty.

VBA Code:
        If name <> "" And month <> "" Then
            .AutoFilter 2, Criteria1:=name
            .AutoFilter 7, Criteria1:=month
        Else
             If name <> "" Then
                 .AutoFilter 2, Criteria1:=name
             End If
             If month <> "" Then
                 .AutoFilter 7, Criteria1:=month
             End If
        End If
 

MKLAQ

Board Regular
Joined
Jan 30, 2021
Messages
108
Office Version
  1. 2016
Platform
  1. Windows
you mean filter based on column 1 about month I did it nothing change
 

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,090
Office Version
  1. 2016
Platform
  1. Windows
Because you wil match OCTOBER with 01/10/2021. That is not the same.
You need to rebuild a date from your text OCTOBER.
Something like this:

VBA Code:
    arr = Array("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december")
    monthnumber = Application.Match(month, arr, False)
    .AutoFilter Field:=1, Criteria1:= ">=" & monthnumber & "-1-2021", Operator:=xlAnd, Criteria2:="<" & monthnumber + 1 & "-1-2021"
 

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,090
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

deleted
 

MKLAQ

Board Regular
Joined
Jan 30, 2021
Messages
108
Office Version
  1. 2016
Platform
  1. Windows
I tested your code but it gives me error about word month "argument not optional"
this is the code
VBA Code:
Sub test()
Dim monthnumber As String
monthnumber = Range("i1").Value
     arr = Array("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december")
    monthnumber = Application.Match(month, arr, False)
    .AutoFilter Field:=1, Criteria1:=">=" & monthnumber & "-1-2021", Operator:=xlAnd, Criteria2:="<" & monthnumber + 1 & "-1-2021"
End Sub
 

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,090
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

month= Range("i1").Value
 

MKLAQ

Board Regular
Joined
Jan 30, 2021
Messages
108
Office Version
  1. 2016
Platform
  1. Windows
then shows another error in this word .AutoFilter Field:=1 "invalid or unqualified reference
 

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,090
Office Version
  1. 2016
Platform
  1. Windows
VBA Code:
Sub test()
    Dim monthnumber As String
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Set source = Worksheets("source")
    month = source.Range("I1")
    arr = Array("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december")
    monthnumber = Application.Match(month, arr, False)
    Set Rng = source.Range(Cells(1, 1), Cells(lr, 7))
    With Rng
         .AutoFilter Field:=1, Criteria1:=">=" & monthnumber & "-1-2021", Operator:=xlAnd, Criteria2:="<" & monthnumber + 1 & "-1-2021"
     End With
End Sub
 

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,090
Office Version
  1. 2016
Platform
  1. Windows
I don't know if the date format is correct.
When you simulate with hand the filter with the macrorecorder on. Then you can see the correct date format: - - or / /
 

Watch MrExcel Video

Forum statistics

Threads
1,129,383
Messages
5,635,942
Members
416,889
Latest member
dhegs

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
Top