I need to create a UserForm with a SpinButton on it. The user selects a value and when done, clicks OK (to pass the selected value to another procedure).
This is how it looks like:
Now, how do I display the selected value in the SpinButton on a Label or something else that shows the user which value she selected?
You need to define your SpinButton in the UserForm_Initialize and add a line in SpinButton_Change to update the Label :
Private Sub UserForm_Initialize()
With SpinButton1
.Min = 0 'Min Value
.Max = 100 'Max Value
'Specify the value of the change when the spin button is clicked
.SmallChange = 5 '(Default = 1)
End With
End Sub
Private Sub SpinButton1_Change()
Label1.Caption = SpinButton1.Value
End Sub
Simply use the SpinButton1_Change event. This will update the label realtime when the you click the spinbutton up/down arrow.
In the userform code area, simply paste this code
Private Sub SpinButton1_Change()
Label1.Caption = SpinButton1.Value
End Sub
Related
I have multiple text boxes under each other. I would like them all to be hidden except the first one. If the first box has text in it then the second box should display. If the second box has text in it then the third box will display.
You can use the AfterUpdate event of the textbox, here for the first:
Me!TextBox2.Visible = Not IsNull(Me!TextBox1.Value)
and so on.
Given that your textboxes are named txtBox1 to txtBox6 you can place this procedure in the AfterUpdate event of each of them.
The procedure also sets the value of invisible textboxes to Null. If you don't want that, just comment the regarding line of code.
Private Sub SetTextBoxes(ByVal startWithTextBoxNr As Long)
Const TEXTBOX_COUNT As Long = 6
Const COMMON_NAME As String = "txtBox"
Dim index As Long
For index = startWithTextBoxNr + 1 To TEXTBOX_COUNT
With Me(COMMON_NAME & index)
.Visible = Not IsNull(Me(COMMON_NAME & index - 1).Value)
If Not .Visible Then .Value = Null
End With
Next index
End Sub
The event handlers would look like this:
Private Sub txtBox1_AfterUpdate()
SetTextBoxes 1
End Sub
Private Sub txtBox2_AfterUpdate()
SetTextBoxes 2
End Sub
Private Sub txtBox3_AfterUpdate()
SetTextBoxes 3
End Sub
Private Sub txtBox4_AfterUpdate()
SetTextBoxes 4
End Sub
Private Sub txtBox5_AfterUpdate()
SetTextBoxes 5
End Sub
Additionally you could add SetTextBoxes to the Load event of the form to initialize the controls once for the first textbox:
Private Sub Form_Load()
SetTextBoxes 1
End Sub
I have an excel vba form which has some tabs(pages).What I need is, if I double click on label, it should be editable. same is the case for the tabs.
I tried to add some things into the double click function, but not showing any change.
Labels are not editable by the end user
you may adopt this workaround
'change "Label1" occurrences to your actual label name
Private Sub Label1_DblClick()
Me.Label1.Caption = Application.InputBox("enter label text", "label editing", "")
End Sub
You can have a TextBox and disable it. The side effect is you will have textbox edit cursor over the label.
Have two subroutines, one called Labelize and another Textboxize. The former locks the textbox, makes it tranparent, and sets the 3D effect to flat. The latter unlocks it, makes it opaque, and sets the 3D to sunken (the default).
' Makes a textbox look like a label
Private Sub Labelize(txtbox As MSForms.TextBox)
txtbox.Locked = True
txtbox.BackStyle = fmBackStyleTransparent
txtbox.SpecialEffect = fmSpecialEffectFlat
End Sub
' Makes a textbox look like a textbox
Private Sub Textboxize(txtbox As MSForms.TextBox)
txtbox.Locked = False
txtbox.BackStyle = fmBackStyleOpaque
txtbox.SpecialEffect = fmSpecialEffectSunken
End Sub
In the form initialize event set call the Labelize method passing in your textboxes.
Private Sub UserForm_Initialize()
Labelize Me.txtExample
End Sub
In the double-click event per textbox, call the Textboxize method passing in your textbox again.
Private Sub txtExample_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Textboxize Me.txtExample
End Sub
Then in each KeyDown event, check if the key pressed was enter then call the Labelize method.
Private Sub txtExample_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then Labelize Me.txtExample
End Sub
I have an Excel user form with multiple multi-line text boxes that need scroll bars. When I click in a textbox to scroll, it starts at the bottom of the text. When this happened with just one textbox on the user form I used this:
Userform1.TextBox1.SelStart = 0
and everything worked. If I try to use that on multiple text boxes in the same form, the scroll bar never appears for any of the boxes. Does anyone know how to fix this?
Update:
Found a quirk that my help narrow down the problem: with multiple textboxes, selstart=0 works on the first box, but then I need a much bigger number for the selstart of the next textbox. Example. The code below puts the scrollbar at the top of both textboxes. The form is shown through a double click on sheet 1 and the the values of the textboxes are create in the initialize sub.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
UserForm1.Show
End Sub
--------------
Private Sub UserForm_Initialize()
UserForm1.TextBox1.Value = Sheets("Sheet1").Cells(1, 1).Value
UserForm1.TextBox1.SelStart = 0
UserForm1.TextBox2.Value = Sheets("Sheet1").Cells(2, 1).Value
UserForm1.TextBox2.SelStart = 200
End Sub
But I could only find that textbox2 had to start at 200 through guess and check. I dont know how to determine where that textbox should start.
I had a breakthrough. If I use SetFocus then do selstart= 0 everything seems to work.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
UserForm1.Show
End Sub
Private Sub UserForm_Initialize()
UserForm1.TextBox1.Value = Sheets("Sheet1").Cells(1, 1).Value
UserForm1.TextBox1.SetFocus
UserForm1.TextBox1.SelStart = 0
UserForm1.TextBox2.Value = Sheets("Sheet1").Cells(2, 1).Value
UserForm1.TextBox2.SetFocus
UserForm1.TextBox2.SelStart = 0
End Sub
I currently have a userform that has a combobox and a listbox. Both contain the same list of items (the combobox has an extra null value). If a user selects an item in the combobox, then the same item will be selected in the list box.
The problem that I am having is coming from the combobox autocompleting when the user tries typing a value instead of choosing one from the combobox. When the user types a value, it will autocomplete what they typed to a value within the combobox. (If I type "8" then the combobox will autocomplete that to "8184123".)
If I set MatchEntry to 2 - fmMatchEntryNone, then the combobox does not autocomplete. However, the combobox does not select a value based on what the user has typed.
Is there any way to stop the combobox from autocompleting while keeping letting MatchEntry stay at 1 - fmMatchEntryComplete? Or is there anyway to implement fmMatchEntryComplete only when the value that the user enters is exactly equal to a value in the list of the combobox?
you can have ComboBox methods and properties work for you
in the form calling sub place:
Sub main()
' code that preceeeds the userform loading...
With UserForm1
'other code to set some userform or userform controls properties...
.ComboBox1.MatchEntry = fmMatchEntryNone ' <--| set this just before showing userform
.Show
End With
Unload UserForm1
' code that follows the userform closing...
End Sub
in the userform code pane place this function:
Function CheckCB(cboBox As MSForms.ComboBox) As Boolean
With cboBox
.Text = .Value '<-- This is the "trick": "refresh" the combobox text
CheckCB = .MatchFound
If Not CheckCB Then
MsgBox "Invalid entry", vbCritical
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Function
despite MSDN online doc the refreshing of .Value actually has MatchEntry and MatchRequired work on it even if MatchEntry is set to fmMatchEntryNone
then you have to call CheckCB() function to prevent leaving userform until your combobox has been entered a valid value
for instance you could place it in any "exit" button click event handler
Private Sub CommandButton1_Click()
If Not CheckCB( ComboBox1 ) Then Exit Sub '<-- if ComboBox check failed then exit
' otherwise let code run ...
End Sub
or, if you wanted the user not to enter any other control until your combobox has a valid value, you must act similarly for every other userform control event handler, i.e. placing If Not CheckCB Then Exit Sub at their beginning
Try using Form1.ComboBox1.AutoWordSelect = True
Custom Autocomplete
Turn off match entry
ComboBox1.MatchEntry = fmMatchEntryNone
When the user types iterate over the combo's list. It there is only 1 possible match, select it.
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim i As Integer, matchIndex As Integer
matchIndex = -1
For i = 0 To ComboBox1.ListCount - 1
If InStr(1, ComboBox1.List(i), ComboBox1.Value) Then
If matchIndex = -1 Then
matchIndex = i
Else
Exit Sub
End If
End If
Next
If matchIndex > -1 Then ComboBox1.ListIndex = matchIndex
End Sub
I am writing a code in Ms-Word vba.
I made two userform say userform_1 and userform_2
userform_1 contains two buttons and two textboxes.
userform_2 is a calendar userform.
I wrote a code for showing a calendar useform on each button click event.
Now, I want to write a code such that when user clicks on 1st button the selected calendar values will display in textbox1 and when user clicks on 2nd button the selected value displayed in the 2nd textbox. But it displays the same value in textboxes.
Please let me know this how could i achieve this...
Private Sub CmB_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Main.Event_DblClick = True
Dim SampleDate As Date
frmAE_Tool.TextBox1.Text = Me.Value
frmAE_Tool.TextBox2.Text = Me.Value
End Sub
What you can do is to create a Sub in your userform_2 which going to determine the output of the calendar.
Userform_2 :
Declare a Private variable like below :
Private mTextBox As Control
(This textbox would be the output textbox of your calendar)
Add the following Sub :
Public Sub SetOutput(pTextBox As Control)
Set mTextBox = pTextBox
End Sub
(This Sub would allow to decide which output you want to use)
Modify your CmB_DblClick Sub like this :
Private Sub CmB_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'Others things that I didn't understand
mToolbox = Me.Value
End Sub
Userform_1 :
For the first button, modify the Click Sub like this :
Private Sub Btn1_Click
[...]
YourCalendarForm.SetOutput Textbox1
End Sub
And for the second, like this :
Private Sub Btn2_Click
[...]
YourCalendarForm.SetOutput Textbox2
End Sub