[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 84
  • Last Modified:

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
0
Jagwarman
Asked:
Jagwarman
  • 3
  • 3
1 Solution
 
Rgonzo1971Commented:
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
 
gowflowCommented:
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
 
JagwarmanAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
gowflowCommented:
Did you try mine ??
gowflow
0
 
JagwarmanAuthor Commented:
Just testing it gowflow
0
 
JagwarmanAuthor Commented:
That does it for me Gowflow thanks
0
 
gowflowCommented:
Your welcome !
gowflow
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now