我的问题类似于已经问过的几个问题,但又不同,以至于我无法将其他解决方案应用到我的问题中。
我每周为一个工作表中的3个位置收集数据,并希望将该数据复制到不同的工作表,其中数据按周保存为时间序列。我可以使用宏记录功能为每个位置选择数据,但我不知道如何编写代码来指定另一个工作表的正确目标列范围,以及应该粘贴数据的位置,这取决于当前周数。这使我可以随时跟踪/分析数据。
为了说明,我有2个工作表,"摘要" &安培; "趋势",结构如下:
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时,我需要宏来粘贴来自"摘要"工作表到"趋势"中的正确范围工作表,基于与#周匹配的列标题(" 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
我希望这会让人们更容易理解。
答案 0 :(得分:0)
试试这个。我只是从我们读取数据的地方硬编码的地方和我们粘贴的地方。
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