0

我有我正在使用的这段代码,在此处的帮助下使它成为 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# 文件夹存在,则在同一行的 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

4

1 回答 1

1

这里的问题是您没有正确的语法来创建超链接。我使用宏记录器查看将超链接添加到工作表的代码。我还搜索了“Excel VBA 将超链接添加到范围”。生成的语法如下:

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。)

于 2012-05-31T14:21:02.580 回答