PDA

View Full Version : Vba search function not pulling links through.



Vikungr
12-10-2020, 06:41 AM
I am currently working on a document for work whereby I used vba to create a basic search return system. The issue I am having is one of my return columns contains links. When the search is complete the links are pulled through but are not active hyperlinks.

Is there some way to pull them through as links not just text? Code is provided for the search function below. Colun with links is x,5 in the code.


Sub SearchAFCS()

Dim erow As Long
Dim ws As Worksheet
Dim lastrow As Long
Dim count As Integer

lastrow = Sheets("AFCS2").Cells(Rows.count, 1).End(xlUp).Row

count = 0

Dim c As Long

d = 12

For x = 2 To lastrow
If Sheets("AFCS2").Cells(x, 1) = Sheets("AFCS").Range("F6") Then
Sheets("AFCS").Cells(d, 4) = Sheets("AFCS2").Cells(x, 1)
Sheets("AFCS").Cells(d, 6) = Sheets("AFCS2").Cells(x, 2)
Sheets("AFCS").Cells(d, 8) = Sheets("AFCS2").Cells(x, 3)
Sheets("AFCS").Cells(d, 9) = Sheets("AFCS2").Cells(x, 4)
Sheets("AFCS").Cells(d, 14) = Sheets("AFCS2").Cells(x, 5)

d = d + 1

count = count + 5
End If
Next x

MsgBox "Search Complete. To perform another search, Clear the results and select a new category."

End Sub

Paul_Hossler
12-10-2020, 09:08 AM
1. Welcome to the forum. Take a minute and read the FAQs at the line in my sig

2. You can/should use the [#] icon to format code, since it formats and sets it off nicely

3. Try inserting the hyperlink, something like this



Sheets("AFCS").Hyperlinks.Add Anchor:=Sheets("AFCS").Cells(d, 14), Address:=Sheets("AFCS2").Cells(x, 5).Value, TextToDisplay:="My Link goes here"

Vikungr
12-10-2020, 09:38 AM
1. Welcome to the forum. Take a minute and read the FAQs at the line in my sig

2. You can/should use the [#] icon to format code, since it formats and sets it off nicely

3. Try inserting the hyperlink, something like this



Sheets("AFCS").Hyperlinks.Add Anchor:=Sheets("AFCS").Cells(d, 14), Address:=Sheets("AFCS2").Cells(x, 5).Value, TextToDisplay:="My Link goes here"


Hi Paul,

Thanks for the speedy reply, apologies not using the code tags this was my first thread.

Where would I insert this string in my code in order to pull through the links with the rest if the data? Again apologies if this is a basic query I am still not fully ofay with VBA.

Thanks

Sam

Paul_Hossler
12-10-2020, 01:25 PM
Replace this line ...




Sheets("AFCS").Cells(d, 14) = Sheets("AFCS2").Cells(x, 5)


... with this one



Sheets("AFCS").Hyperlinks.Add Anchor:=Sheets("AFCS").Cells(d, 14), Address:=Sheets("AFCS2").Cells(x, 5).Value, TextToDisplay: "My Link Goes Here"


The key being the Anchor cell: Sheets("AFCS").Cells(d, 14)

Hyperlinks are .Add-ed to a worksheet, and the Anchor is the cell where they are placed


Let us know how it works

Vikungr
12-11-2020, 01:01 AM
Replace this line ...




Sheets("AFCS").Cells(d, 14) = Sheets("AFCS2").Cells(x, 5)


... with this one



Sheets("AFCS").Hyperlinks.Add Anchor:=Sheets("AFCS").Cells(d, 14), Address:=Sheets("AFCS2").Cells(x, 5).Value, TextToDisplay: "My Link Goes Here"


The key being the Anchor cell: Sheets("AFCS").Cells(d, 14)

Hyperlinks are .Add-ed to a worksheet, and the Anchor is the cell where they are placed


Let us know how it works

Paul,

You are a scholar and a gent! This has solved my issue entirely. Thank you very much! Happy holidays to you and yours sir!

Sam