Excel VBA - 将行分配给变量并对其进行操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22573305/
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
Excel VBA - Assign row to variable and manipulate it?
提问by ShrimpCrackers
I've been searching how to assign a row to a variable and manipulate cells through the variable but I can't seem to find how to do this.
我一直在寻找如何将一行分配给变量并通过变量操作单元格,但我似乎无法找到如何执行此操作。
x = Sheet5.Range("A1").EntireRow
MsgBox x(1, 1)
The above code will get me the row into 'x', but is there any way that I can change a cells value using the variable 'x'? x(1,1) = "foo" will not work, and since it's not an object I can't access .Value.
上面的代码将使我将行转换为“x”,但是有什么方法可以使用变量“x”更改单元格值?x(1,1) = "foo" 不起作用,因为它不是一个对象,我无法访问 .Value。
回答by Doug Glancy
Here's some sample code:
这是一些示例代码:
Sub Ranging()
Dim rng As Excel.Range
Dim ws As Excel.Worksheet
Set ws = ActiveSheet
Set rng = ws.Range("A1").EntireRow
With rng
Debug.Print .Cells(1).Value
Debug.Print .Cells(5).Address
.Cells(43).Value = "SurfN'Turf"
End With
End Sub
Debug.Print
prints to the VBE's Immediate Window (access with Ctrl-G)
Debug.Print
打印到 VBE 的立即窗口(使用Ctrl-访问G)