简体   繁体   English

如何使用VBA在具有通用名称的文件中打开某些Excel文件夹

[英]How to open certain excel folders in a file with a common name using vba

My problem is that I need to open some excel files using VBA (for excel 2007) and extract the data. 我的问题是我需要使用VBA打开某些excel文件(对于excel 2007)并提取数据。 All the files I want to open are called "profit for January.xlsx", "profit for February.xlsx", and so on with only the month name changing, so I think I want to open a file called "profit for*". 我要打开的所有文件都被称为“ January.xlsx的利润”,“ February.xlsx的利润”,依此类推,仅更改月份名称,所以我想打开一个名为“ profit for *”的文件。 。 There is another file in the folder called "total revenue.xlsx" that I do not want to open. 我不想打开文件夹中的另一个文件“ total income.xlsx”。

If possible, I need the code to extract the data from the files in the folder, wherever the folder may be because I am sending this code to my colleagues to put into their own folders, which have the same file name formats etc but different paths. 如果可能的话,我需要代码从文件夹中的文件夹中提取数据,无论文件夹是在什么地方,因为我将此代码发送给同事放入自己的文件夹中,这些文件夹具有相同的文件名格式等,但路径不同。

I have the code to extract the data, which works, but it either imports all the data in the folder or none at all! 我有提取数据的代码,这行得通,但是它要么导入文件夹中的所有数据,要么根本不导入任何数据!

Any help on this would be much appreciated as I am an intern trying to get his foot in the door, and this would be quite a big break for me! 在这方面的任何帮助将不胜感激,因为我是一名实习生,试图让他站起来,这对我来说是一个很大的突破!

Further Information 更多信息

So far I have the code below (I haven't included the dim's because I felt they may be unnecessary?), which I have drawn from other websites. 到目前为止,我有下面的代码(我没有包括暗淡的代码,因为我认为它们可能是不必要的吗?),我从其他网站获得了此代码。 I'm also finding that, in trying to open all the files in the folder, it is trying to open itself! 我还发现,在尝试打开文件夹中的所有文件时,它正在尝试自行打开! If anyone could suggest how to improve this, I would be very grateful. 如果有人可以提出改进建议,我将不胜感激。 I haven't been using VBA for very long, and have been finding this assignment quite tough! 我使用VBA已有很长时间了,并且发现这项任务非常艰巨!

The error box that comes up sometimes says that I need an 'object' for the variable sfilename, and I'm not sure how to do that without messing up another part of the code. 出现的错误框有时表示我需要变量sfilename的“对象”,而且我不确定如何在不弄乱代码另一部分的情况下执行此操作。

sub import data ()

ChDir ThisWorkbook.Path

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set sfolder = objFSO.GetFolder(ThisWorkbook.Path)

    For Each sfilename In sfolder.Files

        If sfilename <> "Total Revenue.xlsx" Then

            Workbooks.Open Filename:= _
                sfilename                             *'open the file*

            Set sfilename = ActiveWorkbook   *'set the file name as sfilename, so the single piece of code will work with the copy-loop*

            b = Sheets.Count                                *'for the data-import loop*

            Call ImportData                                 *'call in the loop*
            sfilename.Close                                 *'close the file*

        End If

    Next

end sub

What are you using at the moment? 您现在在用什么? For each file in folder? 对于文件夹中的每个文件?

Possibilities include 可能包括

  • FileSystemObject FileSystemObject
  • Dir 迪尔
  • For i=1 to 12 对于i = 1到12
    Monthname(i) 月名(i)

EDIT 编辑

Sub import_data()

    sPath = ThisWorkbook.Path
    sTemplate = "\profit for qqq.xls"

    For i = 1 To 12
        sFileName = Replace(sTemplate, "qqq", MonthName(i))

        ''Just checking
        If Dir(sPath & sFileName) <> "" Then
            Workbooks.Open Filename:= _
                sPath & sFileName
                'open the file*

            Set sFileName = ActiveWorkbook
            'set the file name as sfilename, so the single
            'piece of code will work with the copy-loop*

            b = Sheets.Count
            '*'for the data-import loop*

            ''Call ImportData
            '*'call in the loop*
            sFileName.Close
            '*'close the file*
        End If
    Next

End Sub

So far I have the code below (I haven't included the dim's because I felt they may be unnecessary?), which I have drawn from other websites. 到目前为止,我有下面的代码(我没有包括暗淡的代码,因为我认为它们可能是不必要的吗?),我从其他网站获得了此代码。 I'm also finding that, in trying to open all the files in the folder, it is trying to open itself! 我还发现,在尝试打开文件夹中的所有文件时,它正在尝试自行打开! If anyone could suggest how to improve this, I would be very grateful. 如果有人可以提出改进建议,我将不胜感激。 I haven't been using VBA for very long, and have been finding this assignment quite tough! 我使用VBA已有很长时间了,并且发现这项任务非常艰巨!

The error box that comes up sometimes says that I need an 'object' for the variable sfilename, and I'm not sure how to do that without messing up another part of the code. 出现的错误框有时表示我需要变量sfilename的“对象”,而且我不确定如何在不弄乱代码另一部分的情况下执行此操作。

Many thanks and kindest regards, Mark 马克,非常感谢和亲切的问候

sub import data () 子导入数据()

ChDir ThisWorkbook.Path ChDir ThisWorkbook.Path

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set sfolder = objFSO.GetFolder(ThisWorkbook.Path)

For Each sfilename In sfolder.Files

    If sfilename <> "Total Revenue.xlsx" Then

        Workbooks.Open Filename:= _
            sfilename                             *'open the file*

        Set sfilename = ActiveWorkbook   *'set the file name as sfilename, so the single piece of code will work with the copy-loop*

        b = Sheets.Count                                *'for the data-import loop*

        Call ImportData                                 *'call in the loop*
        sfilename.Close                                 *'close the file*

    End If

Next

end sub 结束子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA:打开固定文件路径,但没有固定文件名 - Excel VBA: open fixed file path but not fixed file name 如何循环遍历文件夹 A 的子文件夹以获取每个子文件夹中的文件名并使用 VBA 从文件夹 B 复制其他同名文件 - How to loop through sub folders of folder A to get file name in each subfolder and copy other file with same name from folder B using VBA 如何使用TCL打开现有的Excel文件? - How to open existing Excel file using TCL? 如何使用VBA打开文件浏览器并嵌入文件? - How to open file browser and emded a file using VBA? 使用.bat按文件名将文件排序到文件夹中 - Sorting files into folders by file name using .bat 如何让用户选择一个已经打开的 Excel 文件来导入数据,然后粘贴到他们选择的文件中? (VBA) - How do I have a user select an already open excel file to import data to, and paste into the file they choose? (VBA) 使用不同文件夹中的文件将某些文件信息输出到文本文件 - Output certain file info to text file using files in different folders 如何打开文件中的每个 word 文档并提取数据以放入 Excel VBA 中的单元格 - How can I open each word document in my file and extract data to put into a cell in Excel VBA 如何使用_TCHAR *作为文件名打开文件? C / C ++ - How to open a file using _TCHAR* as a file name? c/c++ 在使用Open XML下载之前,如何压缩Excel文件? - How to zip Excel file before it is downloading using Open XML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM