MsgBox appearing thrice in VBA?

mehidy1437

Active Member
Joined
Nov 15, 2019
Messages
348
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
Hi Guys,

I'm working on below code, end result is okay, but I'm getting the MsgBox thrice.
After editing the cell when I press enter its giving the msg & have to press ok/enter another two times to deactivate the msg.

This code is running on "Worksheet" with change event.
Need your help to solve it.

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim addActiveCell As String, cellValue As String, count As Integer, offaddActiveCell As String
    Dim cwcyiwiy As String, lengthda As String, widthda As String, weightda As String, addida As String, fabprice As String, cmaccsgraothrs As String
    Dim lengthplus As String, widthplus As String, weightplus As String, addiplus As String, cmaccsgraothrsplus As String
      
            
 On Error GoTo Last
                                              
  'vba run or not confirmation
  Dim Msg As String, Ans As Variant
  Msg = "If you run this code, it will convert data into fabrics consumption into ACTIVECELL." _
   & vbNewLine & "Data sequence should be as/or->  cw, 65 22 10, 58 4, 250, 0.15" _
   & vbNewLine & "Data sequence should be as/or->  cy, 65 22 10, 58 4, 58, 0.5" _
   & vbNewLine & "Data sequence should be as/or->  iw, 38 10 4, 27 2, 250, 0.15" _
   & vbNewLine & "Data sequence should be as    ->  iy, 38 10 4, 27 2, 58, 0.5"

  Ans = MsgBox(Msg, vbYesNo)

  Select Case Ans

      Case vbYes
'vba run or not confirmation

    addActiveCell = ActiveCell.Address(0, 0)
    'cellValue = Trim(ActiveCell.Value)
    offaddActiveCell = ActiveCell.Offset(-1, 0).Address(0, 0)
    cellValue = Trim(ActiveCell.Offset(-1, 0).Value)
        'count =Len(string)-Len(Replace(string,"/",""))
        count = Len(cellValue) - Len(Replace(cellValue, ",", ""))
            
            Set Target = Range(offaddActiveCell)
            'Set Target = Range("c13")
                 'If target.Value = count Then
                        
      If count = 4 Then

                        'FabricsConsumptions
                        '========================
                    cwcyiwiy = Trim(Split(Range(offaddActiveCell).Value, ",")(0))
                    lengthda = Trim(Split(Range(offaddActiveCell).Value, ",")(1))
                    widthda = Trim(Split(Range(offaddActiveCell).Value, ",")(2))
                    weightda = Trim(Split(Range(offaddActiveCell).Value, ",")(3))
                    addida = Trim(Split(Range(offaddActiveCell).Value, ",")(4))
                    
                    lengthplus = Replace(lengthda, " ", "+")
                    'Range("i10") = lengthplus
                    widthplus = Replace(widthda, " ", "+")
                    'weightplus = Replace(widthda, " ", "+")
                        If cwcyiwiy = "cw" Then
                        
                            Range(offaddActiveCell).Formula = "=((((" & lengthplus & ")*(" & widthplus & ")*2*12*" & weightda & ")/10000000+" & addida & ")*105%)"
                            
                            ElseIf cwcyiwiy = "cy" Then
                            
                            Range(offaddActiveCell).Formula = "=(((((" & lengthplus & ")*(" & widthplus & ")*2*12)/36/2.54/2.54/" & weightda & ")+" & addida & ")*105%)"
                            
                            ElseIf cwcyiwiy = "iw" Then
                            
                            Range(offaddActiveCell).Formula = "=((((" & lengthplus & ")*(" & widthplus & ")*2*12*" & weightda & ")/1550000+" & addida & ")*105%)"
                            
                            ElseIf cwcyiwiy = "iy" Then
                            
                            Range(offaddActiveCell).Formula = "=(((((" & lengthplus & ")*(" & widthplus & ")*2*12)/36/" & weightda & ")+" & addida & ")*105%)"
                            
                            Else
                            Range(offaddActiveCell).Formula = "=((((" & lengthplus & ")*(" & widthplus & ")*2*12*" & weightda & ")/10000000+" & addida & ")*105%)"

                        End If
 End If
 
 
 If count = 6 Then
                        
                        'total fob price
                        '========================
                    cwcyiwiy = Trim(Split(Range(offaddActiveCell).Value, ",")(0))
                    lengthda = Trim(Split(Range(offaddActiveCell).Value, ",")(1))
                    widthda = Trim(Split(Range(offaddActiveCell).Value, ",")(2))
                    weightda = Trim(Split(Range(offaddActiveCell).Value, ",")(3))
                    addida = Trim(Split(Range(offaddActiveCell).Value, ",")(4))
                    fabprice = Trim(Split(Range(offaddActiveCell).Value, ",")(5))
                    cmaccsgraothrs = Trim(Split(Range(offaddActiveCell).Value, ",")(6))

                    lengthplus = Replace(lengthda, " ", "+")
                    'Range("i10") = lengthplus
                    widthplus = Replace(widthda, " ", "+")
                    cmaccsgraothrsplus = Replace(cmaccsgraothrs, " ", "+")
                        If cwcyiwiy = "cw" Then
                        
                            Range(offaddActiveCell).Formula = "=(((((" & lengthplus & ")*(" & widthplus & ")*2*12*" & weightda & ")/10000000+" & addida & ")*105%)*" & fabprice & "+" & cmaccsgraothrsplus & ")/12*110%"
                            
                            ElseIf cwcyiwiy = "cy" Then
                            
                            Range(offaddActiveCell).Formula = "=(((((" & lengthplus & ")*(" & widthplus & ")*2*12)/36/2.54/2.54/" & weightda & ")+" & addida & ")*105%)*" & fabprice & "+" & cmaccsgraothrsplus & ")/12*110%"
                            
                            ElseIf cwcyiwiy = "iw" Then
                            
                            Range(offaddActiveCell).Formula = "=((((" & lengthplus & ")*(" & widthplus & ")*2*12*" & weightda & ")/1550000+" & addida & ")*105%)*" & fabprice & "+" & cmaccsgraothrsplus & ")/12*110%"
                            
                            ElseIf cwcyiwiy = "iy" Then
                            
                            Range(offaddActiveCell).Formula = "=(((((" & lengthplus & ")*(" & widthplus & ")*2*12)/36/" & weightda & ")+" & addida & ")*105%)*" & fabprice & "+" & cmaccsgraothrsplus & ")/12*110%"
                            
                            Else
                            Range(offaddActiveCell).Formula = "=((((" & lengthplus & ")*(" & widthplus & ")*2*12*" & weightda & ")/10000000+" & addida & ")*105%)*" & fabprice & "+" & cmaccsgraothrsplus & ")/12*110%"

                        End If
                                                      
  End If

'vba run or not confirmation
 Case vbNo
 GoTo Quit:
 End Select
Quit:
'vba run or not confirmation
Last:  Exit Sub

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
That's happening because you're changing cells within a worksheet_change event... which then causes the worksheet_change event to be called again.

To prevent that recursiveness, put
Code:
 Application.EnableEvents = False
at the beginning of your Worksheet_Change Event, and put
Code:
 Application.EnableEvents = True
at the end of your Worksheet_Change Event.
 
Upvote 0
That's happening because you're changing cells within a worksheet_change event... which then causes the worksheet_change event to be called again.

To prevent that recursiveness, put
Code:
 Application.EnableEvents = False
at the beginning of your Worksheet_Change Event, and put
Code:
 Application.EnableEvents = True
at the end of your Worksheet_Change Event.

After adding these , the code run for only one cell in the sheet.
When I do the changes for others cell, it doesn't run the code.
 
Upvote 0
Sounds like your events aren't being enabled after the first cell is changed.

Your Application.EnableEvents needs to go above your "Last:" line. Otherwise, you'll exit the sub before that line is executed.

Note that, if your events are disabled, you can use the immediate window in VBA to run Application.EnableEvents = True to reset it.
 
Upvote 0
Sounds like your events aren't being enabled after the first cell is changed.

Your Application.EnableEvents needs to go above your "Last:" line. Otherwise, you'll exit the sub before that line is executed.

Note that, if your events are disabled, you can use the immediate window in VBA to run Application.EnableEvents = True to reset it.

Many thanks, Its working now.

Just one more point. I'm trying to add this MsgBox under below sections, but couldn't getting any idea, how can I do this?

Here
VBA Code:
 If count = 4 Then
MsgBox with some msg!!

And Here
Code:
If count = 6 Then
MsgBox with some others msg!!

And then I want to press ok/cancel to run the code or not?
 
Upvote 0
Thanks, I have got the idea & working on my code.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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