I want to pass a value selected by user to be displayed in MsgBox. I write the following code but its display nothing.
Public Sub CommandButton1_Click()
SelectedCity = Me.ComboBox1.Value
DistSystem
End Sub
Sub DistSystem()
MsgBox (SelctedCity)
End Sub
Second procedure cannot read variable because of wrong scope.
You have to declare SelectedCity variable as global:
Global SelectedCity
Public Sub CommandButton1_Click()
SelectedCity = Me.ComboBox1.Value
DistSystem
End Sub
Sub DistSystem()
MsgBox (SelctedCity)
End Sub
There are times when you can't pass it in an argument, but you can pass it here.
Option Explicit 'forces to declare all variables
Public Sub CommandButton1_Click()
call DistSystem (Me.ComboBox1.Value)
'same as ( without call, and () ) :
' DistSystem Me.ComboBox1.Value
End Sub
Sub DistSystem(byval selectedCity$) 'same as: byval SelectedCity as string
MsgBox SelectedCity
End Sub
This works even if DistSystem is in a different module.
Declare Selectedcity as a public OUTSIDE of the Sub
Public SelectedCity as String
Public Sub CommandButton1_Click()
SelectedCity = Me.ComboBox1.Value
Call DistSystem
End Sub
Sub DistSystem()
MsgBox (SelectedCity)
End Sub
Obviously place DistSystem in a module!
Related
So far I have used the below VBA in order to continue with a procedure if the user clicked ok in the MsgBox:
Sub Button_Message_Box()
Answer = MsgBox("Do you want to continue the procedure?", vbOK)
If Answer = vbOK Then
Sheet1.Range("A1").Value = 1
Else
End If
End Sub
Now I want to achieve the exact same result using CommandButton1 in UserForm1.
Therefore I tried to go with this:
(1) VBA in UserForm1:
Private Sub CommandButton1_Click()
Unload Me
End Sub
(2) VBA in Modul1:
Sub Button_Procedure()
Call UserForm1.Show(vbModeless)
If CommandButton1 = True Then
Sheet1.Range("A1").Value = 1
Else
End If
End Sub
The VBA goes through but it does not enter the value 1 into Cell A1.
What do I need to modify to achieve the desired result?
I strongly suggest to follow the steps in this article: Rubberduck: UserForm1.Show
Nevertheless, a simple and dirty implementation could be as follows:
The form's code behind:
Add an event to raise when the OK-Cancel button has been pressed passing a boolean value indicating either to proceed or not:
Public Event OnClose(ByVal bool As Boolean)
Private Sub CmdOK_Click()
RaiseEvent OnClose(True)
End Sub
Private Sub CmdCancel_Click()
RaiseEvent OnClose(False)
End Sub
A simple wrapper class:
Here, we just instantiate the form and listen to the OnClose() event.
Option Explicit
Private WithEvents objForm As UserForm1
Private m_flag As Boolean
Public Function Show() As Boolean
Set objForm = New UserForm1
objForm.Show ' No vbModeless here, we want to halt code execution
Show = m_flag
End Function
Private Sub CloseForm()
Unload objForm
Set objForm = Nothing
End Sub
Private Sub objForm_OnClose(ByVal bool As Boolean)
m_flag = bool
CloseForm
End Sub
Calling the wrapper class:
Sub Something()
Dim bool As Boolean
With New FormWrapper
bool = .Show
End With
MsgBox "Should I proceed? " & bool
End Sub
With reference to this question I used a Boolean variable:
(1) Code in UserForm1:
Private continue_procedure As Boolean
Private Sub CommandButton1_Click()
continue_procedure = True
Unload Me
End Sub
Function check_procedure() As Boolean
UserForm1.Show
check_procedure = continue_procedure
End Function
(2) Code in Modul1:
Sub Button_Procedure()
If UserForm1.check_procedure() = True Then
Sheet1.Range("A1").Value = 1
Else
End If
End Sub
I am trying to add a TextBox string value to cell A1. I am barely learning the basics of writing code using an MVP (Modal, View, Presenter), after reading this article from Mathieu Guindon. I am getting a Compile Error: Variable not defined Error is found on Sheet1.Range("A1") = clientName Why?
Here is my code:
Userform Name: "UserForm1"
(code behind UserForm1)
Option Explicit
Public Event OnRunReport()
Public Event OnExit()
Public Property Get clientName() As String
clientName = clientNameBox.Text
End Property
Public Property Let clientName(ByVal value As String)
clientNameBox.Text = value
End Property
Private Sub clientNameBox_Change()
clientName = clientNameBox.Text
End Sub
Private Sub exitButton_Click()
RaiseEvent OnExit
End Sub
Private Sub submit_Click()
RaiseEvent OnRunReport
End Sub
Private Sub UserForm_Initialize()
RaiseEvent OnRunReport
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
Hide
End If
End Sub
Module1
Option Explicit
Private objClientForm As UserForm1
Public Sub AddClientsToSheet()
Sheet1.Range("A1") = clientName '******error line *******
End Sub
Public Sub showMainForm()
If (objPresenter Is Nothing) Then
Set objPresenter = New AddClientsToSheet
End If
objPresenter.show
End Sub
ClassModule Name: AddClientsToSheet
Option Explicit
Private WithEvents objClientForm As UserForm1
Public Sub Class_Initialize()
Set objClientForm = New UserForm1
End Sub
Private Sub Class_Terminate()
Set objClientForm = Nothing
End Sub
Public Sub show()
If Not objClientForm.Visible Then
objClientForm.show
End If
With objClientForm
.Top = CLng((Application.Height / 2 + Application.Top) - .Height / 2)
.Left = CLng((Application.Width / 2 + Application.Left) - .Width / 2)
End With
End Sub
Private Sub Hide()
If objClientForm.Visible Then objClientForm.Hide
End Sub
Private Sub objClientForm_OnExit()
Hide
End Sub
Private Sub objClientForm_OnRunReport()
AddClientsToSheet
End Sub
I couldn't quite find what I'm looking for but maybe you can help me anyway.
My problem is that I have a userform where the user has to make an input. I want to store that input and use it later in a different module i.e. paste it into a cell. The simple solution should be to just make it a public variable, but for some reason it won't work. Here is the code I tried to use:
Userform:
Option Explicit
Public VarBezeichnungReifenliste As String
Private Sub CommandButton3_Click()
VarBezeichnungReifenliste = TextBox1.Value
Call Übertragen
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Module:
Option Explicit
Public Sub Übertragen()
Worksheets("XY").Cells(1, 1).Value = VarBezeichnungReifenliste
End Sub
The error message says the variable is not declared (VarBezeichnungReifenliste) so i guess I didn't declare it publicly enough?
The userform itself is opened via a simple button on the worksheet using Userform1.Show. So nothing fancy here.
Publicly Enough
Solution1
UserForm1:
Option Explicit
Private Sub CommandButton3_Click()
VarBezeichnungReifenliste = TextBox1.Value
Module1.Übertragen
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Module1:
Option Explicit
Public VarBezeichnungReifenliste As String
Sub Übertragen()
Worksheets("XY").Cells(1, 1).Value = VarBezeichnungReifenliste
End Sub
Conclusion
Just move the variable declaration
Public VarBezeichnungReifenliste As String
to a 'normal' module.
Solution2
UserForm1:
Option Explicit
Public VarBezeichnungReifenliste As String
Private Sub CommandButton3_Click()
VarBezeichnungReifenliste = TextBox1.Value
Module1.Übertragen
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Module1:
Option Explicit
Sub Übertragen()
Worksheets("XY").Cells(1, 1).Value = UserForm1.VarBezeichnungReifenliste
Worksheets("XY").Cells(1, 1).Select
End Sub
Conclusion
Just use
Worksheets("XY").Cells(1, 1).Value = UserForm1.VarBezeichnungReifenliste
instead of
Worksheets("XY").Cells(1, 1).Value = VarBezeichnungReifenliste
in Module1.
Solution3
UserForm1:
Option Explicit
Public VarBezeichnungReifenliste As String
Private Sub CommandButton3_Click()
VarBezeichnungReifenliste = TextBox1.Value
Übertragen
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Sub Übertragen()
Worksheets("XY").Cells(1, 1).Value = VarBezeichnungReifenliste
End Sub
Conclusion
Move everything into UserForm1.
VBA is weird about storing variables over the long-term. As a general rule of thumb, if you're able to interact with Excel workbooks in between a variable being saved and when you need to get the value, you can't count on that variable still holding its value.
The safest way to get around this is to just store your value in a cell of a hidden worksheet, and read it from there when you need it.
I am trying to return a value from a userform to another macro.
Here is an example of a piece of code that I want to return the value intMonth:
sub comparison()
UserForm1.Show
end sub
then I have the userform code:
Private Sub initialize()
OptionButton1 = False
End Sub
Private Sub OptionButton1_Click()
intMonth = 1
Me.Hide
End Sub
How do I get the intMonth value of 1 back to my original comparison() function?
This is a minimal example, but should help.
In the UserForm:
Option Explicit
Option Base 0
Public intMonth As Long ' <-- the variable that will hold your output
Private Sub initialize()
OptionButton1 = False
intMonth = 0
End Sub
Private Sub CommandButton1_Click() ' OK button
Me.Hide
End Sub
Private Sub OptionButton1_Click()
intMonth = 1 '<-- set the value corresponding to the selected radio button
End Sub
Private Sub OptionButton2_Click()
intMonth = 2
End Sub
In a module or ThisWorkbook:
Option Explicit
Option Base 0
Sub comparison()
UserForm1.Show
MsgBox CStr(UserForm1.intMonth) ' <-- retrieve the value
End Sub
Another useful way to achieve what you need is to wrap the code in a public function in the userform.
In the UserForm:
Option Explicit
Option Base 0
Private intMonth As Long
Public Function Choose_Option()
OptionButton1 = False
intMonth = 0
Me.show()
Choose_Option = intMonth
End sub
Private Sub CommandButton1_Click() ' OK button
Me.Hide
End Sub
Private Sub OptionButton1_Click()
intMonth = 1
End Sub
Private Sub OptionButton2_Click()
intMonth = 2
End Sub
Then in module, it is simple as this:
Option Explicit
Option Base 0
Sub comparison()
MsgBox Userform1.Choose_Option()
End Sub
This way, the function is in charge of showing the userform, prompting the user and returning the value.
If you debug this function, you will see that after Me.Show() is called, the function halts and continue only when the userform is hidden, which is done in the OK button.
I want to pass a CommandButton as an argument.
Example:
Sub calc(btn as button)
btn.Caption = "Something"
End Sub
Private Sub CommandButton1_Click()
calc(CommandButton1)
End Sub
Private Sub CommandButton2_Click()
calc(CommandButton2)
End Sub
Is something like the above possible? If yes how can I do it?
edit
Thanks for your response, but I dont get it. So it looks like this now:
Public Sub calc(ByRef btn as Object)
btn.Caption = "Something"
End Sub
Private Sub CommandButton1_Click()
calc(CommandButton1)
End Sub
Private Sub CommandButton2_Click()
calc(CommandButton2)
End Sub
Maybe someone can explain it to me in more detail, because Im very new to VBA.
You need:
Sub calc(btn As MSForms.CommandButton)
btn.Caption = "Something"
End Sub
And you must invoke it following the rules:
calc CommandButton1 // best
call calc (CommandButton1) // ok but verbose
calc (CommandButton1) // type mismatch!
(The type mismatch is because the parentheses evaluate CommandButton1 which results in its default property (a string) which is incompatible with the method argument type)
This is the sub:
Public Sub temp(ByRef cmdb As Object)
cmdb.Caption = "somethine else"
End Sub
This is how you would call it
call sub (commandbutton_1)