在 VBA 中将活动单元格值设置为变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28241443/
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
Setting an Active Cell Value to a variable in VBA
提问by SASUSMC
I am trying to run a macro that will open a workbook, complete a series of procedures on it, save it, Close it. Then in the Macro workbook it will move down one row and use that cells value as the filename to open the next workbook.
我正在尝试运行一个宏,它将打开一个工作簿,在它上面完成一系列程序,保存它,关闭它。然后在宏工作簿中,它将向下移动一行并使用该单元格值作为文件名打开下一个工作簿。
The issue I am having is how do I get VBA to store a cells value as a variable:
我遇到的问题是如何让 VBA 将单元格值存储为变量:
The basics of the macro would look like this:
宏的基本原理如下所示:
Dim Num as Long
Num = ActiveSheet.UsedRange.Rows.Count
Dim Name as String
Name = ?
Workbooks.Open Filename:="N:\PricingAudit\FY16 Price Increase\Raw DBF Files\TreatmentFiles\" + Name
...
...
Workbooks(Name).Close
...
Any help would be greatly. Appreciated in this:
任何帮助都会很大。在此赞赏:
Thanks in advance
提前致谢
采纳答案by Gary's Student
Say in the worksheet we have:
在工作表中说我们有:
Then here is one method:
那么这是一种方法:
Sub Dural()
Dim MyName As String
MyName = Range("A1").Text
Workbooks.Open Filename:="C:\TestFolder\" & MyName
Set wbfirst = ActiveWorkbook
'.......
'.......
'.......
wbfirst.Close
End Sub
NOTE:
笔记:
- the string variable does not replicate a known property (Name)
- we closeusing an Object rather than "by name"
- we use &rather than +
- 字符串变量不复制已知属性(名称)
- 我们使用对象而不是“按名称”关闭
- 我们使用&而不是+