更改您选择的路线的宏,该宏表示一个单元格

时间:2019-06-28 23:29:13

标签: excel vba

我有一个宏,该宏列出了某个文件夹中的文件并将它们放在列表框中。接收地址的地址是静态形式。我希望您向我指出此地址取决于单元格J1中的路由。宏如下

Private Sub Userform_initialize()
    Application.EnableEvents = False
    c00 = "C:\Users\Downloads" 'change path to suit
    c01 = Dir(c00 & "*.pdf")
    With CreateObject("scripting.filesystemobject")
        Do While c01 <> "" c02 = c02 & "|" & .GetBaseName(c00 & c01)
            c01 = Dir
        Loop
    End With
    With ListBox1
        .ListIndex = -1
        .List = Split(Mid(c02, 2), "|")
    End With
    Application.EnableEvents = True
End Sub

Private Sub ListBox1_Click() 'change path to suit 
    Filename = "C:\User\Downloads\" & ListBox1.Value & ".pdf"
    ShellExecute 0, "Open", Filename, "", "", vbMaximizedFocus
    ListBox1.ListIndex = -1 
End Sub

1 个答案:

答案 0 :(得分:0)

只需将静态字符串替换为Range(“ J1”)的值

Private Sub Userform_initialize()
    Application.EnableEvents = False
    c00 = Range("J1").Value 'change path to suit
    c01 = Dir(c00 & "*.pdf")
    With CreateObject("scripting.filesystemobject")
        Do While c01 <> "" c02 = c02 & "|" & .GetBaseName(c00 & c01)
            c01 = Dir
        Loop
    End With
    With ListBox1
        .ListIndex = -1
        .List = Split(Mid(c02, 2), "|")
    End With
    Application.EnableEvents = True
End Sub

Private Sub ListBox1_Click() 'change path to suit 
    Filename = Range("J1").Value & ListBox1.Value & ".pdf"
    ShellExecute 0, "Open", Filename, "", "", vbMaximizedFocus
    ListBox1.ListIndex = -1 
End Sub