2016-07-24 4 views
4

MsgBoxReults를 기반으로 Sub를 다시 시작하려고합니다. 내가 가지고있는 코드는 오류가 없지만 사용자 선택에 따라 다시 시작하지 않습니다. (다른 IF 내 IF 문을 사용하는 것이 문제가 아님)Excel VBA 자체 내에서 VbMsgBoxResult를 기반으로 동일한 서브 루틴 다시 시작

도와주세요.

Sub ContinueWeatherList() 

Dim Weather As String 
'Assigning a Message Box result as a Variable for Yes/No 
Dim MoreWeather As VbMsgBoxResult 

Weather = InputBox("Type in the weather for " & Range("C1").End(xlDown) + 1) 

If Weather = "" Then 
    MsgBox ("No data entered. Your response has not been recorded"), vbExclamation 
Else 
    Range("C1").End(xlDown).Offset(1, 0).Value = Range("C1").End(xlDown) + 1 
    Range("A1").End(xlDown).Offset(1, 0).Value = Range("A1").End(xlDown) + 1 
    Range("B1").End(xlDown).Offset(1, 0).Value = Weather 
    Columns("A:C").EntireColumn.AutoFit 
    MsgBox "Thank you for entering your data " & vbNewLine & "Would you like to enter another?", vbYesNo 

    'Using IF statement to decide what happens for each condition 
    If MoreWeather = vbYes Then 
     ''Call' command won't reinitiate Sub/*NEED TO FIX* 
     Call ContinueWeatherList 
    Else 
     MsgBox "Thank you for you input.", vbInformation 
    End If 

End If 

최종 하위

+0

@J VBA 변형 및 IF 문제가되지 않습니다 (달성하려는 논리에 따라 다름). 아래 내 대답을 참조하십시오 –

답변

1

아래 코드를 사용해보십시오. VBYesNo MsgBox에서 피드백을 받으려면 변수를 설정해야합니다.

Option Explicit 

Sub ContinueWeatherList() 

Dim Weather As String 
'Assigning a Message Box result as a Variable for Yes/No  
Dim MoreWeather As Variant 

' add label to restart to 
ContinueWeatherList_Restart: 
Weather = InputBox("Type in the weather for " & Range("C1").End(xlDown) + 1) 

If Weather = "" Then 
    MsgBox ("No data entered. Your response has not been recorded"), vbExclamation 
Else 
    Range("C1").End(xlDown).Offset(1, 0).Value = Range("C1").End(xlDown) + 1 
    Range("A1").End(xlDown).Offset(1, 0).Value = Range("A1").End(xlDown) + 1 
    Range("B1").End(xlDown).Offset(1, 0).Value = Weather 
    Columns("A:C").EntireColumn.AutoFit 
    MoreWeather = MsgBox("Thank you for entering your data " & vbNewLine & "Would you like to enter another?", vbYesNo) 

    'Using IF statement to decide what happens for each condition 
    If MoreWeather = vbYes Then 
     ' use GOTo command and label to reinitiate the sub 
     GoTo ContinueWeatherList_Restart 
    Else 
     MsgBox "Thank you for you input.", vbInformation 
    End If 

End If 

End Sub 
1

이 호출 부에 루프를 이동하십시오 IF의 ISN 내부 gotos없이 @Shai 왜 그래의 대답에서

Sub EnterWeatherListItems() 
Dim MoreWeather As VbMsgBoxResult 

MoreWeather = vbYes 
Do While MoreWeather = vbYes 
    Call FillWeatherList 
    'Assigning a Message Box result as a Variable for Yes/No 
    'Using IF statement to decide what happens for each condition 
    MoreWeather = MsgBox("Thank you for entering your data " & vbNewLine & "Would you like to enter another?", vbYesNo) 
Loop 
MsgBox "Thank you for you input.", vbInformation 
End Sub 

Sub FillWeatherList() 
Dim Weather As String 
Weather = InputBox("Type in the weather for " & Range("C1").End(xlDown) + 1) 

If Weather = "" Then 
    MsgBox ("No data entered. Your response has not been recorded"), vbExclamation 
Else 
    ActiveSheet.Range("C1").End(xlDown).Offset(1, 0).Value = ActiveSheet.Range("C1").End(xlDown) + 1 
    ActiveSheet.Range("A1").End(xlDown).Offset(1, 0).Value = ActiveSheet.Range("A1").End(xlDown) + 1 
    ActiveSheet.Range("B1").End(xlDown).Offset(1, 0).Value = Weather 
    Columns("A:C").EntireColumn.AutoFit 
End If 
End Sub 
0

하지만 나 가지고,

Option Explicit 

Sub ContinueWeatherList() 

    Dim Weather As String 
    'Assigning a Message Box result as a Variable for Yes/No 
    Dim NoMoreWeather As Boolean 

    ' Loop until user says otherwise 
    Do Until NoMoreWeather = vbNo 
     Weather = InputBox("Type in the weather for " & Range("C1").End(xlDown) + 1) 

     If Weather = "" Then 
      MsgBox ("No data entered. Your response has not been recorded"), vbExclamation 
     Else 
      Range("C1").End(xlDown).Offset(1, 0).Value = Range("C1").End(xlDown) + 1 
      Range("A1").End(xlDown).Offset(1, 0).Value = Range("A1").End(xlDown) + 1 
      Range("B1").End(xlDown).Offset(1, 0).Value = Weather 
      Columns("A:C").EntireColumn.AutoFit 
      NoMoreWeather = MsgBox("Thank you for entering your data " & vbNewLine & "Would you like to enter another?", vbYesNo) 

     End If 

    Loop 

End Sub 
+2

'날씨 = InputBox ... '는'Do Until' 루프 내에서 이동해야한다고 생각합니다 – YowE3K

+0

맞습니다! – Shodan