I want to click in a certain cell and for the Userform with the Monthview to appear , then once I have selected a date and it is inserted into the cell, i want the userform to close automatically
This code is in Work Sheet
Private Sub worksheet_selectionchange(ByVal target As Range)
If Not Application.Intersect(Range("l14"), target) Is Nothing Then
UserForm1.Show
End If
End Sub
This code is in the UserForm - MonthView1
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
ActiveCell.Value = DateClicked
End Sub
Any assistance would be Grateful
UserForm1.Show is toxic; you're essentially storing state in global scope, and that will inevitably cause issues down the line. Make a new instance of the form instead:
If Not Application.Intersect(Range("l14"), target) Is Nothing Then
With New UserForm1
.Show
End With
End If
Now, a form/dialog exists to collect user input, not to consume that input and change cell values. What happens to that input should be up to the code that's using that form, not up to the form itself. Expose the selected Date value with a property.
The user can do 2 things: either they select a date and you have a SelectedDate, or they clicked that [X] button and dismissed the form: You need to be able to tell what the user did. Handle QueryClose for that, and expose an IsCancelled property.
Selecting a date, or cancelling out, should hide the form, not destroy it.
Option Explicit
Private selectedValue As Date
Private cancelled As Boolean
Public Property Get IsCancelled() As Boolean
IsCancelled = cancelled
End Property
Public Property Get SelectedDate() As Date
SelectedDate = selectedValue
End Property
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
selectedValue = DateClicked
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = True
cancelled = True
Me.Hide
End If
End Sub
And now you have a date picker form that you can reuse whenever you need it, because it doesn't know or care about what happens to the value that was selected:
If Not Application.Intersect(Range("l14"), target) Is Nothing Then
With New UserForm1 ' todo: rename form to "DatePickerDialog" or something
.Show
If Not .IsCancelled Then
target.Value = .SelectedDate
End If
End With
End If
You could add unload me in that sub:
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
ActiveCell.Value = DateClicked
Unload me
End Sub
Related
Getting a Compile error: method or data member not found when trying to initialize a calendar box from a user form. The error is at Me.Calendar.Value = Date
I believe it worked on 32 bit but we switched to 64 bit. I tried googling and a post said use Monthview but I couldn't get it to work. Any ideas how to get this to work? Is it a reference issue?
Private Sub btnCancel_Click()
' Close form on Cancel
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
' Close form on exit (x-button)
If CloseMode = 0 Then Unload Me
End Sub
Private Sub UserForm_Initialize()
' Start form on today's date
Me.Calendar.Value = Date
datePickerValue = 0
End Sub
Private Sub Calendar_DateClick(ByVal DateClicked As Date)
' Set variable to date selected on calendar
datePickerValue = DateClicked
' Close calendar after date selected
Me.Hide
Unload Me
End Sub
I need some help. I am writing VBA script for an Excel macro. I have two forms, the first has a button calling the second. On the second I want it to return a variable (tempdate, this is declared as public type Date in the main Worksheet).
Main form button code:
Private Sub SetDueButton_Click()
UserForm1.Show
DueDate.Value = tempdate 'display tempdate in the DueDate text box
End Sub
UserForm1 'ok' button:
Private Sub CommandButton53_Click()
tempdate = TextBox1.Value
Unload Me
End Sub
What am I missing to get this variable to load whatever is in TextBox1.Value?
I would use a property in the userform
Your main form code would change to look like this:
Option Explicit
Private Sub SetDueButton_Click()
Dim UF As UserForm1 'you can declare a user form as an independent object
UF.Show
DueDate.Value = UF.tempdate 'get the property
Unload UF 'now you can unload or set to Nothing
End Sub
Your UserForm1 code would be this:
Option Explicit
Public Property Get tempdate() As String
tempdate = Me.TextBox1.Value 'Me refers to the form itself
End Property
Private Sub CommandButton53_Click()
Me.Hide 'hide don't unload yet or you can't get the data.
End Sub
You need to declare the variable in a module, not the worksheet, and the declaration needs to be:
Global tempDate as Date
or
Public tempDate as Date
The variables in the worksheet are not available in the forms.
It would be possible to make a function in the worksheet and call this function, but this is a much simpler solution.
I have a sub that calls a userform to show and would only like to proceed if the user didn't click my Cancel button. I don't want to put all my other sub calls within the userform.
Is it possible to have a userform return a value or a way to check if the user clicked a particular button?
I suppose I can use a global variable, but was wondering if I could pass things to and from a userform.
I prefer to use properties.
Inside your userForm
Private m_bCancel As Boolean
Public Property Get Cancel() As Boolean
Cancel = m_bCancel
End Property
Public Property Let Cancel(ByVal bCancel As Boolean)
m_bCancel = bCancel
End Property
Code for the cancel button
Private Sub cmdCancel_Click()
Me.Cancel=True
Me.Hide
End Sub
Call the userForm from outside like this
sub loadForm()
dim frm
set frm= new UserForm1
frm.show
if frm.Cancel then
Msgbox "Cancelled"
end if
End Sub
I have the following button on a Form:
Private Sub CommandButton1_Click()
Dim pass As String
pass = UserForm1.TextBox1
Unload UserForm1
End Sub
I then have a Module called Module1:
Public Sub Login()
...
UserForm1.Show
driver.findElementByName("PASSWORD").SendKeys pass
...
End Sub
The idea is whatever password the users enters into the input box will be assigned to the variable pass. What I'm having trouble doing however is passing pass from UserForm1 into Module1's Login sub.
I would of thought adding something like Module1.Login (pass) to my form before I unload it would work, however that doesn't seem to pass anything. Any help would be much appreciated. Thanks.
Don't declare the variable in the userform. Declare it as Public in the module.
Public pass As String
In the Userform
Private Sub CommandButton1_Click()
pass = UserForm1.TextBox1
Unload UserForm1
End Sub
In the Module
Public pass As String
Public Sub Login()
'
'~~> Rest of the code
'
UserForm1.Show
driver.findElementByName("PASSWORD").SendKeys pass
'
'~~> Rest of the code
'
End Sub
You might want to also add an additional check just before calling the driver.find... line?
If Len(Trim(pass)) <> 0 Then
This will ensure that a blank string is not passed.
Siddharth's answer is nice, but relies on globally-scoped variables. There's a better, more OOP-friendly way.
A UserForm is a class module like any other - the only difference is that it has a hidden VB_PredeclaredId attribute set to True, which makes VB create a global-scope object variable named after the class - that's how you can write UserForm1.Show without creating a new instance of the class.
Step away from this, and treat your form as an object instead - expose Property Get members and abstract away the form's controls - the calling code doesn't care about controls anyway:
Option Explicit
Private cancelling As Boolean
Public Property Get UserId() As String
UserId = txtUserId.Text
End Property
Public Property Get Password() As String
Password = txtPassword.Text
End Property
Public Property Get IsCancelled() As Boolean
IsCancelled = cancelling
End Property
Private Sub OkButton_Click()
Me.Hide
End Sub
Private Sub CancelButton_Click()
cancelling = True
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
cancelling = True
Cancel = True
Me.Hide
End If
End Sub
Now the calling code can do this (assuming the UserForm was named LoginPrompt):
With New LoginPrompt
.Show vbModal
If .IsCancelled Then Exit Sub
DoSomething .UserId, .Password
End With
Where DoSomething would be some procedure that requires the two string parameters:
Private Sub DoSomething(ByVal uid As String, ByVal pwd As String)
'work with the parameter values, regardless of where they came from
End Sub
I have created a userform that contains two checkboxes. I would like to be able to do different things depending on whether each box is checked or unchecked. However, it seems like no matter what I do, it will always tell me the original value of the checkboxes (false and false). Here is the code attached to clicking CommandButton1:
Private Sub CommandButton1_Click()
ReadData
End Sub
And here ReadData:
Sub ReadData()
Dim myForm As UserForm
Set myForm = UserForms.Add("ComplaintEntryForm")
Debug.Print (myForm!CheckBox1.Name)
Debug.Print (myForm!CheckBox1.Value)
Debug.Print (myForm!CheckBox2.Name)
Debug.Print (myForm!CheckBox2.Value)
End Sub
No matter how the boxes are checked, the immediate window always shows this:
VBA.UserForms.Add("ComplaintEntryForm").Show
CheckBox1
False
CheckBox2
False
I have a screenshot of the whole operation but it won't let me upload it because I'm a new user.
Try this method to load and show the form (this goes in a normal module):
Sub main()
Dim myForm As ComplaintEntryForm
Set myForm = New ComplaintEntryForm
myForm.Show
Set myForm = Nothing
End Sub
In the UserForm's own module, add the following:
Private Sub CheckBox1_Change()
readData
End Sub
Private Sub CheckBox2_Change()
readData
End Sub
Private Sub UserForm_Initialize()
Me.CheckBox1.Value = True
Me.CheckBox2.Value = False
End Sub
Private Sub readData()
Debug.Print Me.CheckBox1.Name
Debug.Print Me.CheckBox1.Value
Debug.Print Me.CheckBox2.Name
Debug.Print Me.CheckBox2.Value
End Sub
I've initialized the two checkboxes to specific values in the Initialize event. This means we are certain about the state the form will start in