2015-11-19 15 views
0
Private Sub CommandButton3_Click() 

Dim i As Integer, n As Integer, inc As Integer, results() As Integer 
Dim text As String 

n = 100 

inc = InputBox("Please enter increment") 

ReDim results(0 To n) As Integer 

For i = 1 To n Step 1 

results(i) = inc + i 

text = results(i) 

Next i 

MsgBox (text) 

End Sub 
+0

實際上,我試圖將它全部顯示在一條消息中,如「1,2,3,4,5,6 .....」 – codenoob

+1

更改text = ... line to Text = Text&「,」&results(i)' –

+0

幫助,謝謝! – codenoob

回答

1

試試這個:

Private Sub CommandButton3_Click() 

Dim i As Integer, n As Integer, inc As Integer, results() As Integer 
Dim text As String 

n = 100 

inc = InputBox("Please enter increment") 

ReDim results(0 To n) As Integer 

For i = 1 To n Step 1 

results(i) = inc + i 

Next i 

text = Join(results, ",") 
MsgBox text 

End Sub 

Join()功能將使用給定的分隔符加入一個一維數組,並返回一個字符串。

+0

感謝您的輸入! – codenoob

+0

如果它的工作 - 不要忘記標記爲答案左邊的綠色刻度:) –

+1

是的,明白了。仍然是新的網站和學習所有的規則。感謝您的提示:) – codenoob

1

據我瞭解,增量是一個遞增 - 連續值之間的差異。因此,代碼應該是:

Private Sub CommandButton3_Click() 

Dim i As Integer, n As Integer, inc As Integer, results() As Integer 
Dim text As String 

n = 100 
inc = InputBox("Please enter increment") 
ReDim results(1 To n) As Integer ' not: (0 to n) 

For i = 1 To n Step inc 
    results(i) = i 
Next i 
text = Join(results, ",") 
MsgBox text 
End Sub 
+0

明白了,謝謝! – codenoob

+0

_theoretically_它應該是'0到n-1',因爲默認情況下VBA使用從零開始的索引。但無論哪種方式工作得很好。 –

相關問題