Excel 使用VB允许用户选择一个文件夹,将所有文件加载到电子表格中
与上述问题斗争 我有一些代码,可以将文件夹中的所有文件加载到电子表格中。我需要这样做,用户可以选择一个文件夹,而不是代码中已经定义的路径 以下是我已有的代码,如有任何指导,将不胜感激Excel 使用VB允许用户选择一个文件夹,将所有文件加载到电子表格中,excel,vba,Excel,Vba,与上述问题斗争 我有一些代码,可以将文件夹中的所有文件加载到电子表格中。我需要这样做,用户可以选择一个文件夹,而不是代码中已经定义的路径 以下是我已有的代码,如有任何指导,将不胜感激 Sub From_IDPXML_To_ExcelReport() On Error GoTo errh Dim myWB As Workbook, WB As Workbook Set myWB = ThisWorkbook Dim myPath myPath = "File path here” Dim m
Sub From_IDPXML_To_ExcelReport()
On Error GoTo errh
Dim myWB As Workbook, WB As Workbook
Set myWB = ThisWorkbook
Dim myPath
myPath = "File path here”
Dim myFile
myFile = Dir(myPath & "*.xml")
Dim t As Long, N As Long, row As Long, column As Long
t = 2
N = 0
Application.ScreenUpdating = False
Do While myFile <> ""
N = N + 1
Set WB = Workbooks.OpenXML(Filename:=myPath & myFile)
If N > 1 Then
row = WB.Sheets(1).Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
column = WB.Sheets(1).Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).column
WB.Sheets(1).Range(Cells(3, "A"), Cells(row, column)).Copy myWB.Sheets(1).Cells(t, "A")
Else
WB.Sheets(1).UsedRange.Copy myWB.Sheets(1).Cells(t, "A")
End If
WB.Close False
t = myWB.Sheets(1).Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row + 1
myFile = Dir()
Loop
Application.ScreenUpdating = True
myWB.Save
Exit Sub
errh:
MsgBox "no xml files in folder specified"
End Sub
Sub-From_IDPXML_To_ExcelReport()
关于错误转到错误
将myWB设置为工作簿,将WB设置为工作簿
设置myWB=ThisWorkbook
昏暗的小路
myPath=“此处的文件路径”
Dim myFile
myFile=Dir(myPath&“*.xml”)
尺寸t为长,N为长,行为长,列为长
t=2
N=0
Application.ScreenUpdating=False
当我的文件“”时执行此操作
N=N+1
设置WB=Workbooks.OpenXML(文件名:=myPath&myFile)
如果N>1,则
行=WB.Sheets(1).Cells.Find(内容:=“*”,搜索顺序:=xlByRows,搜索方向:=xlPrevious)。行
column=WB.Sheets(1).Cells.Find(内容:=“*”,搜索顺序:=xlByColumns,搜索方向:=xlPrevious)。column
工作分解表(1).范围(单元格(3,“A”)、单元格(行、列))。复制我的工作分解表(1).单元格(t,“A”)
其他的
WB.Sheets(1).UsedRange.Copy myWB.Sheets(1).单元格(t,“A”)
如果结束
WB.关闭错误
t=myWB.Sheets(1).Cells.Find(What:=“*”,SearchOrder:=xlByRows,SearchDirection:=xlPrevious)。行+1
myFile=Dir()
环
Application.ScreenUpdating=True
myWB.Save
出口接头
呃:
MsgBox“未指定文件夹中的xml文件”
端接头
谢谢。当然可以,请更换:
myPath = "File path here”
与:
并将此UDF包含在您的模块中:
Public Function GetFolder() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem & "\"
Set fldr = Nothing
End Function
公共函数GetFolder()作为字符串
Dim fldr As FILE对话框
以字符串形式显示
设置fldr=Application.FileDialog(msoFileDialogFolderPicker)
与fldr
.Title=“选择一个文件夹”
.AllowMultiSelect=False
.InitialFileName=Application.DefaultFilePath
如果.Show-1,则转到下一个代码
sItem=.SelectedItems(1)
以
下一个代码:
GetFolder=sItem&“\”
设置fldr=无
端函数
让用户选择目录的最简单、最简单的代码是:
Sub dir_browser()
Set dir_chooser = Application.FileDialog(msoFileDialogFolderPicker)
dir_chooser.Show
picked_dir = dir_chooser.SelectedItems(1)
End Sub
所以你的问题是如何让用户选择一个目录?如果是的话,你是否考虑先在这里搜索先前的问题,然后在发布之前?我找到了一些结果,通过搜索<代码> [VBA]选择目录看起来它们满足你的需求。
Sub dir_browser()
Set dir_chooser = Application.FileDialog(msoFileDialogFolderPicker)
dir_chooser.Show
picked_dir = dir_chooser.SelectedItems(1)
End Sub