Excel VBA将数据粘贴到新工作表中的依赖于条件的列

我的问题类似于已经提出的几个问题,但不同的是,我无法将其他解决scheme应用于我的问题。

我每周都会收集一个工作表中3个位置的数据,并且希望将这些数据复制到不同的工作表中,数据按照时间顺序排列。 我可以使用macrosloggingfunction为每个位置select数据,但我不知道如何编写指定另一个工作表的正确的目标列范围,数据应粘贴到哪里,这是由当前周数。 这使我可以跟踪/分析数据随着时间的推移。

为了说明,我有两个工作表,“总结”和“趋势”,结构如下:

SUMMARY Worksheet Week#: 5 Prod Loc1 Loc2 Loc3 A 70,000 22,000 35,000 B 95,000 65,000 150,000 C 115,200 402,250 110,500 TREND Worksheet Week: 1 2 … 5 6 Prod Loc1 A 84,000 112,000 70,000 ? B 114,000 152,000 95,000 ? C 138,240 184,320 115,200 ? Loc2 A 26,400 35,200 22,000 ? B 78,000 104,000 65,000 ? C 482,700 643,600 402,250 ? Loc3 A 42,000 56,000 35,000 ? B 180,000 240,000 150,000 ? C 132,600 176,800 110,500 ? 

我需要的是vba代码,它将读取“摘要”工作表上的星期编号,以便将“摘要”工作表中的源数据复制到“趋势”工作表中正确的相应列中。 在这个例子中,当周#变为6时,我需要macros将“摘要”工作表中的数据粘贴到“趋势”工作表中正确的范围内,根据匹配星期#(“6” )。 我期望在3次迭代中做到这一点,一次复制和粘贴一个“位置”的数据。 每个位置的“粘贴”地址的行将是恒定的,但我不确定如何将恒定的行数(每个位置)也纳入到VBA编码中。

Justkrys …你是对的…下面是一些代码,以更好地理解我想要做什么:

 Sub Trend_Data() ' ' Trend_Data Macro ' Copies current-week data to the corresponding Week column in Trending worksheet ' where: ' * Defined Week # is entered in "Summary" worksheet cell L2, ' * Corresponding Week # col hdrs (weeks 1 - 21): "Trending" worksheet, range B6:V6, ' * Location1 target row for pasting is "Trend" worksheet row 17, ' * Location2 target row for pasting is "Trend" worksheet row 26, ' * Location3 target row for pasting is "Trend" worksheet row 35, ' ' Sheets("Summary").Select Range("D10,D12,D14,D16,D18,D20").Select Range("D20").Activate Application.CutCopyMode = False Selection.Copy Sheets("Trend").Select ' ' Here is where code is required to read week number in "Summary" cell L2, ' so it can read the week number column headers in worksheet "Trending" row 6, ' to find column header "8" in cell I6. Then it can combine Col I with row 17 ' to correctly specify the range to paste Location 1 data: ' Range("I17").Select *'new code must determine col I is correct, row 17 is fixed target range row for pasting* Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Sheets("Summary").Select Range("E10,E12,E14,E16,E18,E20").Select Range("E20").Activate Application.CutCopyMode = False Selection.Copy Sheets("Trend").Select ' ' Code for Location 2 target range selection goes here, to select col I, row26 ' Range("I26").Select *' this is what the new code will equate to* Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Sheets("Summary").Select Application.CutCopyMode = False Range("F10,F12,F14,F16,F18,F20").Select Range("F20").Activate Selection.Copy Sheets("Trend").Select ActiveWindow.SmallScroll Down:=12 ' ' Code for Location 3 target range selection goes here, to select col I, row35 ' Range("I35").Select *' Column will be determined by new code* Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False End Sub 

我希望这可以让你更容易理解。

尝试这个。 我只是从我们读取数据的地方硬编码的地方,粘贴它的地方。

 Sub copySummary() Dim summarySheet As Worksheet Dim trendSheet As Worksheet Dim weekNumber As Integer Dim index As Integer Application.ScreenUpdating = False Set summarySheet = Sheets("Summary") Set trendSheet = Sheets("Trend") weekNumber = summarySheet.Cells(1, 2).Value index = 2 For columnIdx = 1 To 3 For rowIdx = 1 To 3 trendSheet.Cells(rowIdx + index, weekNumber + 2).Value = summarySheet.Cells(rowIdx + 2, columnIdx + 1).Value Next rowIdx index = index + 4 Next columnIdx Application.ScreenUpdating = True End Sub