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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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中指定扩展名为xlsx的文件_Vba_Excel_For Loop - Fatal编程技术网

在VBA中指定扩展名为xlsx的文件

在VBA中指定扩展名为xlsx的文件,vba,excel,for-loop,Vba,Excel,For Loop,如何将if语句设置为仅从指定文件夹获取xlsx文件 您好,我正在运行一个宏,它将excel文件从多个文件夹和子文件夹中取出,然后将这些文件编译成一个excel文件。它决定要提取哪个文件,因为只有一个文件可以从一个名为hoover test的位置提取 宏的相关部分在这里,我希望将循环更改为仅识别“.xlsx”文件: 这对于检查文件夹中的两个文件非常有效,但我只希望从该文件夹中获取xlsx文件 如果需要,完整宏将显示在此处: 'Option Explicit Public wbm As Workbo

如何将if语句设置为仅从指定文件夹获取xlsx文件

您好,我正在运行一个宏,它将excel文件从多个文件夹和子文件夹中取出,然后将这些文件编译成一个excel文件。它决定要提取哪个文件,因为只有一个文件可以从一个名为hoover test的位置提取

宏的相关部分在这里,我希望将循环更改为仅识别“.xlsx”文件:

这对于检查文件夹中的两个文件非常有效,但我只希望从该文件夹中获取xlsx文件

如果需要,完整宏将显示在此处:

'Option Explicit
Public wbm As Workbook
Public wbk As Workbook
Public File As File

Sub CM()

Dim FileSystem As Object
Dim HostFolder As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
HostFolder = "C:\Review Pack\Hoover Test"
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Set wbm = ThisWorkbook
DoFolder FileSystem.GetFolder(HostFolder)
For Each sht In wbm.Worksheets
    sht.Cells.Replace what:="" & Chr(10) & "", Replacement:=" ",     LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False,     ReplaceFormat:=False
Next sht
Application.ScreenUpdating = True
'LightOff
MsgBox "Done"
End Sub

Sub DoFolder(Folder)
Dim SubFolder As Folder
Dim i As Integer
Dim CopyR As Range


For Each SubFolder In Folder.SubFolders
    DoFolder SubFolder


Next


If Folder.SubFolders.Count = 0 Then
    If Folder.Files.Count = 1 Then
          Else: MsgBox "2+ files: " & Folder.Path
    End If
    For Each File In Folder.Files
        Hoover File
    Next

Else
End If



End Sub

Sub Hoover(File)
Dim i As Integer
Dim LineNo As Integer
Set wbk = Workbooks.Open(File.Path, , False)
Application.AskToUpdateLinks = False


If wbk.MultiUserEditing Then
    wbk.ExclusiveAccess
End If
For i = 2 To 11

    If Sheets(i).FilterMode Then
        wbk.Sheets(i).Unprotect "x"
        Sheets(i).Cells.AutoFilter
    End If

    LineNo = wbm.Sheets(i).Range("A" & Rows.Count).End(xlUp).Row + 1
    wbm.Sheets(i).Range("A" & LineNo & ":" & "AB" & LineNo + 990).Value =    wbk.Sheets(i).Range("A10:AB1000").Value
Next i
     wbk.Close False

End Sub

将文件上的循环更改为

For Each File In Folder.Files
    If LCase(Right(File.Name, 5)) = ".xlsx" Then
        Hoover File
    End If
Next

您有一个文件系统对象已准备就绪并正在等待。使用:

FileSystem.GetExtensionName(file) = "xlsx" 


嗯,我对DoFolder脚本中的条件指令感到迷茫,但我想只有当子文件夹至少包含2.xlsx文件时,您才会执行某些操作。我认为您可以遍历子文件夹中的所有文件,只需检查它们的名称中是否有字符串“.xlsx”或“.xls”。后一个选项也将计算.xlsm和.xlsb

您可以使用此功能:

Function CountXLS(folder) As Long
    Dim f As Object
    Dim cnt As Long

    For Each f In folder.Files
        If InStr(f.Name, ".xls") Then cnt = cnt + 1
        'If InStr(f.Name, ".xlsx") Then cnt = cnt + 1 'more precise variant
    Next f

    CountXLS = cnt

End Function

你的确切问题是什么?我如何设置我的If语句以仅从指定文件夹获取xlsx文件你应该更新你的问题以使其更明确:)谢谢,我在那里更改了它,你想在哪里执行它?@QHarr:
file.extension
不适用于我(“…不支持此属性”),使用Excel 2010和2016进行了尝试,包括后期和早期绑定。你知道吗?试试FileSystem.GetExtensionName。我将看一看第一个。@myles collier:请注意,一个文件的EXCENSION可以用大写字母书写。@FunThomas认为我可能在第一个文件中混淆了vbScript。好的。编辑
Right$(file.Path, Len(file.Path) - InStrRev(file.Path, "."))
Function CountXLS(folder) As Long
    Dim f As Object
    Dim cnt As Long

    For Each f In folder.Files
        If InStr(f.Name, ".xls") Then cnt = cnt + 1
        'If InStr(f.Name, ".xlsx") Then cnt = cnt + 1 'more precise variant
    Next f

    CountXLS = cnt

End Function