How to get VBA hyperlinks working? - excel

I tried to use hyperlinks but it is not doing anything at all. I would like to know what I need to change to use hyperlinks please.
Sub RDB_Worksheet_To_PDF()
Dim FileName As String
Dim PONumber As String
Dim FolderPath As String
PONumber = Sheets("Purchase Order with Sales Tax").Cells(8, 6).Value
FolderPath = "Z:\1.PRODUCTION\1. PURCHASING\PO H 2012\"
If ActiveWindow.SelectedSheets.Count > 1 Then
MsgBox "There is more then one sheet selected," & vbNewLine & _
"be aware that every selected sheet will be published"
End If
'Call the function with the correct arguments
FileName = RDB_Create_PDF(ActiveSheet, FolderPath & PONumber, True, True)
If FileName <> FolderPath & PONumber Then
'Ok, you find the PDF where you saved it
'You can call the mail macro here if you want
MsgBox "Sweet! The PO has been saved as a PDF." & vbNewLine & _
"Click on the PO Number in the PO Number WorkSheet to view."
Else
MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
"Microsoft Add-in is not installed" & vbNewLine & _
"There is no PO number selected" & vbNewLine & _
"The path to Save the file in is not correct" & vbNewLine & _
"You didn't want to overwrite the existing PDF if it exist"
End If
Sheets("PO Number").Select
Range("A1").Select
Set smvar = Cells.Find(What:=PONumber, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not smvar Is Nothing Then smvar.Activate
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
FolderPath & PONumber & ".pdf"
Sheets("Purchase Order with Sales Tax").Select
End Sub

This works for me - adapt to suit.
Sub Tester()
Dim shtPO As Worksheet, smvar As Range
Set shtPO = Sheets("Sheet1")
Set smvar = shtPO.Cells.Find(What:="Hello", _
After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart)
If Not smvar Is Nothing Then
shtPO.Hyperlinks.Add Anchor:=smvar, Address:="C:\folder\folder2\file.pdf"
End If
End Sub
No need to select/activate anything.

Not sure if I understood the issue very clearly...
But may you should select the sheet first then create the Hyperlink...
Sub createHyperLink()
Sheets("Purchase Order with Sales Tax").Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, _
Address:="example.pdf", _
ScreenTip:="This is the link file", _
TextToDisplay:="Linked PDF"
End Sub
This would create the link in the default selected cell in sheet named "Purchase Order with Sales Tax"

Related

Search Value in InputBOX, Look for Value, and Show Values in MSGBOX

I am still new to VBA. I am working on an excel sheet, and I want to look up data using InputBox and get results from the spreadsheet that I have. Here's an example:
Sheet Display:
Names
Occupation
IDs
James
Engineer
e2134
Linda
Programmer
f2123
Input Box:
"Enter IDs:"
e2134
Result:
Message Box will show:
IDs: e2134
Name: James
Occupation: Engineer
I did some code, but I can't access it from home (it's on my work computer).
I want to know what code can I use so VBA can collect the data on the same row (Name and Occupation) and display it in a MessageBox. Or there could be a better way to do that.
You may try below code-
Sub SearchNreturn()
Dim Rng As Range
Dim strID As String
strID = InputBox("Enter ID")
Set Rng = Sheets("Sheet1").Range("C:C").Find(What:=strID, _
After:=Cells(1, 3), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
MsgBox "ID: " & Rng.Value & vbNewLine & _
"Occupation: " & Rng.Offset(0, -1) & vbNewLine & _
"Name: " & Rng.Offset(0, -2)
Else
MsgBox "Nothing found."
End If
End Sub

Print Multiple Selections in Excel into 1 Single page PDF

I am a newbie to Excel VBA, and I get this code from https://trumpexcel.com/convert-excel-to-pdf/ to print selection into PDF.
My only concern at the moment is, how to make all of my uncontinuous selections printed into 1 single PDF. If anybody can help, I would be very grateful for that.
For example, in the photo below, you can see that if I select from the first row to the last row, then when I print Selection into PDF, it will be converted into 1 single page. But for example, I only want to print the Product 1 and Product 3 (skipping product 2), then I will only be able to print them into 2 separated pages. How can I make this into 1 page is my question.
Code:
Sub PrintSelectionToPDF()
Dim ThisRng As Range
Dim strfile As String
Dim myfile As Variant
If Selection.Count = 1 Then
Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
Else
Set ThisRng = Selection
End If
'Prompt for save location
strfile = "Selection" & "_" _
& Format(Now(), "yyyymmdd_hhmmss") _
& ".pdf"
strfile = ThisWorkbook.Path & "\" & strfile
myfile = Application.GetSaveAsFilename _
(InitialFileName:=strfile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and File Name to Save as PDF")
If myfile <> "False" Then 'save as PDF
ThisRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Else
MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected"
End If
End Sub

Make a list in VB Excel based on cells in the same row

I have a worksheet with names and addresses of people. I want to make a Userform that finds a person in Column 1 and then output the data from the following cells in the same row as a list. So the output would look like this:
John
Time Squares 12
New York
0123123123
I manage to find the cell and output the information, but I can't find a way to find and add the info in the following cells in the same row.
Dim FindString As String
Dim Rng As Range
FindString = txtSearch
If Trim(FindString) <> "" Then
With Sheets("servicepartner").Range("A:A")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
MailFormat.Text = Rng.Value & vbNewLine
Else
MsgBox "Nothing found"
End If
End With
End If
Anyone have a suggestion on how to approach this issue? Thanks!
I solved this by setting up a variable (StringRow) with the rownumber of the search result. Then output Cells( StringRow, "B").Value & vbNewLinge & Cells( StringRow, "C") $ etc. etc. Works fine!
The code now looks like this:
Dim FindString As String
Dim Rng As Range
'This variable will find the Row number
Dim StringRow As Long
FindString = txtSearch
If Trim(FindString) <> "" Then
With Sheets("servicepartner").Range("A:A")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
'Output Results (i shorted this to not give you a headache)
StringRow = Rng.Row
MailFormatKlant.Text = Rng.Value & vbNewLine & _
Sheets("servicepartner").Cells(StringRow, "B").Value & _
vbNewLine & Sheets("servicepartner").Cells(StringRow, "D").Value & _
" te " & Sheets("servicepartner").Cells(StringRow, "C").Value & _
vbNewLine & Sheets("servicepartner").Cells(StringRow, "F").Value & _
vbNewLine & Sheets("servicepartner").Cells(StringRow, "G").Value & _
Else
MsgBox "Nothing found"
End If
End With
End If
I hope someone finds this helpful :)
Here's what I was suggesting.
MailFormatKlant.Text = Rng.Value & _
vbNewLine & Rng.Offset(0,1).Value & vbNewLine & _
vbNewLine & Rng.Offset(0,3).Value & vbNewLine & _
vbNewLine & " te " & Rng.Offset(0,2).Value & vbNewLine & _
vbNewLine & Rng.Offset(0,5).Value & vbNewLine & _
vbNewLine & Rng.Offset(0,6).Value

Two TextBox one Answer VBA6

In my UserForm, I have two Text Box. I want to be able to FIND by either Text Box, but use only one or the other. If both are empty I want a MSGBOX telling user to enter information. I can make my code do either/or text box entry but not two textbox where the user skips TextBox1 or does not entry anything.
Here is my code.....
Private Sub OkayCommandButton_Click()
Worksheets("Parts List").Select
Application.ScreenUpdating = False
Range("A2").Select
PN = PartNumber.Value
KN = KanbanNumber.Value
If PartNumber = vbNullString Then
MsgBox "Please enter a Part Number"
PartNumber.SetFocus
Else
Cells.find(What:=PN, After:=Range("A2"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
End If
If Kanban = vbNullString Then
MsgBox "Please enter a Kanban Number"
PartNumber.SetFocus
Else
Cells.find(What:=KN, After:=Range("A1"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
End If
PartInformation.Caption = _
"Part Number" & vbTab & ActiveCell & vbCrLf & _
"Kanban" & vbTab & vbTab & ActiveCell.Offset(0, 45) & vbCrLf & _
"Part Name" & vbTab & ActiveCell.Offset(0, 1) & vbCrLf & _
"Supplier" & vbTab & vbTab & ActiveCell.Offset(0, 2) & vbCrLf & _
"Next Process" & vbTab & ActiveCell.Offset(0, 3) & vbCrLf & _
"Qty in Tote" & vbTab & ActiveCell.Offset(0, 44) & vbCrLf & _
"PC Location" & vbTab & ActiveCell.Offset(0, 46)
PartInformation1.Caption = "Line " & ActiveCell.Offset(0, -1)
End Sub
You could check first if both TextBox-Elements are empty and then give a message to the user. If that check fails either one or both Text-Box-Elements contain text. You have a conflict there, since you only want to use one TextBox-Element for the search if both textboxes contain a search string. In this case you have to give one of the textboxes priority (the first one you check then wins):
This is only a snippet of your text:
PN = PartNumber.Value
KN = KanbanNumber.Value
If ((PartNumber = vbNullString) And (KanbanNumber = vbNullstring)) Then
' Both textboxes are empty, message box opened and focus to part number
MsgBox "Please enter a Part Number or Kanban Number"
PartNumber.SetFocus
Else
' One or more textboxes contain a search string
If Not (PartNumber = vbNullString) Then
'Part number is given, run search
Cells.find(What:=PN, After:=Range("A2"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Else
' Part Number is not given
' Since we checked that at least one textbox contains text
' the Kanban Number must be set if Part Number has not been set
Cells.find(What:=KN, After:=Range("A1"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
End If
End If

If a FIND fails to find input VBA6

It has just dawned on me What if the user puts in something not in the database (excel spreadsheet)? I have poured over all these posted fixes and none seem to meet my needs. Would someone look over my code Please.
Private Sub OkayCommandButton_Click()
Worksheets("Parts List").Select
Application.ScreenUpdating = False
Range("A2").Select
PN = PartNumber.Value
KN = KanbanNumber.Value
If ((PartNumber = vbNullString) And (KanbanNumber = vbNullString)) Then
' Both textboxes are empty, message box opened and focus to part number
MsgBox "Please enter a Part Number or Kanban Number"
PartNumber.SetFocus
Else
' One or more textboxes contain a search string
If Not (PartNumber = vbNullString) Then
'Part number is given, run search
Cells.find(What:=PN, After:=Range("A2"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Else
' Part Number is not given
' Since we checked that at least one textbox contains text
' the Kanban Number must be set if Part Number has not been set
Cells.find(What:=KN, After:=Range("A1"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
End If
End If
PartInformation.Caption = _
"Part Number" & vbTab & ActiveCell & vbCrLf & _
"Kanban" & vbTab & vbTab & ActiveCell.Offset(0, 45) & vbCrLf & _
"Part Name" & vbTab & ActiveCell.Offset(0, 1) & vbCrLf & _
"Supplier" & vbTab & vbTab & ActiveCell.Offset(0, 2) & vbCrLf & _
"Next Process" & vbTab & ActiveCell.Offset(0, 3) & vbCrLf & _
"Qty in Tote" & vbTab & ActiveCell.Offset(0, 44) & vbCrLf & _
"PC Location" & vbTab & ActiveCell.Offset(0, 46)
PartInformation1.Caption = "Line " & ActiveCell.Offset(0, -1)
End Sub
The specification for Range.Find is that it returns Nothing if the search term doesn't exist in the searched area. Nothing is a special value which you can test for.
To deal with search terms which don't exist, try adding Dim rFindResult As Range immediately after the Private Sub... line and then alter the Cells.Find statements like this:
Set rFindResult = Cells.Find(What:=KN, After:=Range("A1"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If (rFindResult Is Nothing) Then
` display MesgBox, reset focus appropriately, exit sub
Else
rFindResult.Activate
End If
Siddarth Rout's point about specifying which sheet to use rather than implicitly relying on the active sheet is well worth heeding. Also, in the case where neither part number nor kanban number is specified, there should probably be an Exit Sub after setting the focus to part number

Resources