Excel VBA将数据粘贴到新工作表中与条件相关的列
我的问题与已经提出的几个问题相似,但差异很大,以至于我无法将其他解决方案应用于我的问题 我每周在一个工作表中收集3个位置的数据,并希望将该数据复制到另一个工作表中,在该工作表中,数据按周作为时间序列保存。我可以使用宏记录功能为每个位置选择数据,但我不知道如何编码以指定另一个工作表的正确目标列范围,将数据粘贴到其中,该范围由当前周数决定。这使我能够随时间跟踪/分析数据 为了说明,我有两份工作表,总结和趋势,结构如下:Excel VBA将数据粘贴到新工作表中与条件相关的列,vba,excel,excel-2010,Vba,Excel,Excel 2010,我的问题与已经提出的几个问题相似,但差异很大,以至于我无法将其他解决方案应用于我的问题 我每周在一个工作表中收集3个位置的数据,并希望将该数据复制到另一个工作表中,在该工作表中,数据按周作为时间序列保存。我可以使用宏记录功能为每个位置选择数据,但我不知道如何编码以指定另一个工作表的正确目标列范围,将数据粘贴到其中,该范围由当前周数决定。这使我能够随时间跟踪/分析数据 为了说明,我有两份工作表,总结和趋势,结构如下: SUMMARY Worksheet Week#: 5 Pr
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
我希望这样更容易理解。试试这个。我只是硬编码了我们读取数据的位置和粘贴数据的位置
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
试试这个。我只是硬编码了我们读取数据的位置和粘贴数据的位置
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
如果需要帮助,你必须发布你编写的代码。如果需要帮助,你必须发布你编写的代码。