2017-02-17 42 views
0

我有一個用戶窗體有兩個文本框命名爲「日期」和「移位」,按鈕來觸發代碼。還有一個名爲data.xlsx的excel文件,「sheet1」的A列的日期已添加爲07/02/2017,B列的添加爲A/B/C。用VBA找到多個條件

date.value = "07/02/2017" 
shift.value = "C" 

,所以我想要做的是找到一個列的行數中包含「2017年7月2日」和B列中包含「C」的data.xlsx。

+0

什麼樣的方法你試過這麼遠嗎?提供正在進行的工作。 – Zerk

+0

使用MATCH()函數怎麼樣? –

回答

0

請嘗試下面的代碼以找到列B中的Shift行(使用Match函數)。

您應該可以進行修改以使其也可以從dateTextBox中查找日期。

代碼

Sub CommandButton1_Click() 

' this code goes inside the command button (inside the User_Form module) 
Dim ValToSearch 
Dim MatchRes As Variant 

ValToSearch = Me.shift.Value '<-- get the value to look for 

With Worksheets("Sheet1") 
    MatchRes = Application.Match(ValToSearch, .Range("B:B"), 0) 
    If IsError(MatchRes) Then '<-- match not found 
     MsgBox "Not found" 
    Else 
     MsgBox "Found at row " & MatchRes 
    End If 
End With 

End Sub 
+0

感謝您的回答。但是我看到你只在B列中找到shift.value。 A列中的date.value怎麼樣?我需要同時找到兩個。 –

+0

@MahmutUzun我有你這個概念,肯定你可以盡最大努力去適應另一種情況 –

+0

我已經通過使用「if」和chechking其他列來了這麼遠。但它會帶來另一個問題。謝謝你的回答。 –