Vba代码,用于在同一工作簿的单独工作表中打开多个文件

Vba代码,用于在同一工作簿的单独工作表中打开多个文件,vba,excel,openfiledialog,worksheet,Vba,Excel,Openfiledialog,Worksheet,我有一个代码,允许我在excel工作簿中打开一个文件,但是我希望能够在同一个工作簿中打开多个文件,名为p00001、p00002、p00003等等。有人知道我如何编辑代码来选择所有以这种方式命名的文件,并在同一工作簿中的单独工作表中打开它们吗 我的代码是: Sub Open_Workbook() Dim my_FileName As Variant my_FileName = Application.GetOpenFilename If my_FileName &l

我有一个代码,允许我在excel工作簿中打开一个文件,但是我希望能够在同一个工作簿中打开多个文件,名为p00001、p00002、p00003等等。有人知道我如何编辑代码来选择所有以这种方式命名的文件,并在同一工作簿中的单独工作表中打开它们吗

我的代码是:

Sub Open_Workbook()

    Dim my_FileName As Variant

    my_FileName = Application.GetOpenFilename

    If my_FileName <> False Then
        Workbooks.Open Filename:=my_FileName
    End If

End Sub
Sub-Open_工作簿()
将my_文件名设置为变体
my_FileName=Application.GetOpenFilename
如果我的文件名为False,那么
工作簿。打开文件名:=我的文件名
如果结束
端接头

在这个解决方案中,我使用文件对话框来选择多个文件。 之后,您需要循环所有这些文件。 在For循环中,必须打开文件并导入图纸。在本例中,我导入了工作簿中的所有工作表。 完成代码导入后,关闭源工作簿,并对其余文件执行相同操作

Sub Import Files()
        Dim sheet As Worksheet
        Dim total As Integer
        Dim intChoice As Integer
        Dim strPath As String
        Dim i As Integer
        Dim wbNew As Workbook
        Dim wbSource As Workbook
        Set wbNew = Workbooks.Add


        'allow the user to select multiple files
        Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = True
        'make the file dialog visible to the user
        intChoice = Application.FileDialog(msoFileDialogOpen).Show

        Application.ScreenUpdating = False
        Application.DisplayAlerts = False

        'determine what choice the user made
        If intChoice <> 0 Then
            'get the file path selected by the user
            For i = 1 To Application.FileDialog(msoFileDialogOpen).SelectedItems.Count
                strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(i)

                Set wbSource = Workbooks.Open(strPath)

                For Each sheet In wbSource.Worksheets
                    total = wbNew.Worksheets.Count
                    wbSource.Worksheets(sheet.Name).Copy _
                    after:=wbNew.Worksheets(total)
                Next sheet

                wbSource.Close
            Next i
        End If

    End Sub
子导入文件()
将工作表设置为工作表
将总计设置为整数
选择整数
将strPath设置为字符串
作为整数的Dim i
以工作簿的形式新建
将wbSource设置为工作簿
设置wbNew=工作簿。添加
'允许用户选择多个文件
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect=True
'使文件对话框对用户可见
intChoice=Application.FileDialog(msoFileDialogOpen.Show)
Application.ScreenUpdating=False
Application.DisplayAlerts=False
'确定用户所做的选择
如果选择0,那么
'获取用户选择的文件路径
对于i=1的Application.FileDialog(msoFileDialogOpen),选择editems.Count
strPath=Application.FileDialog(msoFileDialogOpen)。选择editems(i)
设置wbSource=Workbooks.Open(strPath)
对于wbSource.工作表中的每个工作表
总计=wbNew.Worksheets.Count
wbSource.Worksheets(sheet.Name).复制_
之后:=wbNew.工作表(总计)
下一页
wbSource,关闭
接下来我
如果结束
端接头
如果要从某个目录获取所有文件,可以使用循环更改ApplicationFile对话框,方法如下:

 directory = "c:\test\"
    fileName = Dir(directory & "*.xl??")
    Do While fileName <> ""
    'Put Code From For Loop here.
    Loop
directory=“c:\test\”
fileName=Dir(目录&“*.xl??”)
文件名“”时执行此操作
'将For循环中的代码放在此处。
环

这些文件是什么类型的?你可以循环浏览文件夹中的所有Excel文件并执行你想要的操作,这样行吗?这些文件是dat文件,是的,我希望能够以某种方式循环