2016-07-18 205 views
-2

这个问题不应该很复杂。我有一个大文件夹,里面有200个单独的文件夹。现在,每个文件夹都有一个Excel表格。我想在控制文件夹(200旁边)的vba文件中有一些代码,它可以迭代200个文件夹并更改每个excel文件中的一位数据。我发现目录的东西和文件夹迭代,但我无法在这里和那里合并它们,我需要一些简单的帮助。如何编辑多个excel文件,每个文件位于一个文件夹中的不同文件夹中

我的代码目前是:`子的button1_Click()

Dim wb  As Workbook 
Dim ws  As Excel.Worksheet 
Dim iIndex As Integer 
Dim strPath As String 
Dim strFile As String 

'Get the directories 
strPath = "C:\Users\generaluser\Desktop\testing main folder\" 
strFile = Dir(strPath, vbDirectory) 

'Loop through the dirs 
Do While strFile <> "" 

    'Open the workbook. 
    strFileName = Dir(strPath & strFile & "New Microsoft Excel Worksheet.xlsm", vbDirectory) 
    'Open the workbook. 
    Set wb = Workbooks.Open(Filename:=strPath & strFile & "\" & strFileName, ReadOnly:=False) 

    'Loop through the sheets. 

    Set ws = Application.Worksheets(1) 

    'Do whatever 



    'Close the workbook 
    wb.Close SaveChanges:=True 

    'Move to the next dir. 
    strFile = Dir 
Loop 

末次 `

请帮@MatthewD

+1

如果你没有向我们展示你编写的代码以使它工作,那么这很复杂。从概念上讲,它可能很简单,就是这样,但是没有代码,我们没有办法可以梦想为项目框架着色所需的具体细节。如果您发布了您尝试过的代码,我们可以帮助您进行编辑。没有多少人愿意为你做。 –

+0

好吧,我不确定从哪里开始。我知道代码需要先抓住主文件夹,然后遍历每个文件夹。然后对于我知道的每个文件夹,需要制作工作簿,然后调用第一张工作表,然后进行必要的编辑。它只是一个循环和一些代码,但我不知道如何调用原始文件夹的目录,然后循环其余的所有内容。 –

回答

1

既然你没有显示的代码,它是这样的。

Dim wb  As Workbook 
    Dim ws  As Excel.Worksheet 
    Dim iIndex As Integer 
    Dim strPath As String 
    Dim strFile As String 

    'Get the directories 
    strPath = "c:\temp\" 
    strFile = Dir(strPath, vbDirectory) 

    'Loop through the dirs 
    Do While strFile <> "" 

     'Open the workbook. 
     Set wb = Workbooks.Open(filename:=strPath & strFile & "\filename.xlsx", ReadOnly:=True) 

     'Loop through the sheets. 
     For iIndex = 1 To Application.Worksheets.count 
      Set ws = Application.Worksheets(iIndex) 

      'Do whatever 

     Next iIndex 

     'Close the workbook 
     wb.Close SaveChanges:=False 

     'Move to the next dir. 
     strFile = Dir 
    Loop 

如果工作簿名称未知,则必须在目录中指定xlsx文件。

strFileName = Dir(strPath & strFile & "*.xlsx") 
    'Open the workbook. 
    Set wb = Workbooks.Open(filename:=strPath & strFile & "\" & strFileName , ReadOnly:=True) 
+1

为什么保存更改= false。如果我想保存只是改变为真? –

+0

为什么'ReadOnly'如果用户需要更改文件?而且,代码如何循环子文件夹 - > OP究竟在问什么? –

+0

和strPath是主文件夹? –

0

好的,这应该很容易。只需递归列出所有文件夹中的每个文件。下面的脚本将为您做到这一点。

Sub ListAllFiles() 
    SearchForFiles "C:\Users\rshuell001\Desktop\YourFolder\", "writefilestosheet", "*.*", True, True 
End Sub 

Sub searchForFiles(ByVal DirToSearch As String, ByVal ProcToCall As String, _ 
     Optional ByVal FileTypeToFind As String = "*.*", _ 
     Optional ByVal SearchSubDir As Boolean = False, _ 
     Optional ByVal FilesFirst As Boolean = False) 
    On Error GoTo ErrXIT 
    If Right(DirToSearch, 1) <> Application.PathSeparator Then _ 
     DirToSearch = DirToSearch & Application.PathSeparator 

If FilesFirst Then processFiles DirToSearch, ProcToCall, FileTypeToFind 
If SearchSubDir Then processSubFolders DirToSearch, ProcToCall, _ 
    FileTypeToFind, SearchSubDir, FilesFirst 

    If Not FilesFirst Then _ 
     processFiles DirToSearch, ProcToCall, FileTypeToFind 
    Exit Sub 
ErrXIT: 
    MsgBox "Fatal error: " & Err.Description & " (Code=" & Err.Number & ")" 
    Exit Sub 
End Sub 


Private Sub processFiles(ByVal DirToSearch As String, _ 
      ByVal ProcToCall As String, _ 
      ByVal FileTypeToFind As String) 
     Dim aFile As String 
     aFile = Dir(DirToSearch & FileTypeToFind) 
     Do While aFile <> "" 
      Application.Run ProcToCall, DirToSearch & aFile 
      aFile = Dir() 
      Loop 
End Sub 


Sub writeFilesToSheet(ByVal aFilename As String) 
    With ActiveSheet 
    .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Value = aFilename 
     End With 
End Sub 

接下来,你知道的,你需要访问的每个文件,使用上述技术,打开每个做出更改,保存它,并关闭文件。使用以下URL中描述的技术进行更改。

http://www.rondebruin.nl/win/s3/win010.htm

你必须修改脚本只是有点,因为看起来对所有文件上的一个文件夹,你需要罗恩的脚本,通过你的第一个脚本创建不同的路径运行

相关问题