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
 

MKLAQ

Board Regular
Joined
Jan 30, 2021
Messages
108
Office Version
  1. 2016
Platform
  1. Windows
but it is still the error in word "month"
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

MKLAQ

Board Regular
Joined
Jan 30, 2021
Messages
108
Office Version
  1. 2016
Platform
  1. Windows
nothing shows I choose more than month and I'm sure about my format date mm/dd/yyyy
 

MKLAQ

Board Regular
Joined
Jan 30, 2021
Messages
108
Office Version
  1. 2016
Platform
  1. Windows
I fixed , you indicate to year 2021 but my year 2020 that's why nothing shows
thanks buddy for your assistance ;)
 

MKLAQ

Board Regular
Joined
Jan 30, 2021
Messages
108
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

I have another problem what I say is mystery the month of December doesn't show
do you have any idea?:mad:
 

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,090
Office Version
  1. 2016
Platform
  1. Windows
A problem with the dateformat: 12-1-2021 ==> 1-12-2021 (12 january 2021)

A solution without day and year:

VBA Code:
Sub test()
    Dim month As String
    Dim monthnumber As Integer ' instead of 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, Operator:=xlFilterDynamic, Criteria1:=20 + monthnumber
     End With
End Sub
 
Solution

MKLAQ

Board Regular
Joined
Jan 30, 2021
Messages
108
Office Version
  1. 2016
Platform
  1. Windows
well done ! thanks so much (y)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,383
Messages
5,635,951
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