vba 如何将字符串转换为工作簿名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19134518/
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
How to convert string to workbook name
提问by srt
Sub openwb()
Dim x260path As String
x260path = "E:\sarath\PTMetrics131002\D8 L538-L550 16MY\D8 L538-L550_16MY_Powertrain Metrics_" & Format(Date, "YYYYMMDD") - 1
Workbooks("x260path").Activate
ActiveWorkbook.SaveAs ["E:\sarath\PTMetrics131002\D8 L538-L550 16MY\D8 L538-L550_16MY_Powertrain Metrics_" & Format(Date, "YYYYMMDD")]
Debug.Print x260path
End Sub
Here, when i execute, an error says "subscript out of Range". And it appears on 4th line.when i use 'workbook' to declare 'x260path' instead of string, It shows another error saying "Object variable or with block variable not set" on line 3. Can u help?Why is this happening?
在这里,当我执行时,错误提示“下标超出范围”。它出现在第 4 行。当我使用“工作簿”声明“x260path”而不是字符串时,它在第 3 行显示另一个错误,提示“对象变量或块变量未设置”。你能帮忙吗?为什么会发生这种情况?
采纳答案by Kazimierz Jawor
To activate workbook you need to set reference to workbook's name, not workbook's full path & name. Therefore, out of you x260path
variable you need to get only name. There are few possible ways to do so. This is one I've just have in mind:
要激活工作簿,您需要设置对工作簿名称的引用,而不是工作簿的完整路径和名称。因此,从x260path
变量中您只需要获取名称。有几种可能的方法可以做到这一点。这是我刚刚想到的一个:
Dim x260path As String
x260path = "E:\sarath\PTMetrics131002\D8 L538-L550 16MY\D8 L538-L550_16MY_Powertrain Metrics_" & Format(Date, "YYYYMMDD") - 1
Dim x260name As String
x260name = Split(x260path, "\")(UBound(Split(x260path, "\")))
Workbooks(x260name).Activate
'the rest of your code here