简体   繁体   English

VBA-将多个单元中的一个单元格复制/粘贴到主表

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

I have code which opens multiple files in a folder, prints the name of that file into column 1 (continuing down the column) of a masterfile, closes the current file, and moves onto the next one until the folder is empty. 我有打开一个文件夹中的多个文件的代码,将该文件的名称打印到masterfile的第1列(继续该列),关闭当前文件,然后移至下一个,直到文件夹为空。

There is information in cell J1(preferably written as 1,10) of all of the files that I want to copy while the file is open, paste into column 4 (continuing down the column, equal with the names of each file), and continue to close the current file and move on. 在文件打开时,我要复制的所有文件的单元格J1(最好写为1,10)中都有信息,粘贴到第4列(继续此列,与每个文件的名称相等),然后继续关闭当前文件并继续。

I cannot figure out how to copy just one cell since a range requires information over multiple rows. 我无法弄清楚如何仅复制一个单元格,因为范围需要多行信息。 Here is my working code for looping through files and just printing their name. 这是我的工作代码,用于遍历文件并仅打印文件名。 Any ideas? 有任何想法吗? Thanks! 谢谢!

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

Incorporate this, renaming "MySheet": 合并此名称,重命名“ 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

This should do it: 应该这样做:

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

暂无
暂无

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

相关问题 将一个单元格数据从多个工作表复制到主工作表 - copy one cell data from multiple sheet to master sheet VBA:从一张纸到另一张纸的多次复制和粘贴 - VBA: multiple copy and paste from one sheet to another Excel VBA 从一个工作表中复制范围并将其一次一个单元格地粘贴到另一张工作表中 - Excel VBA copy range from one sheet and paste it one cell at a time in another sheet 在每个工作表匹配的单元格上复制一个单元格并将其粘贴到另一个VBA - Copy a cell on one sheet and paste it on another VBA based on cell from each sheet matching Excel 2013 VBA - 以编程方式从工作表2上的一个单元格复制文本,粘贴到Sheet1模块 - Excel 2013 VBA - Programmatically Copy text from one Cell on Sheet 2, Paste into Sheet1 Module 如何使用vba将多列数据从一张工作表复制并粘贴到另一张工作表 - How to copy and paste multiple column data from one sheet to another sheet using vba VBA 代码,用于根据列标题将粘贴数据从多个源工作簿复制到主数据工作簿(主数据表) - VBA code to copy paste data from multiple source workbooks to a master data workbook (Master data sheet) based on column headers 从多个工作表中复制相同单元格地址的相同非连续数据,并粘贴到一个主工作表中 - Copy same non contiguous data at same cell address from multiple worksheets and paste in one master worksheet VBA 根据日期将工作表 1 中的单元格值复制/粘贴到工作表 2 的宏 - VBA Macro to copy/paste cell value from sheet 1 to 2 based on date 将单元格值(非公式)从一张纸复制并粘贴到另一张纸 - Copy and paste cell values (not formulae) from one sheet to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM