繁体   English   中英

在Excel VBA中查找功能

[英]Find Function in Excel VBA

我想搜索属于一个月的所有日期。 用户将选择月份。 我无法搜索所有日期,只能获取输入的最后一个日期。 我正在使用查找功能,无法提供具体条件。

    mois = InputBox("Choisissez le mois (Entrer la valeur Numérique)!!! (1 pour Janvier, 2 pour Fév .... )", "Titre")
   If mois > 0 & mois < 12 Then
    ' Search for matching date
    Set cellsearch = Range("G1:G" & NbrLinesDate).Find(What:=mois_chercher)

    If cellsearch Is Nothing Then
        If mois < 0 Then
        ElseIf mois > 12 Then
        End If
    Else
    ligne = cellsearch.Row
    Date_to_search = Range("G" & ligne).Value
End If
End If
    MsgBox Date_to_search                           '' Checkpoint_1
    JourTest = Day(Date_to_search)
    JourTest = Trim(JourTest)
    MsgBox JourTest                                 '' Checkpoint_2

Mois是变量,用于存储用户提供的月份值。 在这段代码中,我没有使用此变量mois搜索属于该月的日期。 我无法实现。

Date_de_Survenance
 30/01/2013
 31/01/2013
 31/01/2013
 04/02/2013
 05/02/2013
 07/02/2013
 11/02/2013
 13/02/2013
 13/02/2013
 13/02/2013
 15/02/2013
 20/02/2013

请参阅此示例。 我不是使用Inputbox进行输入,而是使用硬编码值进行演示。

可以说您的Excel数据如下所示。

在此处输入图片说明

只需将此代码粘贴到模块中并运行即可。

'
' Excel Constants for Months for Autofilter
'
'   xlFilterAllDatesInPeriodJanuary = 21
'   xlFilterAllDatesInPeriodFebruray = 22
'   xlFilterAllDatesInPeriodMarch = 23
'   xlFilterAllDatesInPeriodApril = 24
'   xlFilterAllDatesInPeriodMay = 25
'   xlFilterAllDatesInPeriodJune = 26
'   xlFilterAllDatesInPeriodJuly = 27
'   xlFilterAllDatesInPeriodAugust = 28
'   xlFilterAllDatesInPeriodSeptember = 29
'   xlFilterAllDatesInPeriodOctober = 30
'   xlFilterAllDatesInPeriodNovember = 31
'   xlFilterAllDatesInPeriodDecember = 32


Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, lMnth As Long, constmonth As Long

    '~~> Feb
    lMnth = 2
    constmonth = lMnth + 20 '~~> (See the commented section for constants)

    '~~> Change this to the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> Remove any filters
        .AutoFilterMode = False

        With .Range("A1:A" & lRow)
            .AutoFilter Field:=1, Criteria1:= _
            constmonth, Operator:=xlFilterDynamic
        End With
    End With
End Sub

输出量

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM