简体   繁体   English

用另一个工作表单元格上的数据填充一个工作表的多个单元格条目

[英]Populate multiple cell entries of one sheet with data on another sheet cells

I have a spreadsheet #1 which has rows of data and values in many columns such as Column A being ORDER DATE, column B being Region Column C as Reps etc.(Total M rows)我有一个电子表格#1,它在许多列中有数据行和值,例如 A 列是 ORDER DATE,B 列是区域 C 列作为代表等。(总共 M 行)

I have another spreadsheet #2 with other columns and many rows.我有另一个电子表格#2,其中包含其他列和许多行。 Column A is Item, Column B is Region, column C is a number of items and so forth (Total N rows) A 列是项目,B 列是区域,C 列是项目数,依此类推(总共 N 行)

I would like a macro which would populate data in sheet 1 with all the data in sheet 2. For example if 10 rows are present in sheet 1 and five rows sheet 2 then sheet 3 must have 50 rows ie all the five rows of sheet 2 must be populated with each individual rows of sheet 1.我想要一个宏,它将用工作表 2 中的所有数据填充工作表 1 中的数据。例如,如果工作表 1 中有 10 行,工作表 2 中有五行,那么工作表 3 必须有 50 行,即工作表 2 的所有五行必须填充工作表 1 的每一行。

Note: The number of columns are not static (their is no fixed structure for no. of columns in both the sheets)注意:列数不是静态的(对于两个工作表中的列数,它们没有固定的结构)

I have provided screenshots for better understanding (Sheet 1, Sheet 2 and Sheet 3):我提供了截图以便更好地理解(表 1、表 2 和表 3):

表 1 表 2 表 3

I have tried to append data column wise but I am not able to create repetitions for data in sheet 2 Currently my code is only joining the columns of sheets 1 and sheet 2 but nit able to create m X n rows我试图明智地附加数据列,但我无法为工作表 2 中的数据创建重复当前我的代码仅连接工作表 1 和工作表 2 的列,但不能创建 m X n 行

Sub ColumnsPaste()
Dim Source As Worksheet
Dim Destination As Worksheet
Dim Last As Long

Application.ScreenUpdating = False

Set Destination = Worksheets.Add(after:=Worksheets("Sheet1"))
Destination.Name = "Sheet3"

For Each Source In ThisWorkbook.Worksheets        
    If Source.Name <> "Sheet3" Then
        Last = Destination.Range("A1").SpecialCells(xlCellTypeLastCell).Column
    
        If Last = 1 Then
            Source.UsedRange.Copy Destination.Columns(Last)
        Else
            Source.UsedRange.Copy Destination.Columns(Last + 1)
        End If
    End If
Next

Columns.AutoFit

Application.ScreenUpdating = True
End Sub

My VBA code output:我的 VBA 代码输出: 我的 VBA 代码输出

Please, use the next code.请使用下一个代码。 It will be fast enough, using clipboard only for copying the header.它会足够快,仅使用剪贴板来复制标题。 It assumes that the range to be processed exists in columns "A:C" (starting from the second row) and returns in column "D:D":它假定要处理的范围存在于“A:C”列(从第二行开始)并在“D:D”列中返回:

Sub CombiteSheets()
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, lastR1 As Long, lastR2 As Long, lastR3 As Long
  Dim lastCol1 As Long, lastCol2 As Long, repRows As Long, arr2, i As Long, iRow As Long
  
   Set sh1 = ActiveSheet 'use here the sheet you need
   Set sh2 = sh1.Next       'use here the sheet you need
   Set sh3 = sh2.Next       'the same...
   
    lastR1 = sh1.Range("A" & sh1.rows.count).End(xlUp).row  'last row on the first sheet
    lastR2 = sh2.Range("A" & sh2.rows.count).End(xlUp).row  'last row on the second sheet
    lastCol1 = sh1.cells(1, sh1.Columns.count).End(xlToLeft).Column 'last column on the first sheet
    lastCol2 = sh2.cells(1, sh2.Columns.count).End(xlToLeft).Column 'last columln on the second sheet
    repRows = lastR2 - 1
    
    arr2 = sh2.Range("A2", sh2.cells(lastR2, lastCol2)).value 'place the range in an array for faster processing
    
    On Error GoTo SaveExit
    'some code optimization:
    Application.ScreenUpdating = False: Application.Calculation = xlCalculationManual: Application.EnableEvents = False
    
    For i = 2 To lastR1
        sh3.Range("A" & i + iRow).Resize(repRows, lastCol1).value = _
                                   sh1.Range("A" & i, sh1.cells(i, lastCol1)).value
        sh3.cells(i + iRow, lastCol1 + 1).Resize(UBound(arr2), UBound(arr2, 2)).value = arr2
        iRow = iRow + repRows - 1
    Next i
    
    'copy headers:
    sh1.Range("A1", sh1.cells(1, lastCol1)).Copy sh3.Range("A1")
    sh2.Range("A1", sh2.cells(1, lastCol2)).Copy sh3.cells(1, lastCol1 + 1)
    
    'a little formatting:
    someFormat sh3.Range("A1", sh3.cells(UBound(arr2) * (lastR1 - 1) + 1, lastCol1 + lastCol2))

    Application.ScreenUpdating = True: Application.Calculation = xlCalculationAutomatic: Application.EnableEvents = True
    
    MsgBox "Ready..."
    Exit Sub
    
SaveExit:
   Application.ScreenUpdating = True: Application.Calculation = xlCalculationAutomatic: Application.EnableEvents = True
   MsgBox err.Description, vbCritical, err.Number
End Sub

Private Sub someFormat(rng As Range) 'formatting the returned range
 Dim i As Long
 rng.EntireColumn.AutoFit
 For i = 7 To 12
        With rng.Borders(i)
            .LineStyle = xlContinuous
            .ColorIndex = 47
            .Weight = xlThin
        End With
        Next i
End Sub

Please, send some feedback after testing it...请在测试后发送一些反馈...

暂无
暂无

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

相关问题 如何根据另一张表中各列的条目填充单元格内容 - How to populate cell contents based on entries in the columns from the another sheet 如何在一张纸到另一张纸中的特定单元格的范围内复制单元格数据? - How do you copy cell data in a range from one sheet to specific cells in another sheet? 如果第三个单元格数据变量与第一个工作表变量匹配,Excel会将两个数据单元格从一个工作表复制到另一工作表 - Excel copy two cells of data from one sheet to another sheet if a third cell data variable matches the first sheet variable 无法将数据从一张纸填充到另一张纸 - Not able to populate data from one sheet to another sheet 根据标准将数据从一张表合并到另一张表,并仅填充特定单元格 - Merge data from one sheet to another based on criteria and only populate specific cells 根据另一个单元格中的条件将单元格从一个工作表复制到另一个工作表 - Copy cells from one sheet to another based on criteria in another cell 打印同一工作表的多份副本,但用另一工作表中的列表(范围)中的数据替换一个单元格 - Print multiple copies of the same sheet, but replace one cell with the data from a list (range) from another sheet Excel-使用母版工作表上的一个单元格填充另一选项卡上的多个信息 - Excel - Using one cell on a master sheet to populate multiple info on another tab 将一个单元格数据从多个工作表复制到主工作表 - copy one cell data from multiple sheet to master sheet 根据公用单元格将单元格从一张纸添加到另一张纸 - Adding cells from one sheet to another based on common cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM