Solved

Move data to new sheets in Excel

Posted on 2015-02-03
7
74 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 48

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

708 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

18 Experts available now in Live!

Get 1:1 Help Now