簡體   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