2017-11-16 15 views
0

케이스 선택과 msgbox를 포함하는 구문을 작성했습니다. 구문을 사용하여 스프레드 시트에서 첫 번째 활성 셀을 찾고 일부 사전 정의 된 질문과 대답에 따라 A1 또는 A2로 이동 시키지만 명령이 최적으로 작동하지 않습니다. somene는 나를 도울 수 있습니까?케이스 선택 구문이 루프를 만들고 있습니다. 원하지 않습니다.

이전 질문에 제공된 답을 토대로 직관적으로 메시지 상자를 표시하고 싶지만 메시지 상자가 복잡해지기 쉽다.

아래 코드를 첨부했습니다.

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

읽어보십시오. [왜 누군가가 나를 도울 수 있습니까? 실제 질문이 아닙니까?] (https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not) -an- 실제 - 질문) –

+0

나는 그 질문이 합리적이라고 생각한다. 영업 담당자는 자신이 가진 문제를 분명히 밝히고 이유를 알 수 없습니다. –

+0

감사합니다 R.Roe. 나는 내가 성취하려는 것을 알아내는 방법을 알지 못하고 프로그래밍 페이지에서 모든 철학적 인 질문을하는 것이 사소한 데이비드라고 생각하기 때문에 질문을하고있다. –

답변

3

귀하의 goto 결말 문 Quit:은 맨 끝에 와야합니다. 코드에서 Quit:으로 이동하면 코드 아래에있는 모든 코드 줄을 계속 처리합니다. 또한 엄지의 일반적인 규칙과 마찬가지로 의견 일지 모르지만 보통 goto 문을 사용하지 않고도 항상 올바른 코드를 작성할 수 있습니다. 그들은 가치있는 것보다 더 많은 쟁점을 야기합니다. goto 문없이 로직을 작성할 수 있는지 확인하십시오. 그러면 평생 동안 설정 될 것입니다.

+0

다시 고마워한다. –

+0

대답했다고 생각하면 대답으로 표시하도록한다. 너의 질문. –

+0

안녕하세요. Roe. goto 문을 제외해도 그 조건이 충족되지 않아도 "조건부"msgbox가 계속 팝업됩니다. –