我对选择循环的情况有疑问。 第一次循环工作完全正常,一直到案例18.然而,每当我尝试通过键入“Y”重新启动循环时,它会正常重启循环,但每当我尝试进入时都会弹出错误消息框中的值。
我很困惑为什么代码第一次工作但不是第二次。
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
答案 0 :(得分:2)
您未在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)
答案 1 :(得分:1)
在询问Y / N而不是输入框时,您可以使用消息框:
If MsgBox("Do You Want To Input Additional Information", vbYesNo) = vbYes then
......
End if