0

我对此有点陌生。我将如何获取列并将其单元格数据放入整数并遍历该范围内的所有值以将其放入函数中以将结果输出到 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 循环,我可以获取这些值并以某种方式完成所有这些,只是不知道如何!谢谢,麻烦您了!

编辑:我存储了所有列,现在我必须将数组值一一传递给函数,对于公式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
4

1 回答 1

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
于 2013-01-31T13:57:37.083 回答