[英]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):
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
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.