我有点新鲜。我如何获取列并将单元格数据作为整数并遍历该范围内的所有值以将其放入函数以将结果输出到excel工作簿中的另一列。所以我的输出列将是整个Comm列,使用G,J和K列输入函数=100000*slotNumber+300*xpos+ypos
A B C D E F G H I J K
1 Proc Equip Operat Shift Comm Casette SlotNumber Diam Measure XPos YPos
2
3'
所以想一想,如果我拿出每个的值并制作了一个for循环,我可以接受这些值并以某种方式完成所有这些,只是不确定如何!拜托,谢谢!
编辑:我已经存储了所有列,现在我必须逐个将Array值传递给函数,用于公式Z = 100000*slotArr(i)+300xList(i)+yList(i)
或者我可以将它放在for循环中。
编辑:将函数放在循环中...我在函数行中得到一个超出范围错误的对象。
Sub cmdMeans_Click()
Dim i As Long, j As Long
Dim slotList As Range, slotArr() As Variant, xList As Range, xArr() As Variant
Dim yList As Range, yArr() As Variant, cArr() As Variant
Set slotList = Range("P2", Range("P2").End(xlDown))
slotArr() = slotList.Value
Set xList = slotList.Offset(0, 4)
xArr() = xList.Value
Set yList = slotList.Offset(0, 5)
yArr() = yList.Value
'Only one counter required because of the dependancy on the range slotList
For i = 2 To UBound(slotArr, 1)
'Dimensioning Array
ReDim cArr(UBound(slotArr, 1), 1)
cArr(i, 1) = (100000 * slotArr(i, 1)) + (300 * xList(i, 1)) + yList(i, 1)
'MsgBox ("Comment Cell Value" & cArr(i, 1))
Next
'Resizing Array
ReDim Preserve cArr(i)
'This is where the new values will be written to the comment column
Dim cRng As Range
Set cRng = Range(Cells(14, 1), Cells(UBound(cArr(i))))
cRng.Value = Application.Transpose(cArr)
End Sub
答案 0 :(得分:2)
我很担心看你的样本 - appolgy但实际上不是 decipherable
...所以我坚持你的问题标题和评论:
的 VBA Excel Store Range as Array, extract cell values for formula. Offset for other variables
强> 的
如何将范围存储为数组: -
Dim vArray as Variant
vArray = Sheets(1).Range("A2:G50").Value)
如何将数组传递给一个以数组作为参数并返回数组的函数: -
Function passArray(ByRef vA as Variant) as Variant
Dim myProcessedArray as Variant
'----your code goes here
passArray = myProcessedArray
End Function
将单维数组输出到工作表范围: -
Sheets(1).Range("E2").Resize(1, _
UBound(Application.Transpose(singleDArray))) = singleDArray
将多维数组输出到工作表范围: -
Sheets(1).Range("E2").Resize(UBound(multiDArray) + 1, _
UBound(Application.Transpose(multiDArray))) = multiDArray