我撰寫了代碼以在訊息框中顯示字串 ( st1, st2, st3, ...) 中的文本,因為它在For回圈中進行,但它只顯示為數字。
我希望波紋管代碼txt : Test A在其第一個For回圈中輸出。但它給了我txt : 1. 因此,在整個回圈中,它只會顯示回圈編號,而不是字串中的文本。請幫忙。
Sub OutputTxt()
'States of the prosessing----------------------------
Dim stat As String
'string values - Discriptions
Dim st1 As String
Dim st2 As String
Dim st3 As String
Dim st4 As String
Dim st5 As String
Dim st6 As String
Dim st7 As String
Dim st8 As String
st1 = "Test A"
st2 = "Test B"
st3 = "Test C"
st4 = "Test D"
st5 = "Test E"
st6 = "Test F"
st7 = "Test G"
st8 = "Test H"
'-----------------------------------------------------------
Dim Counter As Long
Dim TotalCount As Long
'Initialize the Variables and Objects
TotalCount = 8
For Counter = 1 To TotalCount
stat = st & Counter
'Update the msg box
MsgBox ("Txt : " & stat)
Next Counter
End Sub
uj5u.com熱心網友回復:
每當您覺得需要在變數名中使用數字時:您做錯了!改用陣列。
您不能遍歷名為 like st1to 的變數,st8但可以遍歷陣列:
Option Explicit
Public Sub OutputTxt()
'string values - Discriptions
Dim st(1 to 8) As String
st(1) = "Test A"
st(2) = "Test B"
st(3) = "Test C"
st(4) = "Test D"
st(5) = "Test E"
st(6) = "Test F"
st(7) = "Test G"
st(8) = "Test H"
Dim Counter As Long
For Counter = LBound(st) To UBound(st) 'loop through entire array
'Update the msg box
MsgBox ("Txt : " & st(Counter))
Next Counter
End Sub
uj5u.com熱心網友回復:
For...Next回圈中的 MsgBox
Chr function
Option Explicit
Sub OutputTxt()
Const ProcTitle As String = "Output Text"
Const MsgLeft As String = "Txt : "
Const MsgMiddle As String = "Test "
Const Chr0 As Long = 64 ' '65' is 'A', '90' is 'Z'
Const TotalCount As Long = 8 ' '8' is 'H', max is '26' is 'Z'
Dim Counter As Long
Dim Msg As String
Dim MsgRight As String
For Counter = 1 To TotalCount
MsgRight = ChrW(Chr0 Counter)
Msg = MsgLeft & MsgMiddle & MsgRight
MsgBox Msg, vbInformation, ProcTitle
Next Counter
End Sub
Sub OutputTxtShort()
Const TotalCount As Long = 8 ' '8' is 'H', max is '26' is 'Z'
Dim Counter As Long
For Counter = 1 To TotalCount
MsgBox "Txt : Test " & ChrW(64 Counter)
Next Counter
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/346668.html
下一篇:我如何使用同一張表中的類似潛艇?
