簡體   English   中英

Excel VBA在vlookup公式中使用單元格值

[英]Excel VBA Using cell value in vlookup formula

我正在嘗試創建一個與工作表中的單元格名稱同名的工作表的vlookup。

我產生以下想法,認為可以在vlookup公式中使用“ str”,但是出現“運行時錯誤” 1004”:應用程序定義的錯誤或對象定義的錯誤”

Sub copy()

    Dim LastRow As Long
    Dim str As String

    str = Cells(1, 5).Value

    With Sheets("Overview")
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

        With .Range("E2:E" & LastRow)
            .Formula = "=VLOOKUP(B2, & str &!B:F,5,FALSE)"
        End With

    End With

End Sub

誰能看到我在做什么錯?

您已經在VBA中定義了str ,但是在公式中引用了它而不關閉了引號,請嘗試以下操作:

Sub copy()

Dim LastRow As Long
Dim str As String

str = Cells(1, 5).Value

With Sheets("Overview")
    LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

    With .Range("E2:E" & LastRow)
        .Formula = "=VLOOKUP(B2," & str & "!B:F,5,FALSE)"
    End With

End With

End Sub

暫無
暫無

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

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