views:

57

answers:

1

Hello First up I’m a novice when it comes to this and have been trying to find and adapt scripts unsuccessfully, hope someone can help.

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. (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.

Thanks

+1  A: 

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()

You don't say what your source sheet looks like so I made something up for sheet "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). If you have headings, change all the A1 references to A2.

The other sheets ("homework" etc) must have been created. --And you need a reference set to the Microsoft Scripting Runtime.

The only "interesting" part of the code is figuring out the string for the target range (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
Marc Thibault
Thanks Marc, I do get an 'Compile Error: User-defined type not defined' pointing to 'cats As Dictionary' what am i doing wrong?
Sabine Blackburn
You need the Microsoft Scripting Runtime, where the Dictionary object lives. In the VBA window, click Tools|References and scroll down to select Microsoft Scripting Runtime. Dictionary is really useful, even though I made relatively trivial use of it here. It's one of the tools you should be familiar with.
Marc Thibault
Thanks Marc, it works now - would help if I actually read the instructions - sorry
Sabine Blackburn
My pleasure. Like the faq says, this place is for people who like to code.
Marc Thibault
one strange thing, the last line of each Category (Homework/Advanced/Beginner) is doubled up when it gets copied over.Any ideas?Thanks
Sabine Blackburn
My error. The putString calculation had a putRow that should have been putRow-1. That's fixed. I also threw in code to clear the result sheets in case the new results are fewer than the old results. The changes are marked in the listing. Nice how the use of Dictionary paid off.
Marc Thibault
+1 for the effort.
shahkalpesh
Thanks - I wish I could service like this from people that I pay :-)
Sabine Blackburn
I'll gladly work for cash but, for this one, ticking the accept box will do fine. BTW you don't say what this is for. I'm curious.
Marc Thibault
I'm running the sign up for our local non profit dog school. Sign up is done via google forms but we need to sort the entries into classes on different sheets for each instructor.
Sabine Blackburn
Don't you mean dog-owner school?
Marc Thibault