VBA to Seperate Data and paste into different tabs/files

phoebeyanan

New Member
Joined
Aug 14, 2017
Messages
2
I want to create this macro to automate the process of separating the data.

The manual process is filter the data by column A (company code), and paste the filtered data into a new tab. Tab name is the company code.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Welcome to the forum!

Your post is a bit light on details so I've assumed you have a Co Code header in A1 and the codes are in A2 and down column A with no blank cells between A1 and the last company code in col A.

See if this does what you want.
Code:
Sub CompanyCode()
'Assumes company code begins in A2. Run code with the raw data sheet active
Dim DataSht As Worksheet, R As Range, CompanyIDs As Range, ID As Range
Set DataSht = ActiveSheet
Set R = Range("A1").CurrentRegion
Application.ScreenUpdating = False
Set CompanyIDs = Cells(R.Rows.Count + 3, R.Columns.Count + 3)
CompanyIDs.EntireColumn.Insert
R.Columns(1).AdvancedFilter xlFilterCopy, copytorange:=CompanyIDs, unique:=True
Set CompanyIDs = Range(CompanyIDs(2), CompanyIDs.End(xlDown))
For Each ID In CompanyIDs
    R.AutoFilter field:=1, Criteria1:=ID
    If R.SpecialCells(xlCellTypeVisible).Rows.Count > 1 Or R.SpecialCells(xlCellTypeVisible).Areas.Count > 1 Then
        On Error Resume Next
        Application.DisplayAlerts = False
        Sheets(ID.Value).Delete
        Sheets.Add after:=DataSht
        ActiveSheet.Name = ID
        R.Copy Sheets(ID.Value).Range("A1")
        Sheets(ID.Value).Range("A1").CurrentRegion.EntireColumn.AutoFit
    End If
Next ID
CompanyIDs.EntireColumn.Delete
With DataSht
    .AutoFilterMode = False
    .Activate
End With
With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
End With
End Sub
 
Upvote 0
Thank you very much! I was trying to upload a sample excel file, but I didn't find a place that I can upload the file.

In my data sheet, Column A is company code, column B is account number, column C through column M is the data.

I am wondering in the following code, what does the "+3" use for? Thank you!
Set CompanyIDs = Cells(R.Rows.Count + 3, R.Columns.Count + 3)
 
Upvote 0
Thank you very much! I was trying to upload a sample excel file, but I didn't find a place that I can upload the file.

In my data sheet, Column A is company code, column B is account number, column C through column M is the data.

I am wondering in the following code, what does the "+3" use for? Thank you!
Set CompanyIDs = Cells(R.Rows.Count + 3, R.Columns.Count + 3)
You are welcome.

Since you didn't provide a layout I had to make some assumptions. One of those is that the data are isolated from any other filled cells on the worksheet by at least one empty row and one empty column. If that's true, then the current region property of the data includes all rows & columns holding the data. The +3 ensures that a temporary list of unique company codes is to the right and below the data current region, so filtering of the data has no effect on the list.
 
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,292
Members
449,218
Latest member
Excel Master

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