繁体   English   中英

Excel VBA将数据粘贴到新工作表中与条件相关的列

[英]Excel VBA to paste data to a criteria-dependent column in new worksheet

我的问题类似于已经问过的几个问题,但又相差甚远,以至于我无法将其他解决方案应用于我的问题。

我每周收集一个工作表中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代码,该代码将读取“摘要”工作表上的星期数,因此将“摘要”工作表中的源数据复制到“趋势”工作表中正确的对应列中。 在此示例中,当week#更改为6时,我需要基于宏来将数据从“摘要”工作表粘贴到“趋势”工作表中的正确范围内,该匹配项基于与week#(“ 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM