簡體   English   中英

(VBA)嘗試制作具有4行n列的數組

[英](VBA) Trying to make an array with 4 rows and n columns

Sub abcd()

Dim numcomps As Integer
numcomps = Range("B3").Value
Dim constants() As Integer
ReDim constants(1 To 4, 1 To numcomps)
Dim i As Integer, j As Integer, k As Integer, compnum As Integer

For k = 1 To numcomps
compnum = Cells(k, 7).Value
   For i = 1 To 4
   constants(i, k) = Cells((compnum + 6), (i + 2)).Value

   Next i
Next k

MsgBox (constants(1, 1))
MsgBox (constants(2, 1))
MsgBox (constants(3, 1))


End Sub

基本上,我試圖制作一個包含4行和n列的數組,其中n由用戶指定為numcomps。 每列將包含從電子表格中的一個表格中收集的4個常量。 每次循環時,該代碼似乎都在重寫數組中的值,而不是創建新列。 我需要添加/更改什么?

嘗試此自定義從何處開始收集數據

Sub MultiDArray()

    ' Define variables
    Dim arrayColumns As Integer
    Dim arrayRows As Integer
    Dim constants() As Integer
    Dim initialRow As Integer
    Dim initialColumn As Integer
    Dim rowCounter As Integer
    Dim columnCounter As Integer

    ' Define how many rows
    arrayRows = 4

    ' Define how many columns
    arrayColumns = Range("B3").Value

    ' Define row to begin gathering data
    initialRow = 7

    ' Define column to begin gathering data
    initialColumn = 2

    ' Redimension array to number of rows and columns
    ReDim constants(1 To arrayRows, 1 To arrayColumns)


    ' Loop through each row
    For rowCounter = 1 To arrayRows

        ' Loop through each column
        For columnCounter = 1 To arrayColumns

            ' Gather data into array
            constants(rowCounter, columnCounter) = Cells(rowCounter + initialRow, columnCounter + initialColumn)

            ' For debugging purposes
            Debug.Print "Cell: " & Cells(rowCounter + initialRow, columnCounter + initialColumn).Address, "Row: " & rowCounter, "Column: " & columnCounter, "Value:" & constants(rowCounter, columnCounter)

        Next columnCounter

    Next rowCounter

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM