Why vbYesNo doesnt exit sub when requested

mysticmario

Active Member
Joined
Nov 10, 2021
Messages
323
Office Version
  1. 365
Platform
  1. Windows
Basically I have a vbYEsNO option when soemoen chooses yes the code exectues as intended, but when someone presses "no" the code also "half-***" executes(creates new sheet and adds some data)
First i only had vbYes and Else today i tried with vbYes vbNo and Else, but nothing seems to stop this pesky code from running. thsi is the code:
VBA Code:
Private Sub kartarealizacji_Click()
MsgBox "Is the project already in the system?" & vbCrLf & "To properly generate material sheet project MUST be already in the system (ArtProInfo v1.5/Project List))", vbYesNo
If vbYes Then
LastSh = ActiveSheet.Name

 Sheets("KARTA REALIZACJI").Visible = xlSheetVisible
 Sheets("KARTA REALIZACJI").Copy after:=Sheets(Sheets.Count)
 Sheets("KARTA REALIZACJI").Visible = xlSheetVeryHidden
 ActiveSheet.Name = "Karta Realizacji projektu"
 ProjectSh = ActiveSheet.Name
 Sheets(LastSh).Activate

'EXPORT DANYCH'
    Dim Rng As Range, cell As Range, lr As Long, i&, j&, mtr As Range, paint As Range

    Sheets(ProjectSh).Range("D5") = Date
    lr = 10
    Set paint = ActiveSheet.Range("K39, K72, K105, K138, K171")
    Set mtr = ActiveSheet.Range("E19, E52, E85, E118, E151")
    Set Rng = ActiveSheet.Range("H195:H273")
    For Each cell In mtr
        If Not IsEmpty(cell) And cell.Value <> 0 Then
            lr = lr + 1
           
            Sheets(ProjectSh).Cells(lr, "B").Value = ("Płyta " & cell.Value)
            Sheets(ProjectSh).Cells(lr, "C").Value = cell.Offset(20, 1).Value & "m2"
            lr = lr + 1
            If cell.Offset(20, 4).Value > 0 Then
            Sheets(ProjectSh).Cells(lr, "B").Value = ("Obrzeże " & cell.Value)
            Sheets(ProjectSh).Cells(lr, "C").Value = cell.Offset(20, 4).Value & "mb"
            Else:
                lr = lr - 1
                GoTo nextcell
               
            End If
        End If
nextcell:             Next cell
    lr = lr + 2
    For Each cell In paint
            If Not IsEmpty(cell) And cell.Value <> 0 Then
            Sheets(ProjectSh).Cells(lr, "B").Value = ("Lakier: " & "*WYMAGANY KOLOR*")
            Sheets(ProjectSh).Cells(lr, "C").Value = cell.Value & "m2"
            lr = lr + 1
            Else:
              lr = lr - 1
                GoTo nextitem
            End If
               
nextitem:    Next cell
    Call export_acc
If vbNo Then

    MsgBox "You must first create a project in the system": Exit Sub
    End If
Else

    MsgBox "You must first create a project in the system": Exit Sub
    End If

    End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Your code needs to test the return value of the MsgBox function

couple of ways can do this

VBA Code:
    Dim Response As VbMsgBoxResult
   
    Const strPrompt As String = "Is the project already in the system?" & vbCrLf & _
                                 "To properly generate material sheet project MUST be already in the system" & _
                                 "(ArtProInfo v1.5/Project List))"
   
    Response = MsgBox(strPrompt, vbYesNo)
        If Response = vbYes Then
                 'yes pressed
        
        Else
       
       
        End If

or

VBA Code:
If MsgBox(strPrompt, vbYesNo) = vbYes Then
        'yes pressed
       
    Else
   
   
    End If

You can read more here: MsgBox function (Visual Basic for Applications)

Dave
 
Upvote 0
Solution
Check out the differences between these two code

VBA Code:
Sub NoVar()
    MsgBox "do you want to continue?", vbYesNo
    If vbYes Then
        MsgBox "Yep"
    Else: MsgBox "Nope"
    End If
End Sub

Sub Var()
    Dim yn_Result
    yn_Result = MsgBox("do you want to continue?", vbYesNo)
    If yn_Result = vbYes Then
        MsgBox "Yep"
    Else: MsgBox "Nope"
    End If
End Sub
 
Upvote 0
Your code needs to test the return value of the MsgBox function

couple of ways can do this

VBA Code:
    Dim Response As VbMsgBoxResult
  
    Const strPrompt As String = "Is the project already in the system?" & vbCrLf & _
                                 "To properly generate material sheet project MUST be already in the system" & _
                                 "(ArtProInfo v1.5/Project List))"
  
    Response = MsgBox(strPrompt, vbYesNo)
        If Response = vbYes Then
                 'yes pressed
       
        Else
      
      
        End If

or

VBA Code:
If MsgBox(strPrompt, vbYesNo) = vbYes Then
        'yes pressed
      
    Else
  
  
    End If

You can read more here: MsgBox function (Visual Basic for Applications)

Dave
Thank you, that helped.
With this check everything runs as intended.
 
Upvote 0
most welcome glad we were able to assist & appreciate feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,638
Members
449,093
Latest member
Ahmad123098

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top