【问题标题】:How to auto populate a single spreadsheet from multiple spreadsheets如何从多个电子表格中自动填充单个电子表格
【发布时间】:2013-10-29 08:56:32
【问题描述】:

我有一组 Excel 电子表格要汇总。我的床单编号: xxx-yy-zzzz; xxx-yy-zzz+1;等等。

我想要一个报告电子表格以在每次打开时检索信息。我不介意使用 VBA 或公式。

我有下面的宏。我需要自动递增,直到用完电子表格。所有文件将在同一个文件夹中,此文件可以在任何文件夹中。

Sub Macro1()

'
' Macro1 Macro
' autopop
'
'
    Range("C4").Select
    ActiveCell.FormulaR1C1 = "='[413-05-001.xlsx]Cover'!R4C5"
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "='[413-05-001.xlsx]Cover'!R5C3"
    Range("E4").Select
    ActiveCell.FormulaR1C1 = "='[413-05-001.xlsx]Order'!R27C9"
    Range("F4").Select
    ActiveCell.FormulaR1C1 = "='[413-05-001.xlsx]Cover'!R8C9"

End Sub

【问题讨论】:

  • 我认为您的意思是工作簿已编号?你想在第 5 行显示来自413-05-002 的数据吗?如果是,有多少工作簿?
  • 这正是我的意思......我不知道有多少工作簿 - 它可以是开放式的,所以我希望递归可以继续检查,直到它用完工作簿。 ..

标签: excel vba


【解决方案1】:

当我们使用非常简单的文件名时,上述 Siddharth 的方法非常有效,但是当文件名中添加了内容时,它变得更加困难......所以我做了一些冲浪并找到了“列出所有”的基础文件并将它们放在工作表中”并使用上面 Siddharth 回答中的一些代码(非常感谢 Siddharth 先生)和我在网上找到的示例 http://alanmurray.blogspot.com/2013/08/excel-vba-list-all-excel-files-in-folder.html ,我现在已经完成了我的代码和我的小 VBA 应用程序做我想做的事 - 它打开一个文件夹并通过并拉出特定的单元格并在几秒钟内创建一个摘要报告 -> 将为我节省数小时的繁琐工作......

代码:

Sub ImportFileList()
Dim MyFolder As String 'Store the folder selected by the using
Dim FiletoList As String 'store the name of the file ready for listing
Dim NextRow As Long 'Store the row to write the filename to

On Error Resume Next

Application.ScreenUpdating = False

'Display the folder picker dialog box for user selection of directory
With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Please select a folder"
    .Show
    .AllowMultiSelect = False

    If .SelectedItems.Count = 0 Then
        MsgBox "You did not select a folder"
        Exit Sub
    End If
    MyFolder = .SelectedItems(1) & "\"
End With

'Dir finds the first Excel workbook in the folder
FiletoList = Dir(MyFolder & "*.xls")
Range("A1").Value = "Filename"
Range("B1").Value = "Purchase Order Number" 
Range("C1").Value = "Vendor"
Range("D1").Value = "Date of PO"
Range("E1").Value = "Currency"
Range("F1").Value = "Subtotal"
Range("G1").Value = "VAT"
Range("H1").Value = "Total"
Range("A1:H1").Font.Bold = True

'Find the next empty row in the list
NextRow = Application.CountA(Range("A:A")) + 1 
NextRow = NextRow + 1 ' skip a line

'Do whilst the dir function returns an Excel workbook
Do While FiletoList <> ""
    Cells(NextRow, 1).Value = FiletoList 'Write the filename into the next available cell
    Cells(NextRow, 2).Formula = "='[" & FiletoList & "]Cover'!R4C4" ' Cover is the excel sheet name
    Cells(NextRow, 3).Formula = "='[" & FiletoList & "]Cover'!R6C3"
    Cells(NextRow, 4).Formula = "='[" & FiletoList & "]Cover'!R4C7"
    Cells(NextRow, 5).Formula = "='[" & FiletoList & "]Cover'!R21C4"
    Cells(NextRow, 6).Formula = "='[" & FiletoList & "]Cover'!R19C5"
    Cells(NextRow, 7).Formula = "='[" & FiletoList & "]Cover'!R20C5"
    Cells(NextRow, 8).Formula = "='[" & FiletoList & "]Cover'!R21C5"
    NextRow = NextRow + 1 'Move to next row
    FiletoList = Dir 'Dir returns the next Excel workbook in the folder
Loop

Application.ScreenUpdating = True

End Sub

【讨论】:

【解决方案2】:

这是你正在尝试的吗? (未测试

'~~> Change this to the directory which has .xlsx files
Const sDir = "C:\Temp\"

Sub Sample()
    Dim ws As Worksheet
    Dim i As Long, num As Long, Calcmode As Long
    Dim FilesCount As Long, startNum As Long

    On Error GoTo Whoa

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With Application
        .ScreenUpdating = False
        Calcmode = .Calculation
        .Calculation = xlCalculationManual
    End With

    '~~> Get the number of files in that directory
    FilesCount = getFileCount(sDir)

    startNum = 1

    If FilesCount <> 0 Then
        With ws
            For i = 4 To (FilesCount + 3)
                num = Format(startNum, "000")

                .Range("C" & i).Formula = "='[413-05-" & num & ".xlsx]Cover'!R4C5"
                .Range("D" & i).Formula = "='[413-05-" & num & ".xlsx]Cover'!R5C3"
                .Range("E" & i).Formula = "='[413-05-" & num & ".xlsx]Order'!R27C9"
                .Range("F" & i).Formula = "='[413-05-" & num & ".xlsx]Cover'!R8C9"

                startNum = startNum + 1
            Next i
        End With
    End If

LetsContinue:
    With Application
        .ScreenUpdating = True
        .Calculation = Calcmode
    End With
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

Function getFileCount(s As String) As Long
    Dim Path As String, Filename As String
    Dim Count As Long

    Path = s & "*.xlsx"

    Filename = Dir(Path)

    Do While Filename <> ""
        Count = Count + 1
        Filename = Dir()
    Loop

    getFileCount = Count
End Function

【讨论】:

  • Path = s &amp; "\*.xlsx" - 这里多了一个“\”
  • @sam092:谢谢 :) 我已经修改了。
  • 这看起来绝对很棒......我会测试它并稍后报告......(我现在不在我需要这个的机器上)非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 2018-02-10
  • 2014-01-27
  • 2015-05-21
相关资源
最近更新 更多