0
對於我的一個項目我試圖在VBA中產生一個宏GOTO語句的目的是重複Do while循環,直到它被迫停止。每次我嘗試使用GOTO語句來重複一個循環時,我的宏無法編譯
每次我嘗試運行應用程序死機的程序VBA並沒有錯誤似乎是顯而易見的任何人可以說明爲什麼這可能發生,如果有任何修復
Sub buses()
test.Caption = Day
Dim mytime As String 'intiger showing current time
mytime = "string" ' testing perpus only
Dim counter As Integer 'intiger value for counter
counter = 1 'counter set at 0
Dim record As Integer 'intiger value for record
Jump
Do While counter < 20 'repeat if statment untill counter reaches 20
counter = counter + 1 ' upon repeating counter has increased by 1
If mytime = CStr(ThisWorkbook.Sheets("mon A").Range("A" & counter).Value) Then
'if current time = a time of due to arive bus then
record = counter 'record value and counter value are the same
V1F1.Caption = CStr(ThisWorkbook.Sheets(Day).Range("A" & record).Value) 'display record information on screen
V1F2.Caption = CStr(ThisWorkbook.Sheets(Day).Range("B" & record).Value)
V1F3.Caption = CStr(ThisWorkbook.Sheets(Day).Range("C" & record).Value)
record = record + 1
V2F1.Caption = CStr(ThisWorkbook.Sheets(Day).Range("A" & record).Value) 'display record information on screen
V2F2.Caption = CStr(ThisWorkbook.Sheets(Day).Range("B" & record).Value)
V2F3.Caption = CStr(ThisWorkbook.Sheets(Day).Range("C" & record).Value)
record = record + 1
V3F1.Caption = CStr(ThisWorkbook.Sheets(Day).Range("A" & record).Value) 'display record information on screen
V3F2.Caption = CStr(ThisWorkbook.Sheets(Day).Range("B" & record).Value)
V3F3.Caption = CStr(ThisWorkbook.Sheets(Day).Range("C" & record).Value)
Exit Do 'break do
End If 'end if statment
Application.Wait DateAdd("s", 1, Now) 'wait 1 second
Loop ' loop as a result of if stament not being executed
GoTo Jump: 'repeat Do while
End Sub
您的代碼中可能存在拼寫錯誤。標籤後面應緊跟一個「:」。它是跳轉:不跳轉。然後:在執行Jump之後,你的計數器是20!所以while循環將不會被執行。它只執行一次,然後在Jump和Goto Jump之間有一個無限循環。將線設置計數器上方的跳轉標記移動到1。 –