在Excel VBA中按行单元格信息创建链接

时间:2012-05-31 14:08:18

标签: excel-vba vba excel

我有这个代码,我正在使用这个代码来帮助Mac / PC投诉,但是在我的PC上运行完毕至少现在,我已经创建了一个公司名称的文件夹,并且零件号文件夹供以后使用。

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

现在我要做的就是这样做,如果所说的客户存在,并且SO#Folder存在,则在同一行的P.中创建一个链接。

现在如下图所示,路径为C:\ Images \ Company Name \ SO#\

我想在代表行中提到的链接将是“SO#”的图片,SO#将被替换为行本身的SO#。然后当您单击它时,该链接将您带到资源管理器中的文件夹。

理论上我可以做到这样代码看起来像这样,但我不确定......

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

但我在这里遇到错误:

   Range("P3:P" & lastrow).Value = Hyperlink("C:\$C3:C" & lastrow \" & "B3:B" & lastrow, "Cellname")

说编译错误:预期列表分隔符或)

如果有人可以提供帮助,我们将不胜感激......附上的是excel表。

https://dl.dropbox.com/u/3327208/Excel/picture.xlsx

1 个答案:

答案 0 :(得分:1)

此处的问题是您没有正确的语法来创建超链接。我使用宏录制器来查看代码以向工作表添加超链接。我还用谷歌搜索“Excel VBA添加超链接到范围”。得到的sytax如下:

ActiveSheet.Hyperlinks.Add Anchor:=myRange, Address:= myPath, TextToDisplay:= myText

myRange = the place you want the hyperlink to go
myPath = the path of the hyperlink
myText = the text you want to display

在这里,我给了你答案,但是,如何在将来最好地找到你自己的答案。学习如何搜索答案是学习如何在VBA中编码的一半!

**编辑**

此外,您无法一次设置整个范围。您必须循环遍历行并分别添加每个超链接。否则,您可以在创建作业时添加超链接(从过去的convos中获取。)