Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 循环浏览指定文件夹中的文件并将其导入Excel_Vba_Excel - Fatal编程技术网

Vba 循环浏览指定文件夹中的文件并将其导入Excel

Vba 循环浏览指定文件夹中的文件并将其导入Excel,vba,excel,Vba,Excel,我正试图找到一种方法,让Excel循环浏览各种HTML文件,并一次导入一个,在移动到下一个文件之前,以指定的方式对每个文件进行格式化。这些文件都在FOLDER2中。我如何修改它,使其不仅考虑文件,而且考虑文件1…n?我遇到的一个直接问题是,我收到“运行时错误'1004':Microsoft Excel无法打开或读取此查询文件”。我怀疑这是因为它是HTML而不是XML。当我手动告诉Excel选择所有文件时,没有问题,但宏可能没有这样做 With ActiveSheet.QueryTables.Ad

我正试图找到一种方法,让Excel循环浏览各种HTML文件,并一次导入一个,在移动到下一个文件之前,以指定的方式对每个文件进行格式化。这些文件都在FOLDER2中。我如何修改它,使其不仅考虑文件,而且考虑文件1…n?我遇到的一个直接问题是,我收到“运行时错误'1004':Microsoft Excel无法打开或读取此查询文件”。我怀疑这是因为它是HTML而不是XML。当我手动告诉Excel选择所有文件时,没有问题,但宏可能没有这样做

With ActiveSheet.QueryTables.Add(Connection:= _
        "FINDER;file:///C:/Users/FOLDER1/FOLDER2/FILE", Destination:= _
        Range("$A$1"))
        .CommandType = 0
        .Name = "FILE"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Sub
更新:

我试图修改下面的代码,这样我就可以简单地选择文件夹并以这种方式运行它。这是否合理的做法

Sub ImportXMLData()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String
Dim xlWkBk As Workbook, xmlFile As Workbook, LastRow As Long
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.html", vbNormal)
Set xlWkBk = ThisWorkbook
While strFile <> ""
  LastRow = xlWkBk.Sheets(1).Cells.SpecialCells(xlCellTypeLastCell).Row + 1
  Set xmlFile = Workbooks.OpenXML(Filename:=strFolder & "\" & strFile)
  xmlFile.Sheets(1).UsedRange.Copy
  xlWkBk.Sheets(1).Cells(LastRow, 1).Paste
  xmlFile.Close SaveChanges:=False
  strFile = Dir()
Wend
Set xmlFile = Nothing: Set xlWkBk = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Sub-ImportXMLData()
Application.ScreenUpdating=False
将strFolder设置为字符串,将strFile设置为字符串
Dim xlWkBk作为工作簿,xmlFile作为工作簿,最后一行长度为
strFolder=GetFolder
如果strFolder=“”,则退出Sub
strFile=Dir(strFolder&“\*.html”,vbNormal)
设置xlWkBk=此工作簿
而strFile“”
LastRow=xlWkBk.Sheets(1).Cells.SpecialCells(xlCellTypeLastCell).Row+1
设置xmlFile=Workbooks.OpenXML(文件名:=strFolder&“\”&strFile)
xmlFile.Sheets(1).UsedRange.Copy
xlWkBk.表格(1).单元格(最后一行,1).粘贴
xmlFile.Close SaveChanges:=False
strFile=Dir()
温德
Set xmlFile=Nothing:Set xlWkBk=Nothing
Application.ScreenUpdating=True
端接头
函数GetFolder()作为字符串
作为对象的文件夹的尺寸
GetFolder=“”
文件夹集=CreateObject(“Shell.Application”)。浏览文件夹(0,“选择文件夹”,0)
如果(非oFolder为Nothing),则GetFolder=oFolder.Items.Item.Path
文件夹集=无
端函数

您可以使用
FileSystemObject
(需要添加对Microsoft脚本运行时的引用)循环浏览文件夹的内容:

sub loopThroughFolder(folderPath as string)

dim fso as new FileSystemObject
dim fileObj as file

for each fileObj in fso.GetFolder(folderPath).Files
 if filenameFitsCriteria(fileObj.name) then
  importFile(fileObj.Path)
next

end sub
对于选择文件夹的界面,您可以查看:

Application.FileDialog(msoFileDialogFolderPicker)

也许能帮上忙。谢谢!VBA似乎不希望我在添加(folderPath作为字符串)时运行代码,但由于某种原因,它打开了“宏”窗口,并且没有按子对象的名称显示任何子对象。你知道为什么会这样吗?是的,这是一个sub,你必须从另一个sub调用它,在那里你可以得到文件夹路径。您可以将
folderPath作为字符串删除
,它将自行运行,但您必须在开始处添加一行来选择文件夹,可能需要使用我在回答中提到的
msoFileDialogFolderPicker
。谢谢!让我试试。你也可以调用你编写的
GetFolder()
函数,而不是使用
msoFileDialogFolderPicker
编写一个新函数。我试了一下,但它告诉我GetFolder()是不明确的。你会知道如何防止这种情况发生吗?再次感谢您的帮助。