I have this code I’m working with, to make it Mac/PC complaint with help from here, but after this is done running on my PC at least for now, I have created a folder with a company name, and part number folder for later use.
Sub MakeFolder()
Dim strComp As String, strPart As String, strPath As String
strComp = Range("A1") ' assumes company name in A1
strPart = CleanName(Range("C1")) ' assumes part in C1
strPath = "C:\Images\"
If Not FolderExists(strPath & strComp) Then
'company doesn't exist, so create full path
FolderCreate strPath & strComp & "\" & strPart
Else
'company does exist, but does part folder
If Not FolderExists(strPath & strComp & "\" & strPart) Then
FolderCreate strPath & strComp & "\" & strPart
End If
End If
End Sub
Function FolderCreate(ByVal path As String) As Boolean
FolderCreate = True
Dim fso As New FileSystemObject
If Functions.FolderExists(path) Then
Exit Function
Else
On Error GoTo DeadInTheWater
fso.CreateFolder path
Exit Function
End If
DeadInTheWater:
MsgBox "A folder could not be created for the following path: " & path & ". Check the path name and try again."
FolderCreate = False
Exit Function
End Function
Function FolderExists(ByVal path As String) As Boolean
FolderExists = False
Dim fso As New FileSystemObject
If fso.FolderExists(path) Then FolderExists = True
End Function
Function CleanName(strName as String) as String
'will clean part # name so it can be made into valid folder name
'may need to add more lines to get rid of other characters
CleanName = Replace(strName, "/","")
CleanName = Replace(CleanName, "-","")
etc...
End Function
Now what I am attempting to do is make it so that if said Customer exists, and SO# Folder Exists, to create a link to it in P. on the same row.
Now as shown below the path is C:\Images\Company Name\SO#\
The link I’d like to have would say in the representing row would be Pictures for “SO#” the SO# would be replaced with the SO# of the row itself. Then when you click on it, the link brings you to the folder in Explorer.
Theoretically I could make it so that the code looks like this, but I’m not sure entirely…
Option Explicit
Sub Create_a_Link()
Dim wsJAR As Worksheet 'JL Archive
Dim lastrow As Long, fstcell As Long
Set wsJAR = Sheets("JL Archive")
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
With JAR
lastrow = wsJAR.Cells(Rows.Count, "P").End(xlUp).Row
Range("P3:P" & lastrow).Value = Hyperlink("C:\$C3:C" & lastrow \" & "B3:B" & lastrow, "Cellname")
End With
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
but I keep getting an error here:
Range("P3:P" & lastrow).Value = Hyperlink("C:\$C3:C" & lastrow \" & "B3:B" & lastrow, "Cellname")
Says a compile error: Expected list separator or )
If anyone can help it be appreciated…Attached is the excel sheet.
The problem here is that you don’t have the right syntax to create a hyperlink. I used the macro recorder to see the code to add a hyperlink to a sheet. I also googled “Excel VBA add hyperlink to range”. The resulting sytax is below:
Here I gave you your answer, but also, how to best go about finding your own answer in the future. Learning how to search for answers is half the battle of learning how to code in VBA!
** EDIT **
Also, you can’t set the whole range at once. You have to loop through rows and add each hyperlink separately. Otherwise, you can add the hyperlink when you create the job (take from past convos.)