繁体   English   中英

使用getfolder函数转到默认文件夹,然后选择子文件夹

[英]Using getfolder function to go to a default folder then select sub-folder

我使用在这里找到的一些代码开始尝试将一大堆Excel CSV文件转换为Excel 2003格式。 在转换过程中,我想打开一个默认位置文件夹,然后导航到CSV文件所在的右侧子文件夹,但是,单步执行代码时,不会填充我的变量之一。 我的代码在下面,不会填充的变量是strDir。

我想要代码用默认位置+我选择的文件夹填充strDir,但是我不确定我需要对此代码执行什么操作才能使其实现此功能。

现在,我只对默认位置进行了硬编码,并且当代码运行时,将打开该位置。 但是,当我选择子文件夹时,如何以编程方式进行记录?

我知道我想做什么,但是如何在VBA中实现这一点是我的问题。

Public Sub CSV_to_XLS()


Dim wb As Workbook
Dim strFile As String
Dim strDir As String
Dim strDirCapture As String

'Set base directory for get folder to manipulate csv files

strDirCapture = GetFolder("\\DEVP-APPS-07\File Storgae\1_Pending\")

'strDir = strDirCapture
strDir = strDirCapture & "\"
strFile = Dir(strDir & "*.csv")

MsgBox "String directory path = " & strDirCapture
MsgBox "StrFile = " & strFile

Do While strFile <> ""



    'Set wb = Workbooks.Open(Filename:=strDir & strFile, Local:=True)
    'wb.SaveAs Replace(wb.FullName, ".csv", ".xls"), 56 'UPDATE:
    wb.Close True

    Set wb = Nothing
    strFile = Dir
Loop

End Sub


Function GetFolder(strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .InitialFileName = strPath
    If .Show <> -1 Then GoTo NextCode
    sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function

非常感谢

安德鲁

在捕获的目录末尾添加反斜杠“ \\”的更新似乎已解决了该问题。 上面的代码已更改,以反映此更改。

尝试在strDir = strDirCapture之后添加以下行:

If Right(strDir, 1) <> "\" Then
    strDir = strDir & "\"
End If

暂无
暂无

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

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