2011-06-17 269 views
0

我有以下問題。我有一個帶有輸入字段的用戶窗體。用戶將輸入參與者的號碼。我有四組參加者:Excel VBA中填充陣列

 
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 
+0

你能後失敗的代碼? – jonsca 2011-06-17 08:47:34

回答

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 

現在回答你的問題:有沒有更好的方式來做到這一點?是。但是,這工作正常,儘管它不漂亮,但它在任何方面都不是低效的。

+0

Thx。我用這個和thx來評論。 – user366121 2011-06-29 07:12:28

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 

+0

嗯也會試試這個。 – user366121 2011-06-17 09:19:07

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)) 
+0

我試過你的代碼,但當我嘗試通過數組來查看輸出時出現類型不匹配錯誤。 – user366121 2011-06-17 09:17:24

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