How to separate data into different worksheet and another workbook

fluffyvampirekitten

Board Regular
Joined
Jul 1, 2015
Messages
72
I have this Workbook("Test"), worksheet"Countries"
- I have this column "R" which holds certain values ( America , India , Canada , Japan)

I want to separate the rows depending on the criteria .
Copy the values to another workbook - Workbook2 "FinalTest"
Workbook 2 have the respective tabs( America , India , Canada , Japan) but its empty.


- I want to copy/paste all the "America" rows in "America Worksheet" into Workbook2
- I want to copy/paste all the "India" rows in "India Worksheet" into Workbook2
- I want to copy/paste all the "Canada" rows in "Canada Worksheet" into Workbook2
- I want to copy/paste all the "Japan" rows in "Japan" Worksheet" into Workbook2

Any suggestion on how to do this ?
vba?
Thanks in advance
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Please try after save your books.

Code:
Sub sample_fluffy()
Dim buf As String
Dim ws As Worksheet, wb As Workbook
Const Target As String = "C:\FinalTest.xlsx" 'Please change to full path.
    buf = Dir(Target)
    If buf = "" Then
        MsgBox Target & vbCrLf & "is not exist.", vbExclamation
        Exit Sub
    End If
  
    For Each wb In Workbooks
        If wb.Name = buf Then
            Call sample_fluffy2
            Exit Sub
        End If
    Next wb
    Workbooks.Open Target
    Call sample_fluffy2
End Sub
Sub sample_fluffy2()
Dim i As Long, LastR As Long
Dim str As String
Dim ws As Worksheet
Dim Tgt As Workbook
Set Tgt = Workbooks("FinalTest.xlsx")
    With ThisWorkbook.Sheets("Countries")
        For Each ws In Tgt.Worksheets
            LastR = ws.Cells(Rows.Count, 1).End(xlUp).Row
            .Range("A1").AutoFilter Field:=18, Criteria1:=ws.Name
            .Range(.Range("A2"), .Range("A1").SpecialCells(xlCellTypeLastCell)).Resize(, 18).Copy
            ws.Cells(LastR + 1, 1).PasteSpecial Paste:=xlPasteValues
        Next
    End With
    Msgbox "Done"
End Sub
 
Upvote 0
Omg it can work ! Thanks alot :)

One last thing ,
Can i delete the "country" column in the FinalTest.xlsx ?
is it possible to change this also ?
Code:
Const Target As String = "C:\FinalTest.xlsx" 'Please change to full path.

Because this macro will be use in different computer. Thus , i dont want to keep changing the directory.

How to bypass the FinalTest.xlsx encrypted protected file.
 
Last edited:
Upvote 0
Hi,
you could modify Takae code to display the Open Dialog to allow users to select the file if that is what you want.

Try this modified version & see if helps:

Code:
Sub sample_fluffy()
    Dim FileName As Variant
    Dim wb As Workbook
    
        FileName = GetFile()
        If Not FileName = False Then
            Application.ScreenUpdating = False
            Set wb = Workbooks.Open(FileName, ReadOnly:=False)
            sample_fluffy2 wb
        Else
        'cancel pressed
        End If
End Sub


Sub sample_fluffy2(ByVal wb As Object)
    Dim LastR As Long
    Dim ws As Worksheet
    
    With ThisWorkbook.Sheets("Countries")
        For Each ws In wb.Worksheets
            LastR = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
            .Range("A1").AutoFilter Field:=18, Criteria1:=ws.Name
            .Range(.Range("A2"), .Range("A1").SpecialCells(xlCellTypeLastCell)).Resize(, 18).Copy
            ws.Cells(LastR + 1, 1).PasteSpecial Paste:=xlPasteValues
        Next
    End With
    
    Application.ScreenUpdating = True
    MsgBox "All Done", 48, "Copying Complete"
End Sub




Function GetFile(Optional Title As String = "Select File To Open") As Variant
    Dim FileFilter As String
    Dim FilterIndx As Integer
    
    FilterIndx = IIf(Val(Application.Version) < 12, 1, 2)


    FileFilter = "Excel 2003 (*.xls),*.xls," & _
              "Excel 2007 > (*.xlsx),*.xlsx," & _
              "All Excel Files (*.xl*),*.xl*," & _
              "All Files (*.*),*.*"


    GetFile = Application.GetOpenFilename(FileFilter, FilterIndx, Title)
End Function

The Function GetFile displays the Open Dialog & returns the selected workbook FileName to Open which is then passed as an object to Takae next code sample_fluffy2.
You can use the Function in other procedures where you need to display Open Dialog.

Hope Helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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