我编写了一个包含case select和msgbox的语法。我希望语法在电子表格中找到第一个活动单元格,并根据一些预定的问题和答案将其移动到A1或A2,但命令不能最佳地工作。能帮帮我吗?
我希望根据前面提到的问题的答案直观地弹出消息框,但是在消息框中以循环结束的方式似乎有一个不幸事件。
我附上了代码。
Public Sub SurvAnalysis()
Dim InpSh As Worksheet
Dim fCell As Range
Dim msg1 As String, msg2 As String
Dim Ans1 As Variant, Ans2 As Variant
Set InpSh = ThisWorkbook.Worksheets("Input")
msg1 = "Are these headers included in the Data, and is the data in the correct format? { Dob ∏ StartDate ∏ End Date }"
Ans1 = MsgBox(msg1, vbYesNoCancel, " Data type")
Select Case Ans1
Case vbYes
On Error Resume Next
Set fCell = InpSh.Cells.Find(What:="*", _
After:=InpSh.Cells(Rows.Count, Columns.Count), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
On Error GoTo 0
If fCell Is Nothing Then
MsgBox "All cells are blank."
Else
fCell.CurrentRegion.Cut Destination:=InpSh.Range("A1")
End If
GoTo Quit:
Case vbCancel
MsgBox ("Get your data sorted out")
GoTo Quit:
Case vbNo
GoTo Option2:
End Select
Quit:
Option2:
msg2 = "Are the data in the correct manner and do you wish for us to include the headers on your behalf?"
Ans = MsgBox(msg2, vbYesNo, "Sort Data")
Select Case Ans
Case vbYes
Set fCell = InpSh.Cells.Find(What:="*", _
After:=InpSh.Cells(Rows.Count, Columns.Count), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If fCell Is Nothing Then
MsgBox "All cells are blank."
Else
fCell.CurrentRegion.Cut Destination:=InpSh.Range("A2")
InpSh.Range("A1").Value = " Dob"
InpSh.Range("B1").Value = " StartDate"
InpSh.Range("C1").Value = " End Date"
End If
Case vbNo
MsgBox ("Get your data sorted out")
GoTo Quit:
End Select
End Sub
答案 0 :(得分:3)
你的转到结尾语句Quit:
应该在最后。在代码转到Quit:
时,它将继续并处理它下面的所有代码行。也是一般的经验法则,这可能是意见,但通常你可以总是正确的代码,而不必使用任何goto
语句。它们造成的问题多于它的价值。看看你是否可以在没有任何goto
陈述的情况下编写你的逻辑,并且你将终生。