当直接传递值和通过变量传递值时,vba 中的 DIR 函数的工作方式不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16666163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:12:49  来源:igfitidea点击:

DIR function in vba works diffrent when passing a value directly and by a variable

excelvbadirectory

提问by murugan_kotheesan

Hi i have some files *in a particular folder in my system when i use *the below** code it gives all the files in that folder (passing the path directly)

嗨,我有一些文件 * 在我的系统中的特定文件夹中,当我使用 * 下面的** 代码时,它提供了该文件夹中的所有文件(直接传递路径)

filenm = Dir("C:\Documents and Settings\murugan.k\Desktop\Daily report automation\Eve report\trial\")

Do Until filenm = ""
ActiveSheet.Cells(ctr, 12).Value = filenm
ctr = ctr + 1
filenm = Dir()
Loop

but when i store the same path in a variable and pass the varible to the dir function it gives me only two files (AUTOEXEC.BAT & bar.emf)

但是当我在变量中存储相同的路径并将变量传递给 dir 函数时,它只给了我两个文件(AUTOEXEC.BAT & bar.emf)

filenm = Dir(pth)
Do Until filenm = ""
ActiveSheet.Cells(ctr, 12).Value = filenm
ctr = ctr + 1
filenm = Dir()
Loop

could some one please help me in resolving this problem because i can't hard code the path in macro which has to be dynamic (changes as per user)

有人可以帮我解决这个问题,因为我不能硬编码宏中的路径,它必须是动态的(根据用户进行更改)

回答by

Try different attributes for the Dir() function.

Dir() 函数尝试不同的属性。

path = "C:\Documents and Settings\murugan.k\Desktop\" & _ 
        "Daily report automation\Eve report\trial\"

filenm = Dir(path, vbNormal)

Do Until filenm = ""
    ActiveSheet.Cells(ctr, 12).Value = filenm
    ctr = ctr + 1
    filenm = Dir()
Loop

回答by murugan_kotheesan

i some how got his correct

我是如何得到他的正确的

b4 i have the path without"\" at the end so i concatenate the path with "\" and put that in anothe variable and i pass that variable to DIR function (which din't work) but now i have added "\" at the end of folder path in the input itself and i pass that varible to DIR it works now

b4 我的路径最后没有“\”,所以我将路径与“\”连接起来,并将其放在另一个变量中,然后将该变量传递给 DIR 函数(不起作用),但现在我添加了“\”在输入本身的文件夹路径的末尾,我将该变量传递给 DIR 它现在可以工作

'            checking the available files
filenm = Dir(File_pth)
Do Until filenm = ""
ActiveSheet.Cells(ctr, 12).Value = filenm
ctr = ctr + 1
filenm = Dir()
Loop

回答by jogrohs

well, for me your answer did not work and after some looking around i found a little mistake. your code is fine in general, but the "=" sign will not work because

好吧,对我来说,您的回答不起作用,经过一番环顾四周后,我发现了一个小错误。您的代码总体上很好,但是“=”符号不起作用,因为

When no more file names match, Dir returns a zero-length string ("").

当没有更多文件名匹配时,Dir 返回一个零长度字符串 ("")。

at least i think this is the reason it showed nothing for my output. so i would say it works like:

至少我认为这就是它对我的输出没有显示的原因。所以我会说它的工作原理是:

Sub LoopThruDirectory2()

Dim strPath As String
Dim strFile As String
Dim x As Integer
strPath = ThisWorkbook.path
strFile = Dir(strPath & "\")
Do While strFile <> ""
    x = x + 1
    Debug.Print strFile
    strFile = Dir    ' Get next entry.
Loop

End Sub

feel free to set "thisworkbook.path" as your path and handle strFile as you want. (eg. see above) . for my project the user wanted to pick a chosen folder so i had to use "msoFileDialogFolderPicker". just in case someone has the same problem

随意将“thisworkbook.path”设置为您的路径并根据需要处理 strFile 。(例如,见上文)。对于我的项目,用户想要选择一个选定的文件夹,所以我不得不使用“msoFileDialogFolderPicker”。以防万一有人遇到同样的问题