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
Related
I have a Userform with a multipage object. In one of pages there is a textbox object where I want to add a placeholder when Enter event is not detected. Problem is I'm not figure out how to make reference to textbox from Enter and Exit events due to I have a multipage structure.
I tried to adapt the following approach but it works only for a Userform without Multipage structure.
Got it here: Text box prompt text in Excel VBA
Private Sub TextBox1_Enter()
TB_enter ActiveControl.Name
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TB_exit ActiveControl.Name
End Sub
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)
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
Here is a video that shows how to do it when the Userform don't have a Multipage structure: https://www.youtube.com/watch?v=yJ4fnw1zmGU
Setting up properties Tag and Text of textbox I get the placeholder if use a properly Forecolor (&H8000000) but I can't make it works dynamically, that means: delete placeholder text when Enter event occurs and if lenght (Len) of the string inside textbox is zero when Exit event occurs must show the placeholder string again.
Any suggestions? Thanks in advance.
I have a userform which has multiple RefEdit controls. I need the user to select ranges from multiple sheets and the userform has to be complete before the rest of the code can run.
Issue: The activesheet is "Sheet1" when the userform is initiated. Each time I select a range on "Sheet2" and click into the next RefEdit the visible Excel sheet returns to "Sheet1". I'd like the sheet to remain on "Sheet2", since clicking between the sheets significantly increases the time it takes to select the data.
Because I need the userform to be completed before continuing with my code, using "vbModeless" doesn't appear to work.
I've tried to step through the userform events which appeared to be relevant but none were activated when I entered the RefEdit, selected the data, or left the RefEdit.
Thanks in advance for any help!
Edit: Using some input from the responses and doing some more research I think I've figured out the problem and a work around.
RefEdit events such as Change or Exit (I tried all of them I think) don't appear to trigger when a change occurs in the control. So I couldn't write code to manipulate the activesheet when I changed the control. A workaround found here: http://peltiertech.com/refedit-control-alternative/ uses a textbox and inputbox to simulate a RefEdit control and will actually trigger when changes are made! Code is below. To add other "RefEdit" controls you should repeat the code in the Userform_Initialize event for each control, then add another TextBox1_DropButtonClick and update TextBox1 to the name of the new control. In use when the control updates the workbook jumps to the previous activesheet and then returns the desired activesheet. Not as smooth as I'd like but much better than it was.
Code:
Private Sub CancelButton_Click()
Unload Me
End
End Sub
Private Sub OKButton_Click()
UserForm1.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
End
End Sub
Private Sub UserForm_Initialize()
Me.TextBox1.DropButtonStyle = fmDropButtonStyleReduce
Me.TextBox1.ShowDropButtonWhen = fmShowDropButtonWhenAlways
End Sub
Private Sub TextBox1_DropButtonClick()
Dim ASheet As String ' Active sheet
Me.Hide
'Use input box to allow user to select a range
On Error Resume Next
Me.TextBox1.Value = Application.InputBox("Select the range containing your data", _
"Select Chart Data", Me.TextBox1.Text, Me.Left + 2, _
Me.Top - 86, , , 0)
On Error GoTo 0
'Check if there is a sheet name - if the range selected is on the activesheet the output of the inputbox doesn't have a sheet name.
If InStr(1, Me.TextBox1.Value, "!", vbTextCompare) > 0 Then ' there is a sheet name
ASheet = Replace(Split(Me.TextBox1.Value, "!")(0), "=", "") ' extract sheet name
Else ' there is no sheet name
Me.TextBox1.Value = "=" & ActiveSheet.Name & "!" & Replace(Me.TextBox1.Value, "=", "") ' add active sheet name to inputbox output
ASheet = ActiveSheet.Name
End If
Worksheets(ASheet).Activate ' set the active sheet
Me.Show
End Sub
Have you tried something as simple as:
Sheets("Sheet2").Select
somewhere in the beginning of your form code ?
Since you haven't posted your code, it's hard to provide a good answer.
Hope this helps a little :)
This form module worked for me.
Private Sub CommandButton1_Click() 'Cancel Button
Unload Me
End Sub
Private Sub CommandButton2_Click() 'GO Button
Dim newSheet As Worksheet
abc = Split(RefEdit1.Value, "!")
cbn = abc(0)
Unload Me
Set newSheet = Worksheets(abc(0))
newSheet.Activate
End Sub
I have a userform on an Excel file called "userform":
Private Sub add1_Change()
End Sub
Private Sub add2_Change()
End Sub
Private Sub Calc_Click()
Result.Value = Val(add1.Value) + Val(add2.Value)
End Sub
This userform takes the value from the user and adds them together and shows the result in a textbox.
I want to create a new macro in another workbook named "input". The macro in this workbook should open the userform workbook, enter values in the textbox add1 and add2, then run the userform calculate button.
What I've tried thus far:
Set add1.value to extract a value from say, cell A1, and similarly for add2.value.
Then I created a macro on the input workbook to change the values in cells A1 and A2.
The problem from here is I don't know how to open the userform and click calculate.
Ideally, I would like a macro which opens the userform, enters the data and hits calculate then closes the userform - Rather than editing the userform itself.
You could add the 2 values in the UserForm in this way(its slightly different then you try to do it now):
You use your current code to open the UserForm:
Sub userform()
Workbooks.Open (ThisWorkbook.Path & "\userform.xlsm")
Application.Run "userform.xlsm!Calc"
End Sub
As shown above you don't assign any values this will happen in your userform.xlsm Workbook
Below is the code you put into the sub Initialize of your UserForm:
Private Sub UserForm_Initialize()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks("input.xlsx")
Set ws = wb.Worksheets("Input")
Dim i as Integer
Dim k as Integer
UserForm.add1.Value = ws.Range("A2").Value
UserForm.add2.Value = ws.Range("B2").value
UserForm.calc.Value = val(UserForm.add1.Value) + val(UserForm.add2.Value)
End Sub
As shown above calc is changed to a Textbox, therefor you don't need to click a button its directly done when the UserForm is loaded.
You could also use a Label instead of a Textbox.
the code would then change to:
UserForm.calc.Caption = Str( val(UserForm.add1.Value) + val(UserForm.add2.Value) )
#DirkReichel could you elaborate a bit more on this? I've added what you said, but say I wanted to change the value on the add1 textbox how would I call it? Right now, I have this: 'Sub userform() Dim a As Integer Dim b As Integer a = Cells(1, 2) b = Cells(2, 2) Workbooks.Open (ThisWorkbook.Path & "\userform.xlsm") Application.Run "userform.xlsm!Calc" End Sub' the calc macro just opens up the userform, I don't know how to actually "input" data or hit calculate
The answer:
I created 2 WB and just this simple code worked for me ... however: you may need to change the settings of the trust center.
Book1 Module: (the WB with Userform1 holding TextBox1 and CommandButton1)
Option Explicit
Public Function getUF()
Set getUF = UserForm1
End Function
Book2 Module:
Option Explicit
Public ExtUF As Variant
Sub the_UF()
Workbooks.Open "Book1.xlsm"
Set ExtUF = Application.Run("Book1.xlsm!getUF") 'get the Form
Debug.Print ExtUF.TextBox1.Value 'check old value
ExtUF.TextBox1.Value = "dada" 'change it
Debug.Print ExtUF.TextBox1.Value 'check for new value
ExtUF.CommandButton1.Value = True 'hit the button
ExtUF.Show 'show the form
Stop 'to check the userform
ExtUF.Hide 'hide it again
End Sub
Now just run the_UF and check for functionality. If everything does work, adopt it to your code the way you need it.
If you have any questions, just ask ;)
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"
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