I'm having a slight problem with some VBA coding on a multi-page userform I'm creating. I have some textboxes on each page of the userform, and I had code that I had been using with regular userforms to clear the textboxes provided in the following answer to another thread (https://stackoverflow.com/a/8921247/2477891)
The code looks like this:
Sub TB_enter(TB_name)
If Len(Me.Controls(TB_name).Tag) = 0 Then
Me.Controls(TB_name).Tag = Me.Controls(TB_name).Value
Me.Controls(TB_name).Value = vbNullString
End If
End Sub
Sub TB_exit(TB_name)
'When you click out of the textbox and no information has been entered, returns original text
If Len(Me.Controls(TB_name).Value) = 0 Then
Me.Controls(TB_name).Value = Me.Controls(TB_name).Tag
Me.Controls(TB_name).Tag = vbNullString
End If
End Sub
Along with the following code used for the textboxes to clear them:
Private Sub AdNtbx_Enter()
TB_enter ActiveControl.Name
End Sub
Private Sub AdNtbx_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TB_exit ActiveControl.Name
End Sub
My problem is that they are no longer working because they are on multi-pages, and the following line comes up with an error:
Me.Controls(TB_name).Value = vbNullString
Does anyone have any suggestions as to what could be the problem/solution?
I'd really appreciate it.
Thanks!
I believe this is what you are looking for :
Set Page1 = ThisWorkbook.VBProject.VBComponents("UserForm1").Designer.Controls("Multipage1").Pages("Page1")
Page1.textbox1.Text = vbNullString
You can edit ThisWorkbook, UserForm1 etc.. properties to suit your needs, but with this code you will be able clear textboxes on multipage object
Related
This is my first time posting so thank you all for your help in advance. This is for VBA in Microsoft Word.
I am working on a template for my company that involves many, many little userforms to autopopulate data. When a user creates a new document from the Template (new report), no macros are prompted at open, but they can click specific macros from the ribbon to edit specific variables (i.e. the "Users" macro calls a form for the addresses of each intended user of the report, the "Value" form controls all the currency numbers, etc.). The report itself has restricted editing so variables can't be deleted on accident by the user.
I want these forms to be cleared and free the FIRST time they are opened from the new report, but to still hold the values put in by the user throughout working on the report. That way they can go back and edit if they realize they put in the wrong name, etc, without having to fill out the whole form again.
Each form basically uses the same macro in Module1, and within the forms, each TextBox then includes Instructional Text that disappears when clicked. Example code below.
Macro (in Module1 - copied from Greg Maxey "Create & Employ a Userform")
Sub frmExample()
Dim oFrm As frmExample
Set oFrm = New frmExample
oFrm.Show
Unload oFrm
Set oFrm = Nothing
lbl_Exit:
Exit Sub
End Sub
And then here is an example of how almost every text box works in each form code. Keep in mind there are up to 20 or so textboxes per userform but this is condensed heavily.
Option Explicit
Private Sub UserForm_Initialize()
With TextBox1
.Forecolor = &HC0C0C0
.Font.Italic = True
.Text = "Instructional Text"
End With
cmdCancel.SetFocus
lbl_Exit:
Exit Sub
End Sub
Private Sub TextBox1_Enter()
With TextBox1
If .Text = "Instructional Text" Then
.Forecolor = &H80000008
.Text = ""
.Font.Italic = False
End If
End With
End Sub
Private Sub TextBox1_AfterUpdate()
With TextBox1
If .Text = "" Then
.ForeColor = &HC0C0C0
.Text = "Instructional Text"
.Font.Italic = True
End If
End With
End Sub
Private Sub cmdOkay_Click()
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
If TextBox1.Value = "Instructional Text" Then
MsgBox "Please fill out all required fields.", vbOkOnly + vbExclamation
If TextBox1.Value = "Instructional Text" Then
TextBox1.ForeColor = &HFF
End If
Else
oVars("varExample").Value = TextBox1.Value
ActiveDocument.Fields.Update
Set oVars = Nothing
Me.Hide
End If
End Sub
I think this has to be much more simple than I'm understanding right now but sometimes you just need somebody to tell you you're an idiot before you figure it out. I really, really like that instructional text thing and how it disappears at click so I don't want to lose that. I'm just thinking there could maybe be an additional Sub that works like UserForm_Initialize but instead only calls that when the user has already filled some things out. So after they click cmdOkay and want to go back to the form, all of their stuff is still there.
Any ideas? Thank you again!
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 have a userform with a multiple frames, all filled with multiple checkboxes. I've named the checkboxes to their corresponding Excel cells. Now I want to make VBA run on clicking any of these checkboxes on run time. I know I can do this by creating a click-sub for every individual checkbox, but there must be a cleaner way to do this.
So far I've tried to put this code in the userform_Click and userform_Mousedown events, but they don't run when I click the checkboxes. Does anyone have an idea how to do this?
Dim iControl As Control
For Each iControl In Me.Controls
If TypeName(iControl) = "CheckBox" Then
If iControl.Value = True And Range(iControl.Name).Value = "" Then
Range(iControl.Name).Value = Format(Now, "dd.mm.yyyy")
ElseIf iControl.Value = True And Range(iControl.Name).Font.Color = vbWhite Then
Range(iControl.Name).Font.Color = vbBlack
ElseIf iControl.Value = False And Range(iControl.Name).Value <> "" Then
Range(iControl.Name).Font.Color = vbWhite
End If
End If
Next
As SilentRevolution said - you need an event to fire when you click the button. You're just after a single procedure to fire all check box click events.
So:
Create a class module called cls_ChkBox.
In the class module you'll add the Click event code:
Option Explicit
Private WithEvents chkBox As MSForms.CheckBox
Public Sub AssignClicks(ctrl As Control)
Set chkBox = ctrl
End Sub
Private Sub chkBox_Click()
ThisWorkbook.Worksheets("Sheet1").Range(chkBox.Name).Value = Format(Now, "dd.mm.yyyy")
End Sub
Now you just need to attach the chkBox_Click event to each check box on your form. In your user form add this code:
Option Explicit
Private colTickBoxes As Collection
Private Sub UserForm_Initialize()
Dim ChkBoxes As cls_ChkBox
Dim ctrl As Control
Set colTickBoxes = New Collection
For Each ctrl In Me.Controls
If TypeName(ctrl) = "CheckBox" Then
Set ChkBoxes = New cls_ChkBox
ChkBoxes.AssignClicks ctrl
colTickBoxes.Add ChkBoxes
End If
Next ctrl
End Sub
Each check box is given its own instance of the class, which is stored in the colTickBoxes collection.
Open the form and the cell in Sheet1 will update to show the date depending on the name of the check box.
You need an event to run code, if there is no event, the macro cannot start. I don't know if there is a single event that triggers for any button or checkbox that is clicked.
If the code you want to execute is the same every time except the control, you could write a private sub in the userform module which is called for each event, the private sub can take an input between the () for example.
Private Sub CheckBox1_Click()
Call ExecuteCode(Me.CheckBox1)
End Sub
Private Sub CheckBox2_Click()
Call ExecuteCode(Me.CheckBox2)
End Sub
Private Sub CheckBox3_Click()
Call ExecuteCode(Me.CheckBox2)
End Sub
Private Sub ExecuteCode(IControl As Control)
If TypeName(IControl) = "CheckBox" Then
If IControl.Value = True And Range(IControl.Name).Value = "" Then
Range(IControl.Name).Value = Format(Now, "dd.mm.yyyy")
ElseIf IControl.Value = True And Range(IControl.Name).Font.Color = vbWhite Then
Range(IControl.Name).Font.Color = vbBlack
ElseIf IControl.Value = False And Range(IControl.Name).Value <> "" Then
Range(IControl.Name).Font.Color = vbWhite
End If
End If
End Sub
I just learned this today and thought it might help? Most of the questions, and responses for that matter, to questions regarding checkboxes, listboxes, etc. seem to not distinguish between those forms are inserted directly on the worksheet or are imbedded in a UserForm.
This may work for a checkbox - it works for a ListBox on a UserForm. If you want code to run after a selection, the code you must write has to be in module of the UserForm. I had no clue how to access. Once you have inserted a UserForm in your VBE, add the ListBoxes or whatever to the UserForm. Right click on the UserForm in the upper left project window and select "view code." Here is where you'll place the code, in my case a "change event" such that after a selection is made from the ListBox, other code is automatically run. Her for example:
Sub lstBoxDates_Change()
Dim inputString1 As String
inputString1 = Format(UserForm1.lstBoxDates.Value, "mm-dd-yyyy")
Call EnterDates(inputString1)
Unload Me
End Sub
To explain: Again this code is in the UserForm Module. I named my ListBox,
lstBoxDates. In my main code that I call - EnterDates, I use the variable name = inputString1. The value or date that I have selected from the ListBox is captured from the UserForm1 by UserForm1.lstBoxDates.Value - and I format that to a date, otherwise you see just a number. This is for only one selection.
Because I Dim here, no need to Dim in your main code. The sub for the main code
needs to accept the variable you are passing to it:
Sub EnterDates(inputString1)
This is very generalized but maybe something will click so you can get what you are after. I hope so, for I've worked on this a full two days!!
In VBA for excel, I have a userform then I want this to show only for 1 instance. Even if the user re-open it, it won't open again. Is there any code for it? well, I'm also using this code for my login:
Private Sub UserForm_Initialize()
User.Caption = Environ("Username")
End Sub
I'm thinking if i can use this code in my problem. Hoping for a quick response. thanks guys, you're awesome!
Yes, it's possible.
You have to add new sheet. In a cell A1 type 0 (zero), then hide it. In a code which calls UserForm, use this:
Sub ShowMyForm()
If ThisWorkbook.Worksheets("HiddenSheet").Range("A1")=0 then MyUserForm.Show
End Sub
In a form:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
ThisWorkbook.Worksheets("HiddenSheet").Range("A1")=1
ThisWorkbook.Save()
DoEvents
End Sub
If you don't want to add an extra sheet just to store one bool, you can set a custom document property like this:
Private Sub Workbook_Open()
On Error Resume Next
Dim test As Boolean
test = Me.CustomDocumentProperties("UserFormShown").Value
If Err.Number = 0 Then Exit Sub
UserForm1.Show
Me.CustomDocumentProperties.Add "UserFormShown", False, msoPropertyTypeBoolean, True
End Sub
If the property hasn't been set yet it will throw an error, so trapping an error lets you know if you've set the property (and shown the form).
Working in VBA for excel 2010. This is my first time working with VBA and userforms. Right now I have a barebones userform "UserForm1" trying to sort this issue out. It consists of two radio buttons "OptionButton1" and "OptionButton2" belonging to GroupName "WK" and two textboxes "TextBox1" and "TextBox2".
When I run the userform, I want "OptionButton1" to be selected and the subsequent if/then statements to be run. However, right now I cannot get it to do this. My code:
Public Sub UserForm1_Initialize()
UserForm1.Show
Me.OptionButton1.Value = False
Me.OptionButton1.Value = True
MsgBox ("dia locked")
Me.TextBox1.Value = "blah"
End Sub
Public Sub UserForm1_Activate()
End Sub
Public Sub OptionButton1_Click()
If Me.OptionButton1.Value = True Then
MsgBox ("dia locked")
Me.TextBox1.Value = "blah"
End If
End Sub
Private Sub TextBox1_Change()
End Sub
When I run the form, nothing happens and "OptionButton1" is false. If I click it, the message box displays and the textbox displays the text. It just won't do it on startup. I have tried going into the optionbutton properties and setting 'value' to true, which makes it true on startup, but the messagebox still doesn't display and the textbox is blank until I click it.
...please help. Thank you.
I figured it out.
I suddenly found the dropdowns. Apparently I should have put Userform_Initialize() instead of UserForm1_Initialize(), and instead of OptionButton1_Click() I put the code into OptionButton1_Change() which ran the subsequent initialization sequence.
You guys/gals are awesomesauce. I have learned everything from reading your threads. Thank you!