Solved

Move data to new sheets in Excel

Posted on 2015-02-03
7
79 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 51

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 30

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 30

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 30

Expert Comment

by:gowflow
ID: 40586584
Your welcome !
gowflow
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
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…

691 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