I have two textboxes in a userform.
I need that if the user has entered some number in one textbox and then places the cursor in the other textbox, the first text box data is deleted and vice versa.
I was able to lock the other textbox if one has data in it, as a workaround.
I tried to replicate the lock method for deleting the value, but it does not work.
Sub checkTB()
If ConversionForm.Controls("UnitFromEntry").Text <> "" Then
ConversionForm.Controls("UnitToEntry").Locked = True
Else
ConversionForm.Controls("UnitFromEntry").Locked = True
End If
End Sub
I call this sub in the before update event of the text box.
Private Sub UnitFromEntry_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Call checkTB
End Sub
Try this
Private Sub UnitToEntry_Enter()
UnitToEntry.Locked = False
UnitFromEntry.Locked = True
UnitFromEntry.Text = ""
End Sub
Private Sub UnitFromEntry_Enter()
UnitFromEntry.Locked = False
UnitToEntry.Locked = True
UnitToEntry.Text = ""
End Sub
Related
I have made a combo box within a userform:
private sub Userform_Activate
cmbLocation.AddItem "Field"
cmbLocation.AddItem "Remote"
cmbLocation.AddItem "Other"
end sub
when a user selects Other I'd like a text box to populate for free form text input. When the user does this, I'd like that text to be the .value that is populating into the worksheet table.
is this even possible?
Create a textbox and set it to .visible = False when the userform is activated. Then using an if statement you can output the data from either the combobox or the textbox.
Private Sub UserForm_Activate()
Me.txtLocation.Visible = False
Me.cmbLocation.AddItem "Field"
Me.cmbLocation.AddItem "Remote"
Me.cmbLocation.AddItem "Other"
End Sub
Private Sub cmbLocation_Change()
If Me.cmbLocation.Value = "Other" Then
Me.txtLocation.Visible = True
Else
Me.txtLocation.Visible = False
End If
End Sub
Private Sub CommandButton1_Click()
If Me.txtLocation.Visible = True Then
ThisWorkbook.Sheet1.Range("A1").Value = Me.txtLocation.Value
Else
Thisworkbook.Sheet1.Range("A1").Value = Me.cmbLocation.Value
End If
End Sub
Modify sheet and range as necessary
I'm having an issue with the way a listbox behaves on an Excel form. Steps to reproduce the issue:
Create a user form with one listbox control
Use following code with this user form:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.ListBox1.Locked = True
Me.ListBox1.Locked = False
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
When the form is first shown, I am able to navigate the list box normally, using arrow keys and page keys. However, after the double-click event is triggered, all keyboard navigation has no effect, including tabbing to other controls (if they're on the form). Clicking on the listbox does seem to work, and the focus outline is shown correctly, but there's something wrong with the way the focus is handled after the listbox is locked and then unlocked. What is going on?
Using Office 2013 32-bit edition.
I managed to replicate this issue, and setting the focus somewhere else before locking and unlocking the listbox worked for me:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox1.SetFocus 'or some other control.
Me.ListBox1.Locked = True
Me.ListBox1.Locked = False
Me.ListBox1.SetFocus
End Sub
2 solutions I tested that allows you to use Arrow Keys to navigate.
Given that there isn't a single click event handler, try call the Single Click Event after the DblClick (works all the time):
Private Sub ListBox1_Click()
Debug.Print "ListBox1_Click()"
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "ListBox1_DblClick()"
With Me.ListBox1
.Locked = True
.Locked = False
End With
ListBox1_Click
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
Setting the Cancel = False at the end of DblClick. (Sometimes does not work!)
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "ListBox1_DblClick()"
With Me.ListBox1
.Locked = True
.Locked = False
End With
Cancel = False
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
Changing the zoom in the sheet that has the listbox worked for me.
Private Sub Worksheet_Activate()
Dim temp As Double
Application.ScreenUpdating = False
'Change worksheet zoom setting for the active window
temp = ActiveWindow.Zoom
ActiveWindow.Zoom = temp + 10
ActiveWindow.Zoom = temp
Application.ScreenUpdating = True
End Sub
I was wondering if some one can help me. I need to increment by one a number in a cell and then clean a few textbox but after the printing. There is an beforeprint event but doesnt do what I want becuase before the sheet is printed all the data is cleaned, obviously (I apologize for my english) Is there a way to do what I need or a better practice for it. Thank you.
Private Sub Workbook_BeforePrint(Cancel As Boolean)
[I6] = [I6] + 1
[C11] = ""
[C12] = ""
Range("B16:B27").ClearContents
Range("C16:C27").ClearContents
Range("D16:D27") = ""
Range("H16:H27").ClearContents
End Sub
To act AFTER printing, you need to print the document via VBA's ActiveSheet.PrintOut inside of the BeforePrint event and set Cancel to true, so it doesn't run its own print too.
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Cancel = True
Application.EnableEvents = False
ActiveSheet.PrintOut
Application.EnableEvents = True
[I6] = [I6] + 1
[C11] = ""
[C12] = ""
Range("B16:B27").ClearContents
Range("C16:C27").ClearContents
Range("D16:D27") = ""
Range("H16:H27").ClearContents
End Sub
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
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