VBA to Split data from 3 worksheets based upon column "A" values into separate tab for each distinct value

Vbalearner85

Board Regular
Joined
Jun 9, 2019
Messages
139
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I have a macro (below) which splits the data from sheet "Adata" of workbook based on column "A" distinct values into separate sheets.

What I want to do is modify Macro to also repeat the same process(split data) in sequence for another 2 sheets "Bdata" and " Cdata" of the same workbook and provide resultant data in same sheets based upon values of column "A". Please note All three sheets "Adata/Bdata/Cdata) have similar values in column "A". Data must be copied in sequence-First from "Adata" and then from "BData" (below data from A data) and then from "CData" (below data from b data) in resultant worksheets . Thanks !!!

Sub Split_Sht_in_Separate_Shts()

Const FirstC As String = "A" '1st column

Const LastC As String = "M" 'last column

Const sCol As String = "A" '<<< Criteria in Column B

Const shN As String = "Adata" '<<< Source Sheet

Dim ws As Worksheet, ws1 As Worksheet

Set ws = Sheets(shN)

Dim rng As Range

Dim r As Long, c As Long, x As Long, r1 As Long

Application.ScreenUpdating = False

r = ws.Range(FirstC & ":" & LastC).Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

c = ws.Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 2

Set rng = ws.Range(ws.Cells(1, FirstC), ws.Cells(r, LastC))

ws.Range(sCol & ":" & sCol).Copy

ws.Cells(1, c).PasteSpecial xlValues

Application.CutCopyMode = False

ws.Cells(1, c).Resize(r).RemoveDuplicates Columns:=1, Header:=xlYes

r1 = ws.Cells(Rows.Count, c).End(xlUp).Row

ws.Cells(1, c).Resize(r1).Sort Key1:=ws.Cells(1, c), Header:=xlYes

ws.AutoFilterMode = False

Application.DisplayAlerts = False

For x = 2 To r1

For Each ws1 In Sheets

If ws1.Name = ws.Cells(x, c) Then ws1.Delete

Next

Next

Application.DisplayAlerts = True

For x = 2 To r1

ws.Range(ws.Cells(1, sCol), ws.Cells(r, sCol)).AutoFilter Field:=1, Criteria1:=ws.Cells(x, c)

Set ws1 = Worksheets.Add(after:=Worksheets(Worksheets.Count))

ws1.Name = ws.Cells(x, c).Value

rng.SpecialCells(xlCellTypeVisible).Copy

Range("A1").PasteSpecial Paste:=xlPasteFormats

Range("A1").PasteSpecial Paste:=xlPasteColumnWidths

Range("A1").PasteSpecial Paste:=xlPasteValues

Application.CutCopyMode = False

Next x

With ws

.AutoFilterMode = False

.Cells(1, c).Resize(r).ClearContents

.Activate

.Range("A1").Select

End With

Application.ScreenUpdating = True

End Sub
 
Awesome sir..worked like charm.....thanks so much for your time and effort..Kudos to people like you who impart their knowledge for us....Exactly worked like I was expecting it to be !!!!
 
Last edited:
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Awesome sir..worked like charm.....thanks so much for your time and effort..Kudos to people like you who impart their knowledge for us....Exactly worked like I was expecting it to be !!!!
You're welcome,
regards, JLG
 
Upvote 0
Hi sir, This macro has served my purpose quite brilliantly.

1) Need one tweak - In case any of the source data sheets are blank, it must still do the intended job of splitting data based upon data available in any of the sheets and ignore blank source datasheets without any error message (either or combination of "Ddata", "Edata", "Fdata", "Gdata" sheets could be blank)

2) In addition looking for a modified version of the above macro, which shall split data only for 1 selected value of column A and provide resultant data in sheet "Result" ( and not delete any data from source sheets). Also, the same above condition applies, one or more data sources could be blank as well

Regards
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top