简体   繁体   English

如果条件为,则将excel中的行复制到工作表。

[英]Copy rows in excel to sheets if condition is .

I'm trying to copy entire rows of the Sheet 'All' to another sheet according to the data in column D. There are multiple values in column D (Homework/Advanced/Beginner) and the sheets these rows need to be copied to have corresponding names. 我正在尝试根据D列中的数据将工作表“全部”的整行复制到另一张工作表中。D列中有多个值(家庭作业/高级/入门),需要复制这些行的工作表相应的名称。 (Homework to Homework sheet.) (作业到作业表)。

The data in Sheet 'All' will be added on to and the new data needs to be copied without duplicating the ones that are already there. 工作表“全部”中的数据将被添加到其中,并且需要复制新数据,而无需复制已经存在的数据。

It's not a big problem. 这不是大问题。 The best thing is to keep things simple and copy everything whenever "all" changes. 最好的办法是保持简单,并在“全部”更改时复制所有内容。 I'd have a "Redistribute" button on the "all" sheet and have the event call scatterRows() 我在“所有”工作表上都有一个“重新分发”按钮,并有事件调用scatterRows()

You don't say what your source sheet looks like so I made something up for sheet "all": 您没有说出源工作表的样子,所以我为工作表“ all”做了一些准备:

9   0.181626294 carrot  beginner    Irene
5   0.221180184 beans   advanced    Eva
8   0.221813735 turnip  advanced    Harry
10  0.314800867 lettuce homework    John
4   0.360163255 peas    homework    Doug
11  0.379956592 pepper  advanced    Karen
3   0.44415906  tomato  beginner    Charlie
6   0.647446239 corn    beginner    Frank
2   0.655706735 potato  advanced    Bob
7   0.666002258 lentils homework    George
1   0.768524361 squash  homework    Alice

The code is fairly flexible; 代码相当灵活; it finds the whole source block, so it doesn't matter how many columns you have as long as column "D" holds the sheet key and the data starts in A1 (no headings). 它会找到整个源代码块,因此,只要有“ D”列保留工作表键并且数据以A1(无标题)开头,您就可以拥有多少列。 If you have headings, change all the A1 references to A2. 如果您有标题,请将所有A1引用更改为A2。

The other sheets ("homework" etc) must have been created. 必须已创建其他工作表(“作业”等)。 --And you need a reference set to the Microsoft Scripting Runtime. -并且您需要对Microsoft脚本运行时的引用集。

The only "interesting" part of the code is figuring out the string for the target range (putString). 代码中唯一“有趣”的部分是找出目标范围的字符串(putString)。

Option Explicit

'' Copy rows from the "all" sheet to other sheets
'' keying the sheetname from column D.
'' **** Needs Tools|References|Microsoft Scripting Runtime
'' Changes:
''      [1] fixed the putString calculation.
''      [2] Added logic to clear the target sheets.

Sub scatterRows()

    Dim srcRange As Range
    Dim srcRow As Range
    Dim srcCols As Integer
    Dim srcCat As String
    Dim putRow As Integer
    Dim putString As String
    Dim s                      ''*New [2]

    '' Current row for each category
    Dim cats As Dictionary
    Set cats = New Dictionary
    cats.Add "homework", 0
    cats.Add "beginner", 0
    cats.Add "advanced", 0

    '' Clear the category sheets  *New [2]
    For Each s In cats.Keys
        Range(s & "!A1").CurrentRegion.Delete
    Next s

    '' Find the source range
    Set srcRange = [all!a1].CurrentRegion
    srcCols = srcRange.Columns.Count

    '' Move rows from source Loop
    For Each srcRow In srcRange.Rows

        '' get the category
        srcCat = srcRow.Cells(4).Value

        '' get the target sheet row and increment it
        putRow = cats(srcCat) + 1
        cats(srcCat) = putRow

        '' format the target range string     *Fixed [1]
        '' e.g. "homework!A3:E3"
        putString = srcCat & "!" & _
            [a1].Offset(putRow - 1, 0).Address & _
            ":" & [a1].Offset(putRow - 1, srcCols - 1).Address

        '' copy from sheet all to target sheet
        Range(putString).Value = srcRow.Value
    Next srcRow
End Sub

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

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