split worksheet into new worksheets based on changing value in Column A (by means of an Excel VBA)

Dear Experts:

on the current worksheet I would like to split the current worksheet named 'DataSource' into several worksheets based on the value in Column A,
... ie. whenever the Value in Column A changes, all the rows that belong to this value are to be copied into a new worksheet with the respective value as worksheet name:


Basis (Worksheet named DataSource):
Column A     column B        Column C         Column D
EN-23          House                   New               370 Dollars
EN-23          House                   old                  450 Dollars
EN-23          Apartment           new                380 Dollars
EN-45          House                  old                    220 Dollars
EN-45          House                  new                    93 Dollars
EN-17          HOuse                  old                     84 Dollars
EN-17           HOuse                 new                  30 Dollars


After running the macro:

new worksheet named EN-23 is created with the following contents:
Column A     column B        Column C         Column D
EN-23          House                   New               370 Dollars
EN-23          House                   old                  450 Dollars
EN-23          Apartment           new                380 Dollars


new worksheet named EN-45 is created with the following contents
EN-45          House                  old                    220 Dollars
EN-45          House                  new                    93 Dollars

new worksheet named EN-17 is created with the following contents
EN-17          HOuse                  old                     84 Dollars
EN-17           HOuse                 new                  30 Dollars


Help is very much appreciated. I have attached a sample file for your convenience.

Thank you very much in advance

Regards, Andreas
Split_Worksheet_based_ColumnA.xlsx
Andreas HermleTeam leaderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
Sub SplitDataBasedOnColumnA()
Dim dataWS As Worksheet, WS As Worksheet
Dim dict, x
Dim i As Long, lr As Long

Application.ScreenUpdating = False
Set dataWS = Sheets("DataSource")
lr = dataWS.Cells(Rows.Count, 1).End(xlUp).Row

x = dataWS.Range("A2:A" & lr).Value
Set dict = CreateObject("Scripting.Dictionary")

For i = 1 To UBound(x, 1)
    If x(i, 1) <> "" Then
        dict.Item(x(i, 1)) = ""
    End If
Next i

If dict.Count = 0 Then
    MsgBox "No data found in column A.", vbExclamation, "Data Not Found!"
    Exit Sub
End If

dataWS.AutoFilterMode = False
For Each it In dict.Keys
    On Error Resume Next
    Set WS = Sheets(Replace(it, "/", "-"))
    WS.Cells.Clear
    On Error GoTo 0
    If WS Is Nothing Then
        Sheets.Add(after:=Sheets(Sheets.Count)).Name = Replace(it, "/", "-")
        Set WS = ActiveSheet
    End If
    With dataWS.Rows(1)
        .AutoFilter Field:=1, Criteria1:=it
        dataWS.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy WS.Range("A1")
        Set WS = Nothing
    End With
Next it
dataWS.AutoFilterMode = False
dataWS.Activate
Application.ScreenUpdating = True
End Sub

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rob HensonFinance AnalystCommented:
It is possible to do this without VBA, you can use a feature of Pivot Tables.

Create a Pivot Table on your data and use the Number field as a Page Report. This will create just one pivot.

With the cursor in the Pivot Table, go to Pivot Table Tools ribbon and choose Options tab. AT left hand end of the ribbon there is a dropdown against Options. Click the dropdown and choose "Show Report Filter Pages". This gives a pop-up window showing the fields which have been chosen for Page Reports, just Number in the list in your case. Click Ok and a new tab will be created for each entry.

When updating the data, each of these reports can then be refreshed in one go.

Thanks
Rob H
0
Andreas HermleTeam leaderAuthor Commented:
Rgonzo: great code, works very nice, thank you very much for your superb help. :-)

Rob Henson: Rob, this is very interesting, will test it this evening and then let you know. Thank you very much :-)
0
Andreas HermleTeam leaderAuthor Commented:
Dear both: great jobs from both of you. I now have two approaches for these kind of tasks. That's really great. Rob: very interesting feature, this pivot approach. Thank you very much for it. Since I was asking for a VBA solution, the majority of the points go to Rgonzo. I hope you understand this.

Have a nice weekend. Regards, Andreas
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.