简体   繁体   English

VBA 小Hashmap 获取路径问题

[英]VBA Little Hashmap to get path problems

I got some problems with my hashmap, it is in.xla file (complementary macro), i explain what i want: if i call my macro with parameter (cv_source), i want the path of cv_source, here is my code:我的 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

My problems are the following: - How can I return path?我的问题如下: - 我如何返回路径? I can't put global variable for path because it doesn't work - How can I call the macro in an other file?我不能为路径放置全局变量,因为它不起作用 - 如何在其他文件中调用宏? I selected the xla file in others excel files but to call the macro how can i do?我在其他 excel 文件中选择了 xla 文件,但要调用宏我该怎么办? Thanks in advance提前致谢

You can use Collection instead CreateObject("Scripting.Dictionary")您可以使用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

You can even have public variable as collection, but you have to remember that you have to fill it before using.您甚至可以将公共变量作为集合,但您必须记住在使用之前必须填充它。

____edit ____编辑

You should load data once, not every calling.您应该加载一次数据,而不是每次调用。

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

And in when You need sht from path use only this: path("cv_source") in code.并且当您需要从路径中获取 sht 时,仅使用以下代码: path("cv_source")在代码中。

So, no return, and how can i test in an othersub if this is working?所以,没有回报,如果这有效,我如何在 othersub 中进行测试? You think my function is good here so?你认为我的 function 在这里很好吗?

Public Function path_file(path As Collection)
path.Add "D:\Téléchargements\CreationCV\CreationCV\Sources", "cv_source"
path.Add "D:\Téléchargements\CreationCV\CreationCV\", "creation_cv"
path.Add "D:\Téléchargements\CreationCV\CreationCV\Sauvergardes_chrono", "save_chrono"
path.Add "D:\Téléchargements\CreationCV\CreationCV\Sauvergardes_chrono\Chrono.xls", "save_chrono.xls"
path.Add "D:\Téléchargements\CreationCV\CreationCV\Sources\Chrono2018.xls", "chrono2018"
path.Add "D:\Téléchargements\CreationCV\CreationCV\Sources\Chrono.xls", "chrono"
path.Add "D:\Téléchargements\CreationCV\CreationCV\CV_pdf", "cv_pdf"
path.Add "D:\Téléchargements\CreationCV\Etiquettes\Base dates.xls", "base_dates"
path.Add "D:\Téléchargements\CreationCV\CreationCV\Sources\Ariane.xls", "ariane.xls"
path.Add "D:\Téléchargements\CreationCV\CreationCV\Importer dans DECA.xlsm", "import_deca"

End Function @chronocidal结束 Function @chronocidal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM