VBA 循环问题?
sheet2单元格K2到K7的值如下:450302199908093390483197701230120420403194301112210450302199908092...
sheet2 单元格 K2 到 K7 的值如下:
450302199908093
390483197701230120
420403194301112210
450302199908092001
450302199908092009
需要判断完性别后将性别显示在L2到L7
代码如下:
Sub ID()
Dim i As Integer, L As Integer, J As Integer
For i = 2 To 7
L = Len(Sheets(2).Cells(i, 11))
If L <> 15 And L <> 18 Then
MsgBox "please check your input" & " " & Sheets(2).Cells(i, 11).Address
Sheets(2).Cells(i, 11) = InputBox("PLEAse reput your ID", "reminder")
ElseIf L = 18 Then
J = Mid(Sheets(2).Cells(i, 11), 17, 1)
Else
J = Right(Sheets(2).Cells(i, 11), 1)
End If
If J Mod 2 = 0 Then
Sheets(2).Cells(i, 12) = "女"
Else
Sheets(2).Cells(i, 12) = "男"
End If
Next
End Sub
问题:当K2到K7单元格的字符长度不等于15或18时,需要重新输入,这个程序可以输入,但是当输入的字符长度仍然不等于15或18时,程序还是能继续运行,如何连续检查直到输入的字符长度是符合要求的? 展开
450302199908093
390483197701230120
420403194301112210
450302199908092001
450302199908092009
需要判断完性别后将性别显示在L2到L7
代码如下:
Sub ID()
Dim i As Integer, L As Integer, J As Integer
For i = 2 To 7
L = Len(Sheets(2).Cells(i, 11))
If L <> 15 And L <> 18 Then
MsgBox "please check your input" & " " & Sheets(2).Cells(i, 11).Address
Sheets(2).Cells(i, 11) = InputBox("PLEAse reput your ID", "reminder")
ElseIf L = 18 Then
J = Mid(Sheets(2).Cells(i, 11), 17, 1)
Else
J = Right(Sheets(2).Cells(i, 11), 1)
End If
If J Mod 2 = 0 Then
Sheets(2).Cells(i, 12) = "女"
Else
Sheets(2).Cells(i, 12) = "男"
End If
Next
End Sub
问题:当K2到K7单元格的字符长度不等于15或18时,需要重新输入,这个程序可以输入,但是当输入的字符长度仍然不等于15或18时,程序还是能继续运行,如何连续检查直到输入的字符长度是符合要求的? 展开
1个回答
展开全部
MsgBox "please check your input" & " " & Sheets(2).Cells(i, 11).Address
Sheets(2).Cells(i, 11) = InputBox("PLEAse reput your ID", "reminder")
i=i-1
Sheets(2).Cells(i, 11) = InputBox("PLEAse reput your ID", "reminder")
i=i-1
追问
能解释下吗
追答
输入后,i=i-1,循环又回到你输入时单元格处理,如果不合适,又要输入了,直到输入合适才可以才不运行这步。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询