对于循环个案select错误

我有一个关于select循环的情况的问题。 这个循环第一次工作的非常好,一直到case 18.但是,每当我尝试通过键入“Y”来重新启动循环,它将正常地重新启动循环,但是每当我尝试input时都会popup一个错误一个值放入消息框。

我很困惑,为什么代码第一次运行,但不是第二次。

Sub MessageBoxFunction() Dim wbThis As Workbook Dim wsThis As Worksheet Dim msgBox As Variant Dim txt1, txt2 As String Dim rng1, rng2 As Range Dim i, iA, iB As Integer Set rng1 = Range("A1") Set rng2 = Range("A1") txt2 = rng1.Value Do Until IsEmpty(rng1) Set rng1 = rng1.Offset(1, 0) Loop For i = 1 To 18 Select Case i Case 1 msgBox = InputBox("Do You Want To Input Information? (Y/N)") txt1 = CStr(msgBox) If InStr(1, msgBox, "N", 1) > 0 Then i = 17 End If Case 2 To 17 msgBox = InputBox(txt2) txt1 = CStr(msgBox) iA = i - 2 iB = i - 1 rng1.Offset(0, iA).Value = txt1 txt2 = rng2.Offset(0, iB).Value Case 18 msgBox = InputBox("Do You Want To Input Additional Information (Y/N)") txt1 = CStr(msgBox) If InStr(1, msgBox, "Y", 1) > 0 Then rng1 = rng1.Offset(1, 0) txt2 = rng2.Value i = 1 End If End Select Next i End Sub 

case 18你不在这一行设置范围

 rng1 = rng1.Offset(1, 0) 

这应该适合你。

 Sub MessageBoxFunction() Dim wbThis As Workbook Dim wsThis As Worksheet Dim msgBox As Variant Dim txt1, txt2 As String Dim rng1 as Range, rng2 As Range Dim i, iA, iB As Integer Set rng1 = Range("A1") Set rng2 = Range("A1") txt2 = rng1.Value Do Until IsEmpty(rng1) Set rng1 = rng1.Offset(1, 0) Loop For i = 1 To 18 Select Case i Case 1 msgBox = InputBox("Do You Want To Input Information? (Y/N)") txt1 = CStr(msgBox) If InStr(1, msgBox, "N", 1) > 0 Then i = 17 End If Case 2 To 17 msgBox = InputBox(txt2) txt1 = CStr(msgBox) iA = i - 2 iB = i - 1 rng1.Offset(0, iA).Value = txt1 txt2 = rng2.Offset(0, iB).Value Case 18 msgBox = InputBox("Do You Want To Input Additional Information (Y/N)") txt1 = CStr(msgBox) If InStr(1, msgBox, "Y", 1) > 0 Then Set rng1 = rng1.Offset(1, 0) txt2 = rng2.Value i = 1 End If End Select Next i End Sub 

应该是这个。

 Dim rng1 as Range Set rng1 = rng1.Offset(1, 0) 

询问Y / N而不是input框时,可以使用消息框:

 If MsgBox("Do You Want To Input Additional Information", vbYesNo) = vbYes then ...... End if