在excel VBA VLookup中使用变量作为工作表名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19236200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:42:43 来源:igfitidea点击:
Using variable for sheet name in excel VBA VLookup
提问by Drexl
Private Sub UpdateBoxes()
Dim wsFunc As WorksheetFunction: Set wsFunc = Application.WorksheetFunction
Dim sheet As String
If Range("C1") = "some string" Then
sheet = "SomeSheet"
End If
Range("B6").Value = Sheets("Spreadsheet Ctrl").Range("B7") 'Sets title of Box 1 based on Spreadsheet Ctrl
Range("G6").Value = Sheets("Spreadsheet Ctrl").Range("C7") 'Sets title of Box 2 based on Spreadsheet Ctrl
Range("B11").Value = Sheets("Spreadsheet Ctrl").Range("D7") 'Sets title of Box 3 based on Spreadsheet Ctrl
Range("G11").Value = Sheets("Spreadsheet Ctrl").Range("E7") 'Sets title of Box 4 based on Spreadsheet Ctrl
Range("B16").Value = Sheets("Spreadsheet Ctrl").Range("F7") 'Sets title of Box 5 based on Spreadsheet Ctrl
Range("G16").Value = Sheets("Spreadsheet Ctrl").Range("G7") 'Sets title of Box 6 based on Spreadsheet Ctrl
Range("C7").Value = wsFunc.VLookup(B6,'" & sheet & '"!A1:G5,3,)" 'Vlookup for "Current Revision
End Sub
The variable "sheet" will eventually change based on a nested if. It should then be passed to the last line of code before End Sub
. I am getting a compile error that states "Expected: expression", and it highlights the first tick in '" & sheet & '"
.
变量“sheet”最终将根据嵌套的 if 发生变化。然后它应该被传递到之前的最后一行代码End Sub
。我收到一个编译错误,指出“预期:表达式”,它突出显示了'" & sheet & '"
.
回答by Tim Williams
Something like:
就像是:
Range("C7").Value = wsFunc.VLookup(ActiveSheet.Range("B6"), _
Sheets(sheet).Range("A1:G5"),3,False)