有没有办法将特定单元格存储到 VBA 范围内的新变量中?我的意思是...
假设我已将以下数据设置为范围调用“数字”。
现在在 VBA 中,对于每一行,我想提取每个单独的单元格值并将每个值分配给不同的变量。然后再次重复下一行。
我基本上想使用给定行中的值来做某事,然后让它再次重复下一行。
这有意义吗???
这就是我一直在玩的......但我不知道如何将给定行中的每个单元格分配给新变量
Public Sub try()
Dim rng As Range
Dim row As Range
Dim cell As Range
Dim n As Double
Set rng = Range("numbers")
For Each row In rng.Rows
For Each cell In row.Cells
n = cell.value
Next cell
Next row
End Sub
最佳答案
尝试这个:
Dim numberArray As Variant
' this line will assign numbers inside the range to an array
numberArray = Range("numbers").Value2
' now you are able to access all numbers in you range through this array, like this:
MsgBox numberArray(1, 1) 'it will show 1
您现在的做法没有意义,因为您将所有值分配给一个变量
n
,因此在循环的每次迭代中,先前的值都会被覆盖,从而导致 n
具有范围内的最后一个值,即 3
.
关于excel - 将范围中的特定单元格分配给变量 VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53534039/