[英]How to fix a 'Next with no For' error sent in excel vba
我正在設置一個自動日歷,當varDate()中的相應日期出現在日歷中(Fday是日歷日期)時,該日歷應從varName()中繪制一個名稱。 我收到令人困惑的循環中的“下一個沒有For”錯誤。
目的是為name(varName)和date(varName)創建一個動態數組,在其中可以將名稱繪制為具有相應日期的日歷日。 我已經成功使用靜態數組根據相應的日期將名稱繪制到日歷中,但是當我使用動態數組時,這給我帶來了麻煩。
stRow = Row with calendar date
stCol = Column with calendar date
nameRow = Row containing persons name (below calendar date)
For i = LBound(varDate) To UBound(varDate)
If Cells(stRow + 1, stCol) = Empty Then
nameRow = stRow + 1
If Fday = varDate(i) Then
csheet.Cells(nameRow, stCol) = varName(i)
End If
Else
nameRow = nameRow + 1
If Fday = varDate(i + 1) Then
csheet.Cells(nameRow, stCol) = varName(i + 1)
End If
Next i
您應該縮進代碼...這樣可以避免出現此錯誤:
stRow = Row with calendar date
stCol = Column with calendar date
nameRow = Row containing persons name (below calendar date)
For i = LBound(varDate) To UBound(varDate)
If Cells(stRow + 1, stCol) = Empty Then
nameRow = stRow + 1
If Fday = varDate(i) Then
csheet.Cells(nameRow, stCol) = varName(i)
End If
Else
nameRow = nameRow + 1
If Fday = varDate(i + 1) Then
csheet.Cells(nameRow, stCol) = varName(i + 1)
End If 'you missed this one
End If
Next i
每當我遇到此錯誤時,這意味着我實際上在某處缺少“ End If”。 Excel認為Next語句在IF塊內,因為它沒有找到End If,但是在該IF塊中沒有For!
因此,這就是您得到錯誤的原因。
達米安(Damian)已找到丟失的“如果結束(End If)”應該放到哪里,然后將其放回去。他是對的,正確的縮進使得這些錯誤非常容易發現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.