Excel strLookup帮助(动态工作表值)

时间:2014-10-09 05:14:36

标签: excel excel-vba vlookup vba

美好的一天 我试图使用strLookup从不同的工作簿运行vlookup。 如果工作簿和工作表名称是静态的,它可以很好地工作;但是我需要从我之前在宏中定义的字符串插入动态工作表名称,它不起作用

我尝试了下面的

本作品:

strLookup = "'C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\1- Macro AR reporting\Delta\[test.xlsx]40'!A:B"


这些不起作用(尝试将工作表名称" 40"更改为之前定义的动态值(DailyWeek -1)

strLookup = "'C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\1- Macro AR reporting\Delta\[test.xlsx](Dailyweek -1)'!A:B"

strLookup = "'C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\1- Macro AR reporting\Delta\[test.xlsx]Dailyweek -1'!A:B"

有什么想法吗? 感谢

我的全部代码如下:

Dailyweek = Workbooks("AR Reporting Tool.xlsm").Worksheets("Reports").Range("G30")

Dim sh As Worksheet, flg As Boolean
For Each sh In Worksheets

If sh.Name Like (Dailyweek - 1) Then flg = True: Exit For
Next

If flg = True Then
    'MsgBox "Found!"
    wbk4.Sheets("UAE").Activate
    With wbk4.Sheets("UAE")
    Set clDest = .Range("N2")
    Set clLookup = .Range("A2")
    strLookup = "'C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\1- Macro AR reporting\Delta\[UAE FM.xlsx](Dailyweek -1)'!A:B"
    End With

    If clLookup.Offset(1, 0) <> vbNullString Then
    rws = Range(clLookup, clLookup.End(xlDown)).Rows.Count
    Set clDest = clDest.Resize(rws, 1)
    End If
    clDest.Formula = "=IFERROR(VLOOKUP(" & clLookup.Address(False, False) & "," & strLookup & ",2,0),0)"
    clDest.Value = clDest.Value ' Convert to value

else
  msgbox "Doesnt exist"
end if

1 个答案:

答案 0 :(得分:1)

你只需要正确构建你的字符串 这样的事可能呢?

strLookup = "'C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File" _
    & "\AR Working File\1- Macro AR reporting\Delta\[test.xlsx]" _
    & (Dailyweek -1) & "'!A:B"