Link to home
Start Free TrialLog in
Avatar of Jagwarman
Jagwarman

asked on

Move data to new sheets in Excel

I have a Static Data Sheet with Names of Countries in Column A [ this list will increase in time]

In the Raw Data Sheet in Column P are the names of countries.

If the countries in column P in the Raw Data Tab are in the Static Data table [in Sheet named Static Data] I need to create a new sheet, if one does not already exist.

I then need to copy the entire rows from the Raw Data tab onto the respective [named] Sheets and include the header row in each sheet.

So if Spain is in P10, P11, P12, P13 in the Raw Data Sheet,  I need to copy both the header from the Raw Data Sheet and Rows 10,11,12,13 to the sheet named Spain

Rgonzo1971 provided me with the below code recently but unfortunately the user criteria changed.

Sub c2sheets()
Set SrcSheet = ActiveSheet
For Idx = 2 To Range("M2:M" & Range("M" & Rows.Count).End(xlUp).Row).Count + 1
    If SrcSheet.Range("M" & Idx).Value <> "" Then
        Set DestSh = Worksheets.Add(, Worksheets(Worksheets.Count))
        DestSh.Name = SrcSheet.Range("M" & Idx).Value
        Idx2 = Idx
        While SrcSheet.Range("M" & Idx).Offset(1) = SrcSheet.Range("M" & Idx)
            Idx = Idx + 1
        Wend
        SrcSheet.Range("1:1").Copy DestSh.Range("1:1")
        SrcSheet.Range(Idx & ":" & Idx2).Copy DestSh.Range("2:2")

    End If
Next
End Sub

Thanks in advance
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try

Sub c2sheets()
Set SrcSheet = ActiveSheet
For Idx = 2 To Range("P2:P" & Range("P" & Rows.Count).End(xlUp).Row).Count + 1
    If SrcSheet.Range("P" & Idx).Value <> "" Then
        On Error Resume Next
        Set DestSh = Worksheets(SrcSheet.Range("P" & Idx).Value)
        On Error GoTo 0
        If IsEmpty(DestSh) Then
            Set DestSh = Worksheets.Add(, Worksheets(Worksheets.Count))
            DestSh.Name = SrcSheet.Range("P" & Idx).Value
        End If
        Idx2 = Idx
        While SrcSheet.Range("P" & Idx).Offset(1) = SrcSheet.Range("P" & Idx)
            Idx = Idx + 1
        Wend
        SrcSheet.Range("1:1").Copy DestSh.Range("1:1")
        SrcSheet.Range(Idx & ":" & Idx2).Copy DestSh.Range("A" & Cells.Rows.Count).End(xlUp).Offset(1)
    End If
Next
End Sub

Open in new window

EDIT  corrected

Regards
ASKER CERTIFIED SOLUTION
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jagwarman

ASKER

Hi Rgonzo

yours does not work. my request was...

I have a Static Data Sheet with Names of Countries in Column A [ this list will increase in time]

 In the Raw Data Sheet in Column P are the names of countries.

 If the countries in column P in the Raw Data Tab are in the Static Data table [in Sheet named Static Data] I need to create a new sheet, if one does not already exist.

but yours does not appear to look at the Static Data Sheet and it does not appear to create separate sheets per country

Thanks
Did you try mine ??
gowflow
Just testing it gowflow
That does it for me Gowflow thanks
Your welcome !
gowflow