读取单元格并使用文本作为 VBA 公式的输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28532723/
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
Read cell and use text as input for VBA formula
提问by Josh
I've been searching for a long time now to find a simple solution for my problem but I just can't find it. My problem is that I want to reference a cell and use it as text in my VBA code.
我已经搜索了很长时间来为我的问题找到一个简单的解决方案,但我就是找不到。我的问题是我想引用一个单元格并将其用作 VBA 代码中的文本。
eg: say the cell = C104
例如:说单元格 = C104
VBA would do:
VBA 会这样做:
wb2.Sheets("Cell").Range("F8:F22").Copy
wb1.Sheets("asd").Range("B7:B21").PasteSpecial
This doesn't work because it isn't actually using the text from the cell. I've tried using cell().Text
and .value
but they don't work.
这不起作用,因为它实际上并未使用单元格中的文本。我试过使用cell().Text
,.value
但它们不起作用。
采纳答案by Josh
You have multiple workbooks and multiple worksheets available that could potentially be the parent of cell. You need to reference the cell definitively and take its .Value
or .Text
to be used as a worksheet name without relying on Activesheetfor a default parent.
您有多个工作簿和多个工作表可用,它们可能是cell的父级。您需要明确地引用单元格并将其.Value
或.Text
用作工作表名称,而不依赖于默认父级的Activesheet。
dim myWS as string
myWS = wb1.Sheets("asd").Range("C104").value '? this gets the value of cell. I don't know if I have referenced the correct workbook and worksheet
wb2.Sheets(myWS).Range("F8:F22").Copy _
destination:=wb1.Sheets("asd").Range("B7")