Check action button while running a suboutine

Ulisses_Carso

New Member
Joined
Sep 4, 2020
Messages
39
Office Version
  1. 365
Platform
  1. Windows
Hello guys!

First things first, english isn't my main language so, i'm sorry about any misunderstanding

I have a Main Sub "TVP" with a msgbox condition and two Subs "FillBlank" and "SearchPlate", if user clicks yes it will call first Sub "FillBlank".
Inside "FillBlank" i have another msgbox condition, and again if user clicks yes, the Macro runs, comeback to Sub "TVP" and calls the other sub "SearchPlate".
But, if user click on Cancel button, that Sub Ends and still comeback to Sub "TVP" and run he second Sub.

This is the problem, it can't happen, if the user clicks the Cancel button everything should end. Is there a way to detect in the main Sub if the user clicks the Cancel button of the Sub "FillBlank" on any moment?


Sub TVP()
pergunta = MsgBox("Corrigiu os zeros dos carros novos?", vbQuestion + vbYesNo + vbDefaultButton2, "Macro TVP")
If pergunta = vbYes Then
FillBlank
SearchPlate
Else
End
End If
End Sub

Private Sub FillBlank()

Worksheets("tvp-ago").Activate

Dim InputValue As String
InputValue = InputBox("Digite o valor para preencher a célula em branco:", "Preencher Célula")
If StrPtr(result) <> 0 Then
For Each cell In Selection
If IsEmpty(cell) Then
cell.Value = InputValue
End If
Next
Else
End If
End Sub


Private Sub SearchPlate()
MsgBox ("Didn't worked")
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about like
VBA Code:
Sub TVP()
   Dim InputValue As String

   pergunta = MsgBox("Corrigiu os zeros dos carros novos?", vbQuestion + vbYesNo + vbDefaultButton2, "Macro TVP")
   If pergunta = vbYes Then
      InputValue = InputBox("Digite o valor para preencher a célula em branco:", "Preencher Célula")
      If StrPtr(result) <> 0 Then
         FillBlank (InputValue)
         SearchPlate
      End If
   End If
End Sub

Private Sub FillBlank(InputValue As String)
   
   WorkSheets("tvp-ago").Activate
   
   InputValue = InputBox("Digite o valor para preencher a célula em branco:", "Preencher Célula")
   For Each cell In Selection
      If IsEmpty(cell) Then
         cell.Value = InputValue
      End If
   Next
End Sub


Private Sub SearchPlate()
   MsgBox ("Didn't worked")
End Sub
 
Upvote 0
Hey Fluffy, thanks for your quick reply, but it is not working properly, the way it is, she calls the FillBlank msgbox twice, and if I cancel both times it runs the other sub,

i imagined something like this but it's not working

Sub DM_TVP()
pergunta = MsgBox("Corrigiu os zeros dos carros novos?", vbQuestion + vbYesNo + vbDefaultButton2, "Macro DM-TVP")
If pergunta = vbYes Then
FillBlank
If FillBlank(InputValue) = vbCancel Then
End
Else
SearchPlate
End If
End Sub()

i need to do it separately and if user click on Cancel Button, it returns to Main Sub and check why that sub finished, if it was canceled then ends everything, if not, runs SearchPlate
 
Upvote 0
Oops, forgot to remove the input box from the FillBlank sub.
If you delete it, it should be fine.
 
Upvote 0
I've found the problem, the InputBox doesn't have a Button Argument so i can't treat conditions.
I think i'll have to create two boxes, one with Ok/Cancel and another only with a input value without buttons, do you know if is it possible?
 
Upvote 0
If you delete this line from the FillBlank sub you should be fine
VBA Code:
InputValue = InputBox("Digite o valor para preencher a célula em branco:", "Preencher Célula")
 
Upvote 0
i deleted that line, but i have another input box on Sub "TVP", i need to replace that input box with a box with a field to fill and OK button, if I could handle the input box buttons I would have already solved it
 
Upvote 0
Should the fillblank sub if no value is entered in the TVP inputbox?
 
Upvote 0
I solved it, i removed the InputBox and forced the value to InputValue, i must be zero always, so problem solved!


VBA Code:
Sub TVP()
   Dim InputValue As String

    Worksheets("tvp-ago").Activate
    
    pergunta = MsgBox("Corrigiu os zeros dos carros novos?", vbQuestion + vbYesNo + vbDefaultButton2, "Macro TVP")
    If pergunta = vbYes Then
        FillBlank
        SearchPlate
    Else
        End
    End If
End Sub

Private Sub FillBlank()
   
   Worksheets("tvp-ago").Activate
   
   InputValue = "0"
   For Each cell In Selection
      If IsEmpty(cell) Then
         cell.Value = InputValue
      End If
   Next
End Sub

I would like to deeply thank you for your patience in helping me with this code, I am very grateful to you Fluffy!
Kind Regards
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,090
Members
449,065
Latest member
Danger_SF

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