excel vba填充数组

时间:2011-06-17 08:40:50

标签: arrays excel vba fill

我有以下问题。我有一个带有输入字段的用户表单。用户将为参与者输入一个号码。我有四组参与者:

Group A: 5
Group B: 6
Group C: 1
Group D: 2

每个参与者应该这样命名:{GA1,GA2,...,GD2}我想按顺序将其写入数组,然后使用此数组用名称填充单元格,但我想出的只是有四个for循环将它写入数组并且也失败了。有更好的方法吗?

Dim GA As Integer
Dim GB As Integer
Dim GC As Integer
Dim GD As Integer
Dim PartSum As Integer


GA = TextBox32.Value
GB = TextBox33.Value
GC = TextBox34.Value
GD = TextBox35.Value

PartSum = GA + GB + GC + GD

Dim NamingArray() As String

ReDim NamingArray(1 To PartSum)

For i = 0 To GA
    NamingArray(i) = "GA " & CStr(i)
Next i

For j = GA To GA + GB
    NamingArray(i) = "GB " & CStr(j)
Next j

For k = GA + GB To GA + GB + GC
    NamingArray(i) = "GC " & CStr(k)
Next k

For l = GA + GB + GC To GA + GB + GC + GD
    NamingArray(i) = "GD " & CStr(l)
Next l

'check entries
For i = LBound(NamingArray) To UBound(NamingArray)

    MsgBox (NamingArray(i))

Next i

4 个答案:

答案 0 :(得分:3)

我可以看到为什么您的代码行为不像您期望的三个原因。

首先,您在For ... Next循环中用作索引的变量是不一致的。例如,在此循环中,您递增k,但使用i来索引NamingArray。请注意,i仍然在第一个循环中遗留了值GA+1

For k = GA + GB To GA + GB + GC
    NamingArray(i) = "GC " & CStr(k)
Next k

只需使用i即可完成所有循环。每次都不需要使用不同的字母。

其次,您尝试访问不存在的NamingArray的元素0。

ReDim NamingArray(1 To PartSum) ' starts at 1

For i = 0 To GA 
    NamingArray(i) = "GA " & CStr(i) ' attempt to refer to NamingArray(0)
Next i

第三,你的索引完全搞砸了。例如,NamingArray(GA)将在第一个循环结束时写入,然后在第二个循环开始时被覆盖。所有循环都会发生这种情况;他们的“管辖权”重叠(抱歉,我是加拿大人)。我已经在下面更正了这个(以及所有其他错误)。这有效:

For i = 1 To GA
    NamingArray(i) = "GA " & CStr(i)
Next i

For i = 1 + GA To GA + GB
    NamingArray(i) = "GB " & CStr(i - GA)
Next i

For i = 1 + GA + GB To GA + GB + GC
    NamingArray(i) = "GC " & CStr(i - GA - GB)
Next i

For i = 1 + GA + GB + GC To GA + GB + GC + GD
    NamingArray(i) = "GD " & CStr(i - GA - GB - GC)
Next i

现在回答你的问题:有更好的方法吗?是。但这种方法很好,虽然它不漂亮,但它在任何方面都不是低效的。

答案 1 :(得分:0)

为什么要打扰一个阵列?将int声明为游标

假设你想要他们跨行1

dim col as integer
dim Acount as integer (etc)
'get Acount, Bcount etc from form
for col 1 =1 to Acount
    cells(1,col).value = "GA" & col
next
for col =1 Acount to Bcount-1
    cells(1,col).value = "GB" & col
next

答案 2 :(得分:0)

如果列表不会改变,那么你的数组可能只是声明如下:

Dim participantNames
participantNames = Array("GA1","GA2","GA3","GA4","GA5","GB1","GB2","GB3","GB4","GB5","GB6","GC1","GD1","GD2")

如果字母和计数列表可能有所不同,那么您可能需要一个函数,例如:

Option Explicit

Public Function GroupIDs(grpNames, grpCounts) As Variant

Dim grpIndex
Dim countIndex
Dim output As New Collection

    For grpIndex = LBound(grpNames) To UBound(grpNames)
        For countIndex = 1 To grpCounts(grpIndex)
            output.Add "G" & grpNames(grpIndex) & countIndex
        Next
    Next

ReDim outputArray(1 To output.Count)

    For countIndex = 1 To output.Count
        outputArray(countIndex) = output(countIndex)
    Next

    GroupIDs = outputArray

End Function

...您可以致电:

GroupIds(Array("A", "B", "C", "D"),Array(5, 6, 1, 2))

答案 3 :(得分:0)

将文本框命名为tbxGA,tbxGB,tbxGC和tbxGD,并使用此代码写入范围。

Private Sub cmdWrite_Click()

    Dim i As Long, j As Long
    Dim ctl As Control
    Dim lCnt As Long
    Dim aOutput() As String
    Dim lTotal As Long

    For i = 65 To 68
        Set ctl = Me.Controls("tbxG" & Chr$(i))
        lTotal = lTotal + Val(ctl.Text)
    Next i

    ReDim aOutput(1 To lTotal, 1 To 1)

    For i = 65 To 68
        Set ctl = Me.Controls("tbxG" & Chr$(i))
        For j = 1 To Val(ctl.Text)
            lCnt = lCnt + 1
            aOutput(lCnt, 1) = "G" & Chr$(i) & j
        Next j
    Next i

    Sheet1.Range("A1").Resize(UBound(aOutput, 1), UBound(aOutput, 2)).Value = aOutput

    Unload Me

End Sub