首页 > 解决方案 > VBA Little Hashmap 获取路径问题

问题描述

我的哈希图有一些问题,它在 .xla 文件(补充宏)中,我解释了我想要的:如果我用参数(cv_source)调用我的宏,我想要 cv_source 的路径,这是我的代码:

Sub path_file()
    Dim path
    Set path = CreateObject("Scripting.Dictionary")
    path.Add "cv_source", "D:\Téléchargements\CreationCV\CreationCV\Sources"
    path.Add "creation_cv", "D:\Téléchargements\CreationCV\CreationCV\"
    path.Add "save_chrono", "D:\Téléchargements\CreationCV\CreationCV\Sauvergardes_chrono"
    path.Add "save_chrono.xls", "D:\Téléchargements\CreationCV\CreationCV\Sauvergardes_chrono\Chrono.xls"
    path.Add "chrono2018", "D:\Téléchargements\CreationCV\CreationCV\Sources\Chrono2018.xls"
    path.Add "chrono", "D:\Téléchargements\CreationCV\CreationCV\Sources\Chrono.xls"
    path.Add "cv_pdf", "D:\Téléchargements\CreationCV\CreationCV\CV_pdf"
    path.Add "base_dates", "D:\Téléchargements\CreationCV\Etiquettes\Base dates.xls"
    path.Add "ariane.xls", "D:\Téléchargements\CreationCV\CreationCV\Sources\Ariane.xls"
    path.Add "import_deca", "D:\Téléchargements\CreationCV\CreationCV\Importer dans DECA.xlsm"
    Return
End Sub

我的问题如下: - 我怎样才能返回路径?我不能为路径放置全局变量,因为它不起作用 - 如何在其他文件中调用宏?我在其他 excel 文件中选择了 xla 文件,但要调用宏,我该怎么办?提前致谢

标签: excelvba

解决方案


您可以Collection改用CreateObject("Scripting.Dictionary")

Sub path_file()

    Dim path As New Collection
    path.Add "D:\Téléchargements\CreationCV\CreationCV\Sources", "cv_source"
    path.Add "D:\Téléchargements\CreationCV\CreationCV\", "creation_cv"


    Debug.Print path("cv_source")

End Sub

您甚至可以将公共变量作为集合,但您必须记住在使用之前必须填充它。

____编辑

您应该加载一次数据,而不是每次调用。

Public path as new collection 'this is your global object (on top of module)

'this procedure should be run during exec Excel (I don't know where - in Word there is AutoExec procedure)
Public sub LoadData()

    path.Add "D:\Téléchargements\CreationCV\CreationCV\Sources", "cv_source"
    path.Add "D:\Téléchargements\CreationCV\CreationCV\", "creation_cv"
    '(...)

end sub

当您需要路径中的 sht 时,仅使用此:path("cv_source")在代码中。


推荐阅读