Solved

Move data to new sheets in Excel

Posted on 2015-02-03
7
76 Views
Last Modified: 2015-02-03
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
0
Comment
Question by:Jagwarman
  • 3
  • 3
7 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40586440
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
0
 
LVL 29

Accepted Solution

by:
gowflow earned 500 total points
ID: 40586500
Try this code

Sub CreateCountries()
Dim WSR As Worksheet
Dim WSS As Worksheet
Dim WSNew As Worksheet
Dim MaxRowR As Long, MaxRowS As Long, I As Long, J As Long
Dim cCell As Range

'---> Disable Events
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

'---> Set Variables
Set WSS = Sheets("Static Data")
MaxRowS = WSS.Range("A" & WSS.Rows.Count).End(xlUp).Row
Set WSR = Sheets("Raw Data")
MaxRowR = WSR.Range("A" & WSR.Rows.Count).End(xlUp).Row

'---> Sort Raw Data by Col P
WSR.UsedRange.Sort key1:=WSR.Range("P1"), order1:=xlAscending, Header:=xlYes, MatchCase:=False

For I = 2 To MaxRowR + 1
    If WSR.Range("P" & I).Value <> "" Then
        Set cCell = WSS.Range("A1:A" & MaxRowS).Find(what:=WSR.Range("P" & I).Value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
        If Not cCell Is Nothing Then
            
            '---> Check if Sheet Exist
            On Error Resume Next
            Set WSNew = Sheets(cCell.Value)
            If Err <> 0 Then
                Set WSNew = Worksheets.Add(, Worksheets(Worksheets.Count))
                WSNew.Name = WSR.Range("P" & I).Value
                J = I
                While WSR.Range("P" & J).Offset(1) = WSR.Range("P" & I)
                    J = J + 1
                Wend
                WSR.Range("1:1").Copy WSNew.Range("1:1")
                WSR.Range(I & ":" & J).Copy WSNew.Range("2:2")
            End If
            On Error GoTo 0
        End If

    End If
Next I

'---> Enable Events
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

MsgBox "Sheets Created !", vbInformation
End Sub

Open in new window


Here is a sample workbook.
gowflow
Countries.xlsm
0
 

Author Comment

by:Jagwarman
ID: 40586552
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
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 29

Expert Comment

by:gowflow
ID: 40586557
Did you try mine ??
gowflow
0
 

Author Comment

by:Jagwarman
ID: 40586568
Just testing it gowflow
0
 

Author Comment

by:Jagwarman
ID: 40586580
That does it for me Gowflow thanks
0
 
LVL 29

Expert Comment

by:gowflow
ID: 40586584
Your welcome !
gowflow
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvieā€¦
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question