Solved

Move data to new sheets in Excel

Posted on 2015-02-03
7
75 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now