I am trying to assign a hyperlink function to a cell, as follow, using VBA code
=HYPERLINK(env("OneDriveCommercial") & "\..\DPS\Engagements\Mary\PR-00000", PR-00000)
The Windows Environment Variable is OneDriveCommercial
- OneDriveCommercial=C:\Data\Collection\OneDrive\Active
I have a defined function called env
My VBA code works to a Point but not completely. When assigning the hyperlink function to the cell value, I am getting atsigns (@) which is not expected
=HYPERLINK(@env("OneDriveCommercial") &@ PathAdd,@ TargetValue)
For all intended purposes, the excel spreadsheet would have
cell(r,2)="Mary"
cell (r,5)="PR-00000"
Goal: Execute Code from any cell in the row and For Cell (R,5) to display the Hyperlink Friendly Name but contain the Hyperlink formula/function
The VBA code is below. Any assistance is appreciated in rectifying my cell value
=HYPERLINK(env("OneDriveCommercial") & "\..\DPS\Engagements\Mary\PR-00000", PR-00000)
The Windows Environment Variable is OneDriveCommercial
- OneDriveCommercial=C:\Data\Collection\OneDrive\Active
I have a defined function called env
My VBA code works to a Point but not completely. When assigning the hyperlink function to the cell value, I am getting atsigns (@) which is not expected
=HYPERLINK(@env("OneDriveCommercial") &@ PathAdd,@ TargetValue)
For all intended purposes, the excel spreadsheet would have
cell(r,2)="Mary"
cell (r,5)="PR-00000"
Goal: Execute Code from any cell in the row and For Cell (R,5) to display the Hyperlink Friendly Name but contain the Hyperlink formula/function
The VBA code is below. Any assistance is appreciated in rectifying my cell value
VBA Code:
Sub MakeFolders_Hyperlink2()
Dim Rng As Range
Dim TargetDir, TargetPath, ENVPath, PathAdd As String
Dim TargetValue As String
Dim iStart, answer As Integer
Dim aDirs As Variant
Dim sCurDir As String
Dim i, r As Integer
Dim fld, myFile As Object
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set Rng = ActiveCell
Set fld = CreateObject("Scripting.FileSystemObject")
r = Rng.Row
TargetValue = Cells(r, 5)
TargetColor = Cells(r, 5).Font.Color
ENVPath = (env("OneDriveCommercial"))
PathAdd = "\..\DPS\Engagements\" & Cells(r, 2) & "\" & TargetValue
TargetPath = ENVPath & PathAdd
If Len(Dir(TargetPath, vbDirectory)) = 0 Then
If TargetPath <> "" Then
aDirs = Split(TargetPath, "\")
If Left(TargetPath, 2) = "\\" Then
iStart = 3
Else
iStart = 1
End If
sCurDir = Left(TargetPath, InStr(iStart, TargetPath, "\"))
For i = iStart To UBound(aDirs)
sCurDir = sCurDir & aDirs(i) & "\"
If Dir(sCurDir, vbDirectory) = vbNullString Then
MkDir sCurDir
End If
Next i
ActiveSheet.Cells(r, 5).Formula = "=HYPERLINK(env(""OneDriveCommercial"") & PathAdd, TargetValue)"
' ==> attempts to execute env function and do not want it to <==
' ==> want cell (r,5) to be =HYPERLINK(env("OneDriveCommercial") & "\..\DPS\Engagements\Mary\PR-00000", PR-00000)
Cells(r, 5).Font.Color = TargetColor
Set myFile = fld.CreateTextFile(TargetPath & "\Notes.txt", False)
End If
Else
answer = MsgBox("Directory " & TargetPath & " Already exists", vbQuestion + vbYesNo + vbDefaultButton2, "Create Hyperlink")
If answer = vbYes Then
ActiveSheet.Cells(r, 5).Formula = "=HYPERLINK(env(""OneDriveCommercial"") & PathAdd, TargetValue)"
' ==> attempts to execute env function and do not want it to
' ==> want cell (r,5) to be =HYPERLINK(env("OneDriveCommercial") & "\..\DPS\Engagements\Mary\PR-00000", PR-00000)
Cells(r, 5).Font.Color = TargetColor
End If
End If
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
Function env(vn As String) As String
env = Environ(vn)
End Function