Excel VBA: CommandButton click time - excel

I wonder if there is a way to speed up time of clicking CommandButtons?
Thing is when I use my UserForm in many cases I click "next" button multiple times, however button does not react as fast as I click. So there are times I clicked 3 times and "next" button took only to second record rather then forth.
Edit:
Private Sub CommandButton1_Click()
TextBox1 = TextBox1 + 1
End Sub
Private Sub CommandButton2_Click()
TextBox1 = TextBox1 - 1
End Sub
Private Sub UserForm_Initialize()
TextBox1.Value = 1
End Sub

The anwser credits goes to Rory who pointed out DblClick Event. So having that in mind it is easy to to change code to work better. All we need to do is add Dblclick Event and work it as minus / plus 2 so it looks like working faster then before.
In my case I needed to add some restriction with numbers below zero but main idea is wrote below :)
Private Sub CommandButton1_DblClick()
TextBox1 = TextBox1 + 2
End Sub
Private Sub CommandButton2_DblClick()
TextBox1 = TextBox1 - 2
End Sub

Related

One date picker for all combo boxes in an Excel userform

What I'd like to accomplish:
Have separate comboboxes on a userform, such that when the individual drop down button is clicked, load a global userform with the calendar control on it. Since I will have multiple combo boxes, it wouldn't be feasible to keep adding new separate userforms with calendars controls on it. Instead, just to have one main userform with a calendar control that does all of the work for all my combo boxes.
When a date is selected from the global date picker calendar control, to insert the selected date into the combo box that was clicked.
I've found pieces but I can't make sense of them since I am new to this.
Think about this, I can think of one solution.
Add a Label.
When clicking the combobox, the label will have the combobox name, then from there, when you click a date in the calendar control, it will send the date to the combobox name in the label.
I just grabbed whatever calendar control that was available in the toolbox, yours may be different.
Private Sub ComboBox1_DropButtonClick()
Me.Label1 = Me.ComboBox1.Name
End Sub
Private Sub ComboBox2_DropButtonClick()
Me.Label1 = Me.ComboBox2.Name
End Sub
Private Sub ComboBox3_DropButtonClick()
Me.Label1 = Me.ComboBox3.Name
End Sub
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
s = Me.Label1.Caption
Me.Controls(s) = Me.MonthView1.Value
End Sub
As pointed out by #MathieuGuindon, when clicking the combobox you can assign the tag to the Calendar control, then you would not have to place the label approach.
Thanks Mathieu...
Private Sub ComboBox1_DropButtonClick()
Me.MonthView1.Tag = Me.ComboBox1.Name
End Sub
Private Sub ComboBox2_DropButtonClick()
Me.MonthView1.Tag = Me.ComboBox2.Name
End Sub
Private Sub ComboBox3_DropButtonClick()
Me.MonthView1.Tag = Me.ComboBox3.Name
End Sub
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
s = Me.MonthView1.Tag
Me.Controls(s) = Me.MonthView1
End Sub

VBA: Userform Textbox_Change Only Works Once

Having a weird issue with Excel Userforms.
I've got 2 userforms, one with a textbox and the other with a button. The one with the textbox uses Textbox_Change event to track what's in the box and after 10 characters have been entered it loads Userform2.
When Userform2 initialises Userform1 is unloaded. Userform2 has a button which then reloads Userform1.
This is a very slimmed down version of my end result, but I needed to see it in it's most basic form to make sure it wasn't my code that was causing an issue.
Anyway, when I'm typing in Userform1's text box it tracks the changes of TextBox1, but after it's reloaded it stops tracking.
Below is my code.
Userform1:
Dim Iteration As Integer
Private Sub TextBox1_Change()
Debug.Print Me.TextBox1.Value
Iteration = Iteration + 1
If Iteration = 10 Then
UserForm2.Show
End If
End Sub
Private Sub UserForm_Initialize()
Iteration = 0
End Sub
Userform2:
Private Sub CommandButton1_Click()
UserForm1.Show
End Sub
Private Sub UserForm_Initialize()
Unload UserForm1
End Sub
It's as simple as it could possibly be.
When I first loaded Userform1 I entered into the text box "1234567890", which outputted the console:
1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
As expected.
However after I've loaded Userform2 and then click the button to reload Userform1, when I enter "abcdefghij" into the textbox it doesn't output anything to the console.
Is there some fundamental concept of userforms that I'm missing here? Or something I'm getting wrong? I can't see anything that would be causing this.
I've been reading up on what unloading and show actually does and I can't find anything that'd affect whether or not the event should fire. From what I know when you run Userform.show it reinitialises everything, but even if it didn't it should still output SOMETHING to the console.
Can anyone help?
EDIT:
even if I change Userform 1 to be:
Dim Iteration As Integer
Private Sub TextBox1_Change()
Debug.Print Me.TextBox1.Value
Iteration = Iteration + 1
If Iteration = 10 Then
Unload Me
UserForm1.Show
End If
End Sub
Private Sub UserForm_Initialize()
Iteration = 0
End Sub
It still doesn't track changes on the second time round.
Okay, I fixed the issue.
I'm not sure why it works, need to look into it further, but if I add vbModeless to the .show function when initialising a userform it allows me to track changes on the second form.

excel macro pass data between two forms where focus is set

I have two forms.
form1 has four text boxes and a button for each textbox.
The button would setfocus on the textbox and bring up form2.
form2 is basicly a number keypad that also has a text box so that the user can select a number. That number will go in form2.textbox, which when changed will put the data in form1.textbox1.
The problem I'm having is how to tell form2.textbox to put data in form1.textbox2.
This is what my code looks like:
Public Sub textbox1_Click()
Me.textbox1.SetFocus
numbfrm.Show
End Sub
Private Sub textbox2_Click()
Me.textbox2.SetFocus
numbfrm.Show
End Sub
Private Sub textbox3_Click()
Me.txtactual.SetFocus
numbfrm.Show
End Sub
This is what is in the number form. It contains all of the numbers 1 to 10, but I just put the first three numbers here.
Private Sub Cmd1_Click()
TxtNumber.Value = TxtNumber.Value & "1"
End Sub
Private Sub Cmd2_Click()
TxtNumber.Value = TxtNumber.Value & "2"
End Sub
Public Sub TxtNumber_Change()
passnumber
End Sub
This is in a module:
Sub passnumber()
form1.textbox1.Value = numbfrm.TxtNumber
End Sub
I've been looking through the web to find an easy way to do that.
I tried puting in the module
Sub passnumber()
If form1.texbox1.foucs =true then
form1.textbox1.Value = numbfrm.TxtNumber
Else If form1.textbox2.foucs = true then
form1.texbox2.value =numbfrm.txtnumber
End sub
I have made a workaround for it, I put toggle buttons next to each box and when button is pressed it would mark it as true, and then I told it if that toggle is true it would use certain text box
Sub passnumber()
If form1.option1.value =true then
form1.textbox1.Value = numbfrm.TxtNumber
else
If form1.option2.value =true then
form1.textbox2.Value = numbfrm.TxtNumber
end if
end if
End sub

Excel VBA Set a Commandbutton Caption from a Userform Textbox

I have a button that is on an excel spreadsheet (not in a userform) and I have a userfrom that has a textbox on and would like it to, when I enter a name in the textbox on my userform for it to then set the Caption of my button that is on my excel sheet to what ever is entered in the textbox. I would like to know what code I need to use and where to insert that code?
Hope that make sense, please keep in mind I'm a newbie to this so this is why I am asking where to insert the code
Many thanks in advance
Code in your userform, assuming a textbox named TextBox1, could be like this:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(Me.Textbox1.text) > 0 then
ActiveSheet.Buttons("Button 1").Caption = Me.Textbox1.text
End If
End Sub
or if you want the caption to update as you type:
Private Sub TextBox1_Change()
If Len(Me.TextBox1.Text) > 0 Then _
ActiveSheet.Buttons("Button 1").Caption = Me.TextBox1.Text
End Sub
As you have used "CommandButton" (which is an ActiveX control) yet seemingly heading towards a Form control, I have gone with the 2 methods you will need:
ActiveX:
ActiveSheet.Shapes("YourButton").OLEFormat.Object.Object.Caption = "YourText"
Forms:
ActiveSheet.Shapes("YourButton").TextFrame.Characters.Text = "YourText"

about option buttons in User form

I have a question:
I need to create a user form that contain that usual OK and Cancel Buttons. It also should contain two sets of Options buttons, each set placed inside a frame. The captions on the first set should be basketball, baseball, football, the captions on the second set should be watch on TV and Go to games. I need to write the event handlers and code in a module so that when the program runs, the user sees the form. If the user makes a couple of choices and clicks OK, he should see a message like "Your favorite sport is basketball, and you usually watch on TV." If the user clicks Cancel, the message "Sorry you don't want to play" should appear.
I think I almost have it working, but I don't know why I cannot successfully execute the Macro.
My Code is :
Option Explicit
Private Sub CommandButton2_Click()
MsgBox ("sorry if you don't want to play")
End Sub
Private Sub commandbuttons_Click()
Dim optbasket As String, optbaseball As String, optfootball As String
Dim optwog As String, optgtg As String
Select Case True
Case optbasket
optbasket = True
Case optbaseball
optbaseball = True
Case optfootball
optfootball = True
End Select
If optwog Then
optwog = True
Else
optgtg = True
End If
btnok = MsgBox("you favorite sport is " & Frame1.Value & "you usually " & Frame2.Value & ",")
End Sub
Private Sub OptionButton1_Click()
End Sub
Private Sub btmcancel_Click()
End Sub
Private Sub btnok_Click()
End Sub
Private Sub Frame1_Click()
End Sub
Private Sub Frame2_Click()
End Sub
Private Sub optbaseball_Click()
End Sub
Private Sub optbasketball_Click()
End Sub
Private Sub optfootball_Click()
End Sub
Thank you very much!!!
There are a few things here:
You should name your buttons to be "OkButton" and "CancelButton" or something like that. They'll be easier to track later. Same thing for your radio buttons (baseball, basketball, etc.)
You don't need your select statement or your if statement
I don't think Frame1 and Frame2 have a .Value property you can call
Here is some sample code. You would add an object to your worksheet that could be clicked on. In this example I just inserted a rectangle object. form the Insert tab. Then in the UserForm code, I renamed the Ok Button to be OkButton and added the function OkButton_click. When it is clicked, I capture the values of the radio buttons. I named them baseball, basketball, and football accordingly as well as watch and go. If one of them is true, then I assign "game" which is a string I declared to be the appropriate title of the game. I did the same thing for whether the person likes to go to the game or watch it. Then I added the CancelButton_Click function to close the userForm.
Private Sub Rectangle1_Click()
UserForm1.Show
End Sub
Private Sub OkButton_Click()
Dim game as String, watchOrGo as String
If baseball Then game = "baseball"
If basketball Then game = "basketball"
If football Then game = "football"
If watch Then watchOrGo = "watch"
If go then watchOrGo = "go"
okbtn = Msg("Your favorite sport is " & game & ". You usually " & watchOrGo)
End Sub
Private Sub CancelButton_Click()
cnclbtn = Msg("Sorry you don't want to play")
Unload Me
End Sub
If you want the code in commandbuttons_Click() to run when you click OK, you need to put it in the click handler for the OK button: btnok_Click(); likewise for CommandButton2_CLick() and btncancel_Click().

Resources