簡體   English   中英

Excel Vba .find

[英]Excel Vba .find

大家好,馬克,我是VBA的新手。

我有兩個工作簿,其中包含需要比較的數據。 我目前正在使用此代碼進行比較。 它可以正常工作,並提供我需要的結果。

我要添加的功能是能夠在第二個工作簿范圍D:D的描述中進行搜索,並將匹配項帶回到活動工作簿中的單元格Cells(rw,4 )。 此信息將放置在Cells(rw,29 )中

我研究了find函數,但無法在兩個工作簿中使用它。 這里的挑戰是我搜索的工作簿或活動的工作簿名稱發生更改。

Sub VlookUpExampleDifferBooks()

'This example look up table in different book and sheet (TABLE 1 - ActiveSheet, TABLE 2 - CMF.xlxs and sheet1)
'Validate_Down Macro

Dim LastRow As Long
Dim rw As Long
Dim mx As Integer

Application.ScreenUpdating = False 'Find Last Row
LastRow = Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row

For rw = 11 To LastRow ' Loop until rw = Lastrow
Cells(rw, 27) = "'" & Cells(rw, 4)
Cells(rw, 28) = "'" & Cells(rw, 2)
Cells(rw, 25) = Application.VLookup(Cells(rw, 27), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("A:D"), 3, False) ' Vlookup function
Cells(rw, 26) = Application.VLookup(Cells(rw, 28), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("B:D"), 2, False) ' Vlookup function
Cells(rw, 20) = Application.VLookup(Cells(rw, 18), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("C:D"), 2, False) ' Vlookup function
Cells(rw, 19) = Application.VLookup(Cells(rw, 18), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("C:E"), 3, False) ' Vlookup function

If IsError(Cells(rw, 25)) Then Cells(rw, 25) = ""
If IsError(Cells(rw, 26)) Then Cells(rw, 26) = ""
If Cells(rw, 25) <> Cells(rw, 26) Then Cells(rw, 18) = Cells(rw, 25) & "/" & Cells(rw, 26)
If Cells(rw, 25) = Cells(rw, 26) Then Cells(rw, 18) = "'" & Cells(rw, 25)
If Cells(rw, 25) <> Cells(rw, 26) And Cells(rw, 26) = "" Then Cells(rw, 18) = "'" & Cells(rw, 25)
If Cells(rw, 25) <> Cells(rw, 26) And Cells(rw, 25) = "" Then Cells(rw, 18) = "'" & Cells(rw, 26)
If IsError(Cells(rw, 20)) Then Cells(rw, 20) = ""
If IsError(Cells(rw, 19)) Then Cells(rw, 19) = ""

Next

我相信您在使用FIND函數時遇到了麻煩,因為該函數旨在在單個單元格內查找值/字符串。 您正在嘗試搜索整個列。 解決問題的一種蠻力方法是遍歷原始數據列並檢查每個單元格是否具有所需的匹配項。 我在下面的代碼中包含了一個示例。

我使用了instr()函數而不是find 它們的工作原理幾乎相同,但是instr()在VBA中使用更加方便。

Sub VlookUpExampleDifferBooks()

'This example look up table in different book and sheet (TABLE 1 - ActiveSheet, TABLE 2 - CMF.xlxs and sheet1)
'Validate_Down Macro

Dim LastRow As Long
Dim rw As Long
Dim mx As Integer

Application.ScreenUpdating = False 'Find Last Row
LastRow = Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row

For rw = 11 To LastRow ' Loop until rw = Lastrow
Cells(rw, 27) = "'" & Cells(rw, 4)
Cells(rw, 28) = "'" & Cells(rw, 2)
Cells(rw, 25) = Application.VLookup(Cells(rw, 27), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("A:D"), 3, False) ' Vlookup function
Cells(rw, 26) = Application.VLookup(Cells(rw, 28), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("B:D"), 2, False) ' Vlookup function
Cells(rw, 20) = Application.VLookup(Cells(rw, 18), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("C:D"), 2, False) ' Vlookup function
Cells(rw, 19) = Application.VLookup(Cells(rw, 18), Workbooks("CMF Export.xlsx").Sheets("Sheet1").Columns("C:E"), 3, False) ' Vlookup function


'Loop through all cells in column D and check each one for the value in cells(rw,4)
For Each testcell In Workbooks("CMF Export.xlsx").Sheets("Sheet1").Range("D:D")
       If InStr(1, testcell.Value, Cells(rw, 4).Value, vbTextCompare) <> 0 Then 
            Cells(rw, 29).Value = Cells(rw, 4).Value
            Exit For

        End If
Next testcell

If IsError(Cells(rw, 25)) Then Cells(rw, 25) = ""
If IsError(Cells(rw, 26)) Then Cells(rw, 26) = ""
If Cells(rw, 25) <> Cells(rw, 26) Then Cells(rw, 18) = Cells(rw, 25) & "/" & Cells(rw, 26)
If Cells(rw, 25) = Cells(rw, 26) Then Cells(rw, 18) = "'" & Cells(rw, 25)
If Cells(rw, 25) <> Cells(rw, 26) And Cells(rw, 26) = "" Then Cells(rw, 18) = "'" & Cells(rw, 25)
If Cells(rw, 25) <> Cells(rw, 26) And Cells(rw, 25) = "" Then Cells(rw, 18) = "'" & Cells(rw, 26)
If IsError(Cells(rw, 20)) Then Cells(rw, 20) = ""
If IsError(Cells(rw, 19)) Then Cells(rw, 19) = ""

Next

希望這可以幫助!

-Jacob

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM