I have a userform with a combobox that is populated with a list of client names that are stored in a hidden sheet. I am trying to make it so the user can select a name and click a button that brings up that client's profile (name address, phone number etc).
I am trying to get it to work for just the name before I do the other fields. When I view the profile the name label (which I named name_) is blank. ClientBox is the name of the combobox with the names which is on userform2, and ClientProfile is the name of the userform that has the name_ label.
The last 2 lines commented out are what I tried before I tried this method. Neither method works.
Sub ProfilePopulator()
Dim index As Integer, str As String
Application.ScreenUpdating = False
Sheets("Clients").Visible = True
Sheets("Clients").Select
Range("A1").Select
index = UserForm2.ClientBox.ListIndex
str = Cells(index + 1, 1)
ClientProfile.Name_.Caption = str
'ActiveCell.Offset(index, 0).Select
'ClientProfile.Name_.Caption = ActiveCell
End Sub
The problem wasn't actually in this part of the code (oops). It was in the code for the button that opens the ClientProfile userform. Before I had:
Private Sub CommandButton2_Click()
UserForm2.Hide
ClientProfile.Show
Call ProfilePopulator
End Sub
ProfilePopulator has to run before ClientProfile is shown, so now it looks like this and it works:
Private Sub CommandButton2_Click()
UserForm2.Hide
Call ProfilePopulator
ClientProfile.Show
End Sub
Related
I have been wondering about this one for a while now.
Let's say I have a formula in A1, Worksheet("Main")
=IF(B2="English";"Good morning";"Guten Morgan")
Then I have userform with code:
Private Sub TextBox1_Change()
ThisWorkbook.Worksheets("Main").Range("A1").Value = Me.TextBox1.Text
End Sub
Private Sub UserForm_Initialize()
Me.TextBox1.Text = ThisWorkbook.Worksheets("Main").Range("A1").Value
End Sub
How can I make it work so, that if I don't input anything into textbox, it will keep displaying functions result. If I will start to type text into textbox it will input my typed text to A1. Now if I open the userform it will overwrite A1 with the text in textbox and there will be no formula anymore. So if I change language in B2 result will no longer be interfaced into textbox.
Can be also some other approach with VBA. Everything is acceptable as long as logic will work.
I have tried to use textbox properly, something like linkedsource or similar, but it is crashing excel workbook sometimes. That's why I am trying to avoid it.
EDIT:
Thank you for suggestions! I have tried to implement this somehow but still don't get it. I am creating variable where I want to store result from ThisWorkbook.Worksheets("Other Data").Range("L49").Value then I would like to use it in Userform Me.TextBox14.Text to be displayed. Then once it is changed in Me.TextBox14.Text and Enter button has been pressed it should change also in ThisWorkbook.Worksheets("Other Data").Range("L49").Value.
Here is my current code I am trying to play with:
Private ProjectClass As String
Private Sub TextBox14_Enter()
ThisWorkbook.Worksheets("Other Data").Range("L49").Value = ProjectClass
End Sub
Private Sub UserForm_Initialize()
Me.TextBox14.Text = ProjectClass
End Sub
The TextBox.Enter event isn't fired when the user presses Enter, but when the control is entered - that is, when it gets the focus and a caret/cursor starts blinking inside it. You'll want to update the backing variable when the value is modified:
Private Sub TextBox14_Enter()
'runs when the control gets focus
End Sub
Private Sub TextBox14_Exit()
'runs when the control loses focus
End Sub
Private Sub TextBox14_Change()
'runs whenever the value changes (real-time)
End Sub
So in this case I'd go with the TextBox.Change event handler, and make it update the variable (not the worksheet):
Private ProjectClass As String
Private Sub TextBox14_Change()
ProjectClass = TextBox14.Text
End Sub
Now the problem is that the ProjectClass value needs to be accessible from outside the form, so that the caller can set an initial value. One way to do this could be to expose it as a property - one property (get+let) for each field you want to seed a value for:
Public Property Get ProjClass() As String
ProjClass = ProjectClass
End Property
Public Property Let ProjClass(ByVal value As String)
ProjectClass = value
ApplyModelProperties
End Property
Private Sub ApplyModelProperties()
TextBox14.Text = ProjectClass
'...
End Sub
Now from outside the form, at the call site (the code that's showing this dialog), you can seed the value from the worksheet, and the form never needs to know or care that a worksheet was involved:
With New UserForm1
.ProjClass = ThisWorkbook.Worksheets("Other Data").Range("L49").Value
.Show
MsgBox .ProjClass
End With
Note that because the value is exposed as a property, the calling code doesn't need to know about TextBox14 anymore.
I'm working on a basic userform project to learn and use it on my actual database for business.
a. I have created two userforms;
UserForm1 = Adding data to last row, show data on a listbox and deletes a row
UserForm2 = You can open it from on UserForm1 (I named "Edit")
b. The data that I have basically 4 columns and values;
ID || Name || Last Name || Date
When you click the "Edit" button on that and the row that you want to update data on listbox, it will open 3 textboxes with which you can change ID, Name, and Last Name. Additionally there is a "Save" button
I have a code that I made with an youtuber but what he did was he remains the ID column with Label and you can not change it, just shows the value.
What I want to do is I want to change all values (ID, Name, Last Name) not just Name and Last Name?
I tried and searched a lot but what all I did had not worked. Here is my code;
The Edit Button's code on UserForm1
Private Sub CommandButton4_Click()
UserForm2.Label4.Caption = ListBox1.List(ListBox1.ListIndex) ' I wanted to update this data also
UserForm2.TextBox1.Text = ListBox1.Column(1, ListBox1.ListIndex)
UserForm2.TextBox2.Text = ListBox1.Column(2, ListBox1.ListIndex)
UserForm2.Show
End Sub
Save Button Code on UserForm2
'When I make changes on this, It updates all rows with the entered values. This is original code that I want to make changes.
Private Sub CommandButton2_Click()
Dim i As Integer
For i = 2 To Range("A10000").End(xlUp).Row
If Cells(i, 1) = Label4.Caption Then
Cells(i, 2) = TextBox1.Text
Cells(i, 3) = TextBox2.Text
End If
Next i
MsgBox "Saved!", vbInformation
Unload Me
End Sub
So I need help finishing up a app lookup on elements from the periodic table. The idea if to have the user input the element in the combo box in Userform1 which has a drop down list. To then depending on the element chosen be directed to Userform2 that has a text box, and in that text box be given the atomic mass for each element . I need an example of how and where to write the code in either userfroms. This is what i have so far.
UserForm1 name is Element_LookUp
Userform2 Name is Element_LookUp_Result
Userform1 :
Private Sub CommandButton1_Click()
Unload Me 'Closes The Screen after the click
Element_LookUp_Result.Show 'Shows The result scrren in a new pop up
End Sub
Private Sub UserForm_Initialize()
' All 118 Elements will be shown on a drop down list
' The elements are in order ; autofill helps input the element.
ComboBox1.AddItem "Hydrogen"
ComboBox1.AddItem "Helium"
ComboBox1.AddItem "Lithium"
ComboBox1.AddItem "Beryllium"
ComboBox1.AddItem "Boron"
ComboBox1.AddItem "Carbon"
End Sub ' Not all the elements are listed below i just wanted to save time
UserForm 2 :
Private Sub UserForm_Terminate()
Unload Me 'Once the (X) is clicked the result screen goes away
Element_Lookup.Show 'Result scrren goes back to the input screen again
End Sub
As you're maintaining data from different userforms/modules, etc., I believe you're looking to have a global variable... At the top of the module for your userform (for the purpose of this post, will say UF1 is userform1, UF2 as userform2), you will put (not inside of a subroutine):
Public ElementName as String
Now, in your commandbutton1_click:
ElementName = Combobox1.Value
In UF2 initilize subroutine, you will put something like:
Textbox1.value = UF1.ElementName
The above example would be how to just pass the variable. You could also use ElementName such as:
Textbox1.Value = Application.Index(OutputRange,Application.Match(UF1.ElementName,LookupRange,0))
With that index/match you have used ElementName to find an output, where you would want the column for atomic mass.
I am currently working on a userform to create an order for users at a company to send to my dept.
At the moment i have come to a standstill as i am struggling to work out the following.
I have a combobox which has a list of products our business offers. Based on the selection i want to be able to add labels and textbox which require the user to enter data for example.
If this selection in the combo box then
Enter name, date required, location of user etc.
Also this needs to be specific to the combobox selection.
Any help would be much appreciated :)
UPDATE
Apologies, as i did not have any code for that function I did not add any Here is the code i have.
Private Sub CommandButton1_Click()
Windows("RFS User Form Mock.xlsm").Visible = True
End Sub
Private Sub LegendDefinition_Change()
LegendDefinition.Locked = True
End Sub
Private Sub RequestList_Change()
Dim i As Long, LastRow As Long
LastRow = Sheets("Definition").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Sheets("Definition").Cells(i, "A").Value = (Me.RequestList) Then
Me.DefinitionBox = Sheets("Definition").Cells(i, "B").Value
End If
Next
End Sub
Private Sub RequestList_DropButtonClick()
Dim i As Long, LastRow As Long
LastRow = Sheets("Definition").Range("A" & Rows.Count).End(xlUp).Row
If Me.RequestList.ListCount = 0 Then
For i = 2 To LastRow
Me.RequestList.AddItem Sheets("Definition").Cells(i, "A").Value
Next i
End If
End Sub
Sub UserForm_Initialize()
SiteList.List = Array("Birmingham", "Bristol", "Cardiff", "Chelmsford", "Edinburgh", "Fenchurch Street", "Glasgow", "Guernsey", "Halifax", "Homeworker", "Horsham", "Ipswich", "Jersey", "Leeds", "Leicester", "Lennox Wood", "Liverpool", "Manchester", "Peterborough", "Redhill", "Sunderland", "Madrid")
End Sub
Private Sub VLookUp_Change()
VLookUp.Locked = True
End Sub
When posting a question, you are expected to provide some code showing where you're standing trying to address the problem. Here's nevertheless a short demo that will give you a starting point.
Create a new UserForm and put a combobox, a label and a textbox on it; ensure they're named ComboBox1, Label1 and TextBox1, respectively.
Then, paste this code in the form's module:
Option Explicit
Private Sub ComboBox1_Change()
Dim bVisible As Boolean
'Only show the label and the textbox when the combo list index is 1, which corresponds to "Item 2".
'Note: bVisible = (ComboBox1.Text = "Item 2") would also work.
bVisible = (ComboBox1.ListIndex = 1)
Label1.Visible = bVisible
TextBox1.Visible = bVisible
End Sub
Private Sub UserForm_Layout()
'Populate the combo.
ComboBox1.AddItem "Item 1", 0
ComboBox1.AddItem "Item 2", 1
'Note: the code below could be removed by setting the corresponding
'design-time properties from the form designer.
ComboBox1.Style = fmStyleDropDownList
Label1.Visible = False
TextBox1.Visible = False
End Sub
Then press F5 to show the form. You'll notice that the label and textbox are only visible when the combo shows "Item 2". The visibility adjustment is performed within the ComboBox1_Change event handler.
If you plan on having numerous controls shown / hidden depending on your combo's value, you could group them into one or more Frame controls, and show / hide those frames instead.
I have a simple spreadsheet database where the second column of every row contains a hyperlink to a pdf. Each Hyperlink is unique for each row.
I want to add a command button to a user form that when clicked, allows the user to browse to the correct pdf and embed it as a hyperlink into the spreadsheet database in the correct cell. (cell 2 or every row in my case)
I know how to create a label that will follow the hyperlink once it is in the cell, but I am totally stuck on how to embed the hyperlink in the first place!
Hope someone can help.
I want to comment to Tim Williams but its too hard to format.
Thanks for your suggestion to record a macro, it gave me some clues as to what to do. I now have a working solution, but it is quite ugly.
Private Sub AddHyper_button_Click()
Dim URL As String
URL = Application.GetOpenFilename
Hyperlink_textbox = URL
End Sub
Private Sub Hyperlink_Label_Click()
Dim URL As String
Dim cellvalue As Variant
cellvalue = Hyperlink_textbox.Value
URL = cellvalue
On Error GoTo urlnotopen
ActiveWorkbook.FollowHyperlink Address:=URL, NewWindow:=True
Exit Sub
urlnotopen:
MsgBox "Cannot open " & URL
End Sub
This finds the file address on button click, posts it to a text box on the form (which is hidden) which can be then written to the correct cell. There is also a label on the form which will follow the hyperlink to the correct file.
You don't need to put the selection in a textbox, unless you really want to.
Private Sub CommandButton1_Click()
Dim x As String
x = Application.GetOpenFilename
Label1 = x
End Sub
Private Sub Label1_Click()
ActiveWorkbook.FollowHyperlink Address:=Me.Label1, NewWindow:=True
Unload Me
End Sub
Private Sub UserForm_Initialize()
With Label1
.ForeColor = vbBlue
End With
End Sub
Here is a short clip on this.
UserForm HyperLink