打开文件夹中所有excel文件的VBA代码

打开文件夹中所有excel文件的VBA代码,vba,excel,macros,Vba,Excel,Macros,我使用的是vba,我试图根据单元格值打开文件夹(大约8-10)中的所有excel文件。我想知道这是否是打开它的正确方法,它一直给我在编写目录时的语法错误。当我重写该部分时,vba只弹出了msgbox,这意味着它必须循环并做正确的事情?但是没有打开任何文件。任何信息都会有帮助。非常感谢你们花时间来帮助我 Sub OpenFiles() Dim search As Worksheet Dim customer As Range Dim customerfolder As Range Dim Qu

我使用的是vba,我试图根据单元格值打开文件夹(大约8-10)中的所有excel文件。我想知道这是否是打开它的正确方法,它一直给我在编写目录时的语法错误。当我重写该部分时,vba只弹出了msgbox,这意味着它必须循环并做正确的事情?但是没有打开任何文件。任何信息都会有帮助。非常感谢你们花时间来帮助我

Sub OpenFiles()

Dim search As Worksheet
Dim customer As Range
Dim customerfolder As Range

Dim QualityHUB As Workbook

'setting variable references
Set QualityHUB = ThisWorkbook
Set search = Worksheets("Search")
Set customer = Worksheets("Search").Range("$D$1")
Set customerfolder = Worksheets("Search").Range("$D$3")


With QualityHUB

If IsEmpty((customer)) And IsEmpty((customerfolder)) Then

MsgBox "Please Fill out Customer Information and search again"

Exit Sub

End If

End With

With QualityHUB


Dim MyFolder As String
Dim MyFile As String
Dim Directory As String

Directory = "O:\LAYOUT DATA\" & customer & "\" & customerfolder"


MyFile = Dir(Directory & "*.xlsx")


Do While MyFile <> ""

Workbooks.Open Filename:=MyFile

MyFile = Dir()


Loop


MsgBox "Files Open for " + customerfolder + " complete"


End With


End Sub
子OpenFiles()
Dim搜索为工作表
暗淡的客户范围
调暗客户文件夹作为范围
Dim QualityHUB作为工作簿
'设置变量引用
Set QualityHUB=此工作簿
设置搜索=工作表(“搜索”)
设置客户=工作表(“搜索”)。范围($D$1)
设置customerfolder=工作表(“搜索”)。范围($D$3)
带QualityHUB
如果IsEmpty((客户))和IsEmpty((客户文件夹)),则
MsgBox“请填写客户信息并再次搜索”
出口接头
如果结束
以
带QualityHUB
将MyFolder设置为字符串
将MyFile设置为字符串
将目录设置为字符串
Directory=“O:\LAYOUT DATA\”&customer&“\”&customerfolder”
MyFile=Dir(目录&“*.xlsx”)
当我的文件“”时执行此操作
工作簿。打开文件名:=MyFile
MyFile=Dir()
环
MsgBox“文件打开为”+customerfolder+“完成”
以
端接头

我在网上找到了这段代码,它将打开文件夹中的所有excel文件,打开后,您可以修改代码以将函数应用于工作簿

Option Explicit

Type FoundFileInfo
    sPath As String
    sName As String
End Type

Sub find()
Dim iFilesNum As Integer
Dim iCount As Integer
Dim recMyFiles() As FoundFileInfo
Dim blFilesFound As Boolean

blFilesFound = FindFiles("G:\LOCATION OF FOLDER HERE\", _
       recMyFiles, iFilesNum, "*.xlsx", True)
End Sub

Function FindFiles(ByVal sPath As String, _
    ByRef recFoundFiles() As FoundFileInfo, _
    ByRef iFilesFound As Integer, _
    Optional ByVal sFileSpec As String = "*.*", _
    Optional ByVal blIncludeSubFolders As Boolean = False) As Boolean
    Dim iCount As Integer           '* Multipurpose counter
    Dim sFileName As String         '* Found file name
    Dim wbResults, file, WS_Count, i, gcell, col, finRow, wbCodeBook As Workbook, lCount, name, looper
    Dim WorksheetExists
    Set wbCodeBook = ThisWorkbook

    '*
    '* FileSystem objects
    Dim oFileSystem As Object, _
        oParentFolder As Object, _
        oFolder As Object, _
        oFile As Object

    Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set oParentFolder = oFileSystem.GetFolder(sPath)
    If oParentFolder Is Nothing Then
        FindFiles = False
        On Error GoTo 0
        Set oParentFolder = Nothing
        Set oFileSystem = Nothing
        Exit Function
    End If
    sPath = IIf(Right(sPath, 1) = "\", sPath, sPath & "\")
    '*
    '* Find files
    sFileName = Dir(sPath & sFileSpec, vbNormal)
    If sFileName <> "" Then
        For Each oFile In oParentFolder.Files
            If LCase(oFile.name) Like LCase(sFileSpec) Then
                iCount = UBound(recFoundFiles)
                iCount = iCount + 1
                ReDim Preserve recFoundFiles(1 To iCount)
                file = sPath & oFile.name
                name = oFile.name
            End If
                On Error GoTo nextfile:
                Set wbResults = Workbooks.Open(Filename:=file, UpdateLinks:=0)


''insert your code here


               wbResults.Close SaveChanges:=False
nextfile:
        Next oFile
        Set oFile = Nothing         '* Although it is nothing
    End If
    If blIncludeSubFolders Then
        '*
        '* Select next sub-forbers
        For Each oFolder In oParentFolder.SubFolders
            FindFiles oFolder.path, recFoundFiles, iFilesFound, sFileSpec, blIncludeSubFolders
        Next
    End If
    FindFiles = UBound(recFoundFiles) > 0
    iFilesFound = UBound(recFoundFiles)
    On Error GoTo 0
    '*
    '* Clean-up
    Set oFolder = Nothing           '* Although it is nothing
    Set oParentFolder = Nothing
    Set oFileSystem = Nothing

End Function
Function SSCGetColumnCodeFromIndex(colIndex As Variant) As String
    Dim tstr As String
    Dim prefixInt As Integer
    Dim suffixInt As Integer
    prefixInt = Int(colIndex / 26)
    suffixInt = colIndex Mod 26
    If prefixInt = 0 Then
        tstr = ""
    Else
        prefixInt = prefixInt - 1
        tstr = Chr(65 + prefixInt)
    End If
    tstr = tstr + Chr(65 + suffixInt)
    SSCGetColumnCodeFromIndex = tstr
End Function
Function GetColNum(oSheet As Worksheet, name As String)
Dim Endrow_Col, i
'For loop to get the column number of name
Endrow_Col = oSheet.Range("A1").End(xlToRight).Column
oSheet.Select
oSheet.Range("A1").Select
For i = 0 To Endrow_Col - 1 Step 1
If ActiveCell.Value <> name Then
    ActiveCell.Offset(0, 1).Select
ElseIf ActiveCell.Value = name Then
    GetColNum = ActiveCell.Column
    Exit For
    End If
Next i
End Function
Function ShDel(name As String)

End If
End Function
选项显式
键入FoundFileInfo
斯帕特作为一根绳子
作为字符串的sName
端型
子查找()
Dim iFilesNum作为整数
Dim I以整数形式计数
Dim recMyFiles()作为FoundFileInfo
未找到布尔值
blFilesFound=FindFiles(“G:\LOCATION OF FOLDER HERE\”_
recMyFiles,iFilesNum,“*.xlsx”,True)
端接头
函数FindFiles(ByVal sPath作为字符串_
ByRef recFoundFiles()作为FoundFileInfo_
ByRef IFilesFind为整数_
可选的ByVal sFileSpec As String=“***”_
可选的ByVal Blinclude子文件夹为布尔值=False)为布尔值
Dim I计数为整数'*多用途计数器
Dim sFileName作为字符串“*找到文件名
Dim wbResults、文件、WS_计数、i、gcell、col、finRow、wbCodeBook As工作簿、lCount、名称、活套
昏暗的工作表
设置wbCodeBook=ThisWorkbook
'*
'*文件系统对象
作为对象的系统的尺寸_
oParentFolder作为对象_
以文件夹为对象_
以文件为对象
leSystem=CreateObject的集合(“Scripting.FileSystemObject”)
出错时继续下一步
设置oParentFolder=oFileSystem.GetFolder(sPath)
如果oParentFolder为空,则
FindFiles=False
错误转到0
设置oParentFolder=Nothing
leSystem的集合=无
退出功能
如果结束
sPath=IIf(右(sPath,1)=“\”,sPath,sPath&“\”)
'*
“*查找文件
sFileName=Dir(sPath&sFileSpec,vbNormal)
如果是sFileName“”,则
对于oParentFolder.Files中的每个文件
如果LCase(oFile.name)与LCase(sFileSpec)相似,那么
iCount=UBound(recFoundFiles)
iCount=iCount+1
重拨保留recFoundFiles(1到iCount)
file=sPath&oFile.name
name=oFile.name
如果结束
转到下一个文件时出错:
设置wbResults=Workbooks.Open(文件名:=file,更新链接:=0)
''在此处插入您的代码
wbResults.Close SaveChanges:=False
下一个文件:
下一个文件
文件集=Nothing'*尽管它是Nothing
如果结束
如果包含子文件夹,则
'*
'*选择下一个子福布斯
对于oParentFolder.SubFolders中的每个文件夹
Folder.path、recFoundFiles、iFilesFound、sFileSpec和blIncludeSubFolders的FindFile
下一个
如果结束
FindFiles=UBound(recFoundFiles)>0
iFilesFound=UBound(recFoundFiles)
错误转到0
'*
*清理
文件夹集=Nothing'*尽管它是Nothing
设置oParentFolder=Nothing
leSystem的集合=无
端函数
函数SSCGetColumnCodeFromIndex(colIndex作为变量)作为字符串
Dim tstr As字符串
作为整数的Dim prefixInt
作为整数的Dim后缀
prefixInt=Int(colIndex/26)
后缀=colIndex Mod 26
如果prefixInt=0,则
tstr=“”
其他的
prefixInt=prefixInt-1
tstr=Chr(65+prefixInt)
如果结束
tstr=tstr+Chr(65+足够)
SSCGetColumnCodeFromIndex=tstr
端函数
函数GetColNum(oSheet作为工作表,名称作为字符串)
迪姆·恩德罗·科尔,i
'For循环获取名称的列号
Endrow_Col=oSheet.Range(“A1”).End(xlToRight).列
oSheet.选择
oSheet.范围(“A1”)。选择
对于i=0至Endrow_Col-1步骤1
如果是ActiveCell.Value名称,则
ActiveCell.Offset(0,1)。选择
ElseIf ActiveCell.Value=name然后
GetColNum=ActiveCell.Column
退出
如果结束
接下来我
端函数
函数ShDel(名称为字符串)
如果结束
端函数

我在网上找到了这段代码,它将打开文件夹中的所有excel文件,打开后,您可以修改代码以将函数应用于工作簿

Option Explicit

Type FoundFileInfo
    sPath As String
    sName As String
End Type

Sub find()
Dim iFilesNum As Integer
Dim iCount As Integer
Dim recMyFiles() As FoundFileInfo
Dim blFilesFound As Boolean

blFilesFound = FindFiles("G:\LOCATION OF FOLDER HERE\", _
       recMyFiles, iFilesNum, "*.xlsx", True)
End Sub

Function FindFiles(ByVal sPath As String, _
    ByRef recFoundFiles() As FoundFileInfo, _
    ByRef iFilesFound As Integer, _
    Optional ByVal sFileSpec As String = "*.*", _
    Optional ByVal blIncludeSubFolders As Boolean = False) As Boolean
    Dim iCount As Integer           '* Multipurpose counter
    Dim sFileName As String         '* Found file name
    Dim wbResults, file, WS_Count, i, gcell, col, finRow, wbCodeBook As Workbook, lCount, name, looper
    Dim WorksheetExists
    Set wbCodeBook = ThisWorkbook

    '*
    '* FileSystem objects
    Dim oFileSystem As Object, _
        oParentFolder As Object, _
        oFolder As Object, _
        oFile As Object

    Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set oParentFolder = oFileSystem.GetFolder(sPath)
    If oParentFolder Is Nothing Then
        FindFiles = False
        On Error GoTo 0
        Set oParentFolder = Nothing
        Set oFileSystem = Nothing
        Exit Function
    End If
    sPath = IIf(Right(sPath, 1) = "\", sPath, sPath & "\")
    '*
    '* Find files
    sFileName = Dir(sPath & sFileSpec, vbNormal)
    If sFileName <> "" Then
        For Each oFile In oParentFolder.Files
            If LCase(oFile.name) Like LCase(sFileSpec) Then
                iCount = UBound(recFoundFiles)
                iCount = iCount + 1
                ReDim Preserve recFoundFiles(1 To iCount)
                file = sPath & oFile.name
                name = oFile.name
            End If
                On Error GoTo nextfile:
                Set wbResults = Workbooks.Open(Filename:=file, UpdateLinks:=0)


''insert your code here


               wbResults.Close SaveChanges:=False
nextfile:
        Next oFile
        Set oFile = Nothing         '* Although it is nothing
    End If
    If blIncludeSubFolders Then
        '*
        '* Select next sub-forbers
        For Each oFolder In oParentFolder.SubFolders
            FindFiles oFolder.path, recFoundFiles, iFilesFound, sFileSpec, blIncludeSubFolders
        Next
    End If
    FindFiles = UBound(recFoundFiles) > 0
    iFilesFound = UBound(recFoundFiles)
    On Error GoTo 0
    '*
    '* Clean-up
    Set oFolder = Nothing           '* Although it is nothing
    Set oParentFolder = Nothing
    Set oFileSystem = Nothing

End Function
Function SSCGetColumnCodeFromIndex(colIndex As Variant) As String
    Dim tstr As String
    Dim prefixInt As Integer
    Dim suffixInt As Integer
    prefixInt = Int(colIndex / 26)
    suffixInt = colIndex Mod 26
    If prefixInt = 0 Then
        tstr = ""
    Else
        prefixInt = prefixInt - 1
        tstr = Chr(65 + prefixInt)
    End If
    tstr = tstr + Chr(65 + suffixInt)
    SSCGetColumnCodeFromIndex = tstr
End Function
Function GetColNum(oSheet As Worksheet, name As String)
Dim Endrow_Col, i
'For loop to get the column number of name
Endrow_Col = oSheet.Range("A1").End(xlToRight).Column
oSheet.Select
oSheet.Range("A1").Select
For i = 0 To Endrow_Col - 1 Step 1
If ActiveCell.Value <> name Then
    ActiveCell.Offset(0, 1).Select
ElseIf ActiveCell.Value = name Then
    GetColNum = ActiveCell.Column
    Exit For
    End If
Next i
End Function
Function ShDel(name As String)

End If
End Function
选项显式
键入FoundFileInfo
斯帕特作为一根绳子
作为字符串的sName
端型
子查找()
Dim iFilesNum作为整数
Dim I以整数形式计数
Dim recMyFiles()作为FoundFileInfo
未找到布尔值
blFilesFound=FindFiles(“G:\LOCATION OF FOLDER HERE\”_
recMyFiles,iFilesNum,“*.xlsx”,True)
端接头
函数FindFiles(ByVal sPath作为字符串_
ByRef recFoundFiles()作为FoundFileInfo_
ByRef IFilesFind为整数_
可选的ByVal sFileSpec As String=“***”_
可选的ByVal Blinclude子文件夹为布尔值=False)为布尔值
Dim I计数为整数'*多用途计数器
Dim sFileName作为字符串“*找到文件名
将wbResults、文件、WS_计数、i、gcell、col、finRow、wbCodeBook作为Workboo
Sub OpenFiles()

Dim search As Worksheet
Dim customer As Range
Dim customerfolder As Range

Dim QualityHUB As Workbook

'setting variable references
Set QualityHUB = ThisWorkbook
Set search = Worksheets("Search")
Set customer = Worksheets("Search").Range("$D$1")
Set customerfolder = Worksheets("Search").Range("$D$3")


With QualityHUB

If IsEmpty((customer)) And IsEmpty((customerfolder)) Then

    MsgBox "Please Fill out Customer Information and search again"

Exit Sub

End If

End With

With QualityHUB


Dim MyFolder As String
Dim MyFile As String
Dim Directory As String

Directory = "O:\LAYOUT DATA\" & customer & "\" & customerfolder & "\"


MyFile = Dir(Directory & "*.xlsx")

Do While MyFile <> ""

Workbooks.Open Filename:=Directory & MyFile

MyFile = Dir()


Loop


MsgBox "Files Open for " + customerfolder + " complete"


End With


End Sub
Workbooks.Open Filename:=Directory & MyFile
Workbooks.Open Filename:=MyFile