复制信息的工作表 目标表目前正在寻找一种最有效的方法,可以将数据从一个工作表(从第三方系统中提取的报表的输出)复制和粘贴到另一个工作表中,如果愿意的话,该工作表是一个稳定的“遗留文件”。从本质上说,目标文件是不同年份/月数据的存储库,列标题包含三个不同的类别(现在我们可以称它们为x、y、z)。这些类别都有不同的属性,例如,让我们关注x,这将有4个属性附加到它,想想看
根据从系统中提取的信息。我要想办法:
我现在正在进行这个过程,这是非常耗时的,以前是手动完成的,我不愿意花2-3个小时去做一些可以自动化的事情。我已经开始编写下面的代码,但是仍然停留在粘贴基于列标题的特殊值上:
Dim wsCopy As Worksheet
Dim wsRR As Worksheet
Dim wsRRPiv As Worksheet
Dim lcopylastrow As Long
Dim lRRLastRow As Long
'Setting variables for report support workbook as well as download
'
Set wsCopy = ActiveWorkbook.Worksheets(1)
Set wsRR = ThisWorkbook.Worksheets("Data")
Set wsRRPiv = ThisWorkbook.Worksheets("Pivot")
'This will activate the workbook and work sheet which as the data which will then be moved from into this sheet
'
lcopylastrow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lRRLastRow = wsRR.Cells(wsRR.Rows.Count, "A").End(xlUp).Offset(1).Row
' Unmerging cells within download
'
ActiveWorkbook.Worksheets(1).Range("A3:G" & lcopylastrow).UnMerge
' This section of code will introduce the copy and pasting necessary to get the data into the consolidation of the report. As the report can only handle two separate Copy and pastes at a time, this first section will focus on getting the correct data sets for the left most columns (Sales & Cases)
'
wsCopy.Range("A8:D" & lcopylastrow).Copy _
wsRR.Range("C" & lRRLastRow)
wsCopy.Range("E8:E" & lcopylastrow).Copy _
wsRR.Range("AR" & lRRLastRow)
wsCopy.Range("F8:F" & lcopylastrow).Copy _
wsRR.Range("AR" & lRRLastRow)
wsCopy.Range("G8:G" & lcopylastrow).Copy _
wsRR.Range("AR" & lRRLastRow)
End Sub发布于 2021-12-16 22:15:58
我将补充这个答案,并继续改进它,因为我更好地理解你的目标。
当将数据聚合到现有表中时,一种常见且可取的策略是逐行进行。这样,您可以搜索表以查看记录是否已经存在,您可以添加到现有记录、覆盖它或跳过它。
我编写了一个示例,说明如何逐行处理您的文件:
Sub Example()
Dim wsCopy As Worksheet
Dim wsRR As Worksheet
Dim wsRRPiv As Worksheet
Dim lcopylastrow As Long
Dim lRRLastRow As Long
'Setting variables for report support workbook as well as download
'
Set wsCopy = ActiveWorkbook.Worksheets(1)
Set wsRR = ThisWorkbook.Worksheets("Data")
Set wsRRPiv = ThisWorkbook.Worksheets("Pivot")
lcopylastrow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lRRLastRow = wsRR.Cells(wsRR.Rows.Count, "A").End(xlUp).Offset(1).Row
' Unmerging cells within download
'
wsCopy.Range("A3:G" & lcopylastrow).UnMerge
' This section of code will introduce the copy and pasting necessary to get the data into the consolidation of the report. As the report can only handle two separate Copy and pastes at a time, this first section will focus on getting the correct data sets for the left most columns (Sales & Cases)
'
Const wsCopy_STARTING_ROW As Integer = 6 'First row containing data in wsCopy
Const wsRR_STARTING_ROW As Integer = 5 'First row containing data in wsRR
Dim i As Long
For i = wsCopy_STARTING_ROW To lcopylastrow
'For each row of data in wsCopy
'save a reference to the current row
Dim CurrentRow As Range
Set CurrentRow = wsCopy.Rows(i)
'Grab all data values from the current row
'ASSUMES DATA IS IN COLUMNS 1 to 7 (A:G)
Dim CustomerID As String, CostCenter As String, ProductID As String, ProductName As String, WSV As String, LBS_Sold As String, Tubes_Sold As String
CustomerID = CurrentRow.Cells(1).Value
CostCenter = CurrentRow.Cells(2).Value
ProductID = CurrentRow.Cells(3).Value
ProductName = CurrentRow.Cells(4).Value
WSV = CurrentRow.Cells(5).Value
LBS_Sold = CurrentRow.Cells(6).Value
Tubes_Sold = CurrentRow.Cells(7).Value
'Try to find the destination row in wsRR using the CustomerID and ProductID
Dim rDestination As Range
Dim j As Long
For j = wsRR_STARTING_ROW To lRRLastRow
'For each row in wsRR
'Compare the customerID and ProductID in wsRR to the ones from wsCopy
'ASSUMES CUSTOMER ID is in wsRR COLUMN B and PRODUCT ID is in wsRR COLUMN D
If wsRR.Cells(j, 2) = CustomerID And wsRR.Cells(j, 4) = ProductID Then
Set rDestination = wsRR.Rows(j)
Exit For
End If
Next
If rDestination Is Nothing Then
'If no destination row was found, add it to the bottom of wsRR
Set rDestination = wsRR.Rows(lRRLastRow)
'ASSUMES CUSTOMER ID to PRODUCT DESCRIPTION ARE IN wsRR COLUMNS B:E
rDestination.Cells(2) = CustomerID
rDestination.Cells(3) = CostCenter
rDestination.Cells(4) = ProductID
rDestination.Cells(5) = ProductName
lRRLastRow = lRRLastRow + 1
End If
'At this point we have all the data from wsCopy and we have identified a destination row in wsRR
'I don't know what to put in "METRIC" or what to do with Walled Sales Value, Sales Pounds, Tubes Sold
'You can do stuff like
'rDestination.Cells(1) = "WSV"
'rDestination.Cells(10) = CDbl(WSV)
'Which would put the string "WSV" in column A and then put the Walled Sales Value as a number into column J
Next
End Subhttps://stackoverflow.com/questions/70371200
复制相似问题