VBA - 将多个工作表中的一个单元格复制/粘贴到母版 sheet

VBA - copy / paste one cell from multiple workseets to master sheet

我有代码可以打开文件夹中的多个文件,将该文件的名称打印到主文件的第 1 列(继续向下列),关闭当前文件,然后移动到下一个文件,直到文件夹空。

在文件打开时我要复制的所有文件的单元格 J1(最好写为 1,10)中有信息,粘贴到第 4 列(继续向下列,等于名称每个文件),并继续关闭当前文件并继续前进。

我不知道如何只复制一个单元格,因为一个范围需要多行的信息。这是我的工作代码,用于遍历文件并仅打印其名称。有任何想法吗?谢谢!

Sub LoopThroughDirectory()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim MyFolder As String
    Dim Sht As Worksheet
    Dim i As Integer
    Dim LastRow As Integer, erow As Integer

    'Speed up process by not updating the screen
    'Application.ScreenUpdating = False

    MyFolder = "C:\Users\trembos\Documents\TDS\progress\"

    Set Sht = ActiveSheet

    'create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'get the folder object
    Set objFolder = objFSO.GetFolder(MyFolder)
    i = 1
    'loop through directory file and print names
    For Each objFile In objFolder.Files

        If LCase(Right(objFile.Name, 3)) = "xls" Or LCase(Left(Right(objFile.Name, 4), 3)) = "xls" Then
            'print file name
            Sht.Cells(i + 1, 1) = objFile.Name
            i = i + 1
            Workbooks.Open fileName:=MyFolder & objFile.Name

        End If

        'Macro recording of manual copy/paste but I want to apply on general scale
        'Range("J1").Select
        'Selection.Copy
        'Windows("masterfile.xlsm").Activate
        'Range("D2").Select
        'ActiveSheet.Paste
        ActiveWorkbook.Close SaveChanges:=False

        Next objFile

'Application.ScreenUpdating = True

End Sub

合并这个,重命名 "MySheet":

Option Explicit

Sub CopyFromSheets()

    Dim WB As Workbook
    Dim ws As Worksheet
    Dim i As Integer

    Set WB = ActiveWorkbook
    i = 1

    With WB
        For Each ws In .Worksheets
            With ws
                .Range("J1").Copy Workbooks("masterfile.xlsm").Sheets("MySheet").Cells(i, 10) 'Rename Mysheet
                i = i + 1
            End With
        Next ws
    End With
End Sub

应该这样做:

Option Explicit

Sub LoopThroughDirectory()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim MyFolder As String
    Dim Sht As Worksheet, ws As Worksheet
    Dim WB As Workbook
    Dim i As Integer
    Dim LastRow As Integer, erow As Integer

    Application.ScreenUpdating = False

    MyFolder = "C:\Users\trembos\Documents\TDS\progress\"

    Set Sht = Workbooks("masterfile.xlsm").Sheets("MySheet")

    'create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'get the folder object
    Set objFolder = objFSO.GetFolder(MyFolder)
    i = 1
    'loop through directory file and print names
    For Each objFile In objFolder.Files
        If LCase(Right(objFile.Name, 3)) = "xls" Or LCase(Left(Right(objFile.Name, 4), 3)) = "xls" Then
            'print file name

            Workbooks.Open Filename:=MyFolder & objFile.Name
            Set WB = ActiveWorkbook

            With WB
                For Each ws In .Worksheets
                    Sht.Cells(i + 1, 1) = objFile.Name
                    With ws
                        .Range("J1").Copy Sht.Cells(i + 1, 4)
                    End With
                    i = i + 1
                Next ws
                .Close SaveChanges:=False
            End With
        End If
    Next objFile

    Application.ScreenUpdating = True

End Sub