簡體   English   中英

如何使用VBA在具有通用名稱的文件中打開某些Excel文件夾

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

我的問題是我需要使用VBA打開某些excel文件(對於excel 2007)並提取數據。 我要打開的所有文件都被稱為“ January.xlsx的利潤”,“ February.xlsx的利潤”,依此類推,僅更改月份名稱,所以我想打開一個名為“ profit for *”的文件。 。 我不想打開文件夾中的另一個文件“ total income.xlsx”。

如果可能的話,我需要代碼從文件夾中的文件夾中提取數據,無論文件夾是在什么地方,因為我將此代碼發送給同事放入自己的文件夾中,這些文件夾具有相同的文件名格式等,但路徑不同。

我有提取數據的代碼,這行得通,但是它要么導入文件夾中的所有數據,要么根本不導入任何數據!

在這方面的任何幫助將不勝感激,因為我是一名實習生,試圖讓他站起來,這對我來說是一個很大的突破!

更多信息

到目前為止,我有下面的代碼(我沒有包括暗淡的代碼,因為我認為它們可能是不必要的嗎?),我從其他網站獲得了此代碼。 我還發現,在嘗試打開文件夾中的所有文件時,它正在嘗試自行打開! 如果有人可以提出改進建議,我將不勝感激。 我使用VBA已有很長時間了,並且發現這項任務非常艱巨!

出現的錯誤框有時表示我需要變量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

您現在在用什么? 對於文件夾中的每個文件?

可能包括

  • FileSystemObject
  • 迪爾
  • 對於i = 1到12
    月名(i)

編輯

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

到目前為止,我有下面的代碼(我沒有包括暗淡的代碼,因為我認為它們可能是不必要的嗎?),我從其他網站獲得了此代碼。 我還發現,在嘗試打開文件夾中的所有文件時,它正在嘗試自行打開! 如果有人可以提出改進建議,我將不勝感激。 我使用VBA已有很長時間了,並且發現這項任務非常艱巨!

出現的錯誤框有時表示我需要變量sfilename的“對象”,而且我不確定如何在不弄亂代碼另一部分的情況下執行此操作。

馬克,非常感謝和親切的問候

子導入數據()

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

結束子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM