Hi mjr veverka,
I'm running this code from within MS Access since my Access code exports data to an Excel sheet and then manipulates the Excel sheet (i.e. formatting, coloring, etc.). But this all happens from within the MS Access database VBA.
I modified your code slightly to update the column number as well as to account for Access not recognizing the "Application.Clean" command. But I get an error on the line with "If Right$(RTrim$(tbl(r, 1)), Len(crt2)) = crt2 Then" it highlights "tbl" with error "Compile Error ByVal Argument Type Mismatch".
Here's the code:
Error Happens at this line:
If Right$(RTrim$(tbl(r, 1)), Len(crt2)) = crt2 Then
This allows the use of the "Clean" feature.
Function MyClean(x As String) As String
MyClean = Excel.Application.WorksheetFunction.Clean(x)
End Function
Modified code you provided:
Public Sub HyperAdd()
Const crt1 = "#http://"
Const crt2 = ".pdf"
Const dlmtr = "#"
Dim c As Long, r As Long, rws As Long
Dim sadr As String, st As String
Dim tbl, txt
c = 12
sadr = ""
st = "Click to Open"
With ActiveSheet
rws = .Cells(.Rows.Count, c).End(xlUp).Row
tbl = .Range(Cells(1, c), Cells(rws, c)).value
For r = 1 To rws
If InStr(1, tbl(r, 1), crt1, 0) <> 0 Then
If Right$(RTrim$(tbl(r, 1)), Len(crt2)) = crt2 Then
txt = Split(MyClean(tbl(r, 1)), dlmtr, -1, 0)
.Hyperlinks.Add Anchor:=.Cells(r, c), _
Address:=Trim$(txt(1)), SubAddress:=sadr, _
ScreenTip:=st, TextToDisplay:=Trim$(txt(0))
End If
End If
Next
End With
The code runs flawlessly in Excel VBA, just not from the MS Access VBA side. I guess I could pass the code to Excel and execute this from within Excel, but I'll have to research that.
Any thoughts?
Bookmarks