我有一個作業簿,我需要使用 vlookup 在 Sheet1、C13:c31 和單元格 H13:H31 的每一行中填充結果。查找值位于 Sheet1 的單元格 B13:B31 中。我的表格陣列在 Sheet4 中。我需要在 Sheet4、C 和 D 列中獲取結果,并在 Sheet 1、單元格 c13:c31 和單元格 H13:h31 中顯示結果。(我不想顯示公式,這就是我想使用 VBA 的原因。
請檢查我下面的代碼,因為它不起作用。
Sub Vlookup()
Dim c As Range
Dim d As Range
If Sheet1.Range("B13").Value = "*" Then
c = Application.WorksheetFunction.Vlookup(Sheet1.Range("B13").Value, Sheet4.Range("A:E"), 3, False)
d = Application.WorksheetFunction.Vlookup(Sheet1.Range("B13").Value, Sheet4.Range("A:E"), 4, False)
End If
If Sheet1.Range("B14").Value = "*" Then
c = Application.WorksheetFunction.Vlookup(Sheet1.Range("B13").Value, Sheet4.Range("A:E"), 3, False)
d = Application.WorksheetFunction.Vlookup(Sheet1.Range("B13").Value, Sheet4.Range("A:E"), 4, False)
End If
If Sheet1.Range("B15").Value = "*" Then
c = Application.WorksheetFunction.Vlookup(Sheet1.Range("B13").Value, Sheet4.Range("A:E"), 3, False)
d = Application.WorksheetFunction.Vlookup(Sheet1.Range("B13").Value, Sheet4.Range("A:E"), 4, False)
End If
End Sub
uj5u.com熱心網友回復:
if 陳述句可能有問題。
您可以嘗試If IsEmpty(Range("B13").Value) = False Then代替通配符“*”。
uj5u.com熱心網友回復:
這應該讓您了解如何完成它:
Sub Vlookup()
Dim c As Range, v, r1, r2, rngSearch As Range
Set rngSearch = Sheet4.Range("A:E")
For Each c In Sheet1.Range("B13:C31").Cells 'loop the input range
v = c.Value
If Len(v) > 0 Then 'is there anything to look up?
'drop the `WorksheetFunction` to prevent run-time
' error if there's no match
v1 = Application.Vlookup(v, rngSearch, 3, False)
v2 = Application.Vlookup(v, rngSearch, 4, False)
'IsError(vx) will be True if no match was found
c.EntireRow.Columns("C").Value = IIf(IsError(v1), "-", v1) ' "-" if no match
c.EntireRow.Columns("C").Value = IIf(IsError(v2), "-", v1)
End If
Next c
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411633.html
標籤:
下一篇:如何交叉表以獲取差異?
