VBA help required: to create multiple workbooks from one workbook and also paste data in those workbooks.

aman2059

Board Regular
Joined
Jan 17, 2016
Messages
75
This forum is really helpful. I have been learning so much from the forum users. First of all thanks all :)

So I have below questions regarding creating multiple workbooks from one sheet. My steps are

1. There will "first.last name" in Column A. Sort column A for A to Z..
2. For all unique names in the column A, I have to copy data along with data present in column B,C,D against that unique name and then paste it all in the new workbook with the header(row 1). and then save the file with name "first.last"( which is present in column A of the sheet). Just like this, I have to do it for all the unique names present in the excel in column A.

Here is one example -

IF cell A2, A3, and A4 have name Aman.Bhardwaj
So I need all three row items to be copied and pasted into the new workbook along with the header(Row 1) and then it gets saved.

then if there another name in column A such as Aman sharma.
I need to perform same procedure again for this.

I have to do above for all names present in the excel.

Could anyone tell me if it is possible and if it is, please suggest me the code. I am not able to figure out code for this. This looks very advanced to me.

Thanks you in advance.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Maybe:

Code:
Sub aman2059()
Dim ws As Worksheet, i As Long, x As String
With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
    .Calculation = xlCalculationAutomatic
End With
Set ws = ActiveSheet
With ws
    For i = .Range("A" & Rows.Count).End(3).row - 1 To 1 Step -1
    x = .Cells(i + 1, "A").Value
        If .Cells(i + 1, "A") <> .Cells(i, "A") Then
            Workbooks.Add
            ActiveWorkbook.SaveAs x & ".xlsx"
            ActiveSheet.Name = x
            Rows(1).Value = .Rows(1).Value
            .Range("A1:A" & .Range("A" & Rows.Count).End(3).row).AutoFilter 1, .Cells(i + 1, "A")
            .Range("A2:A" & .Range("A" & Rows.Count).End(3).row).SpecialCells(12).Copy Range("A" & Rows.Count).End(3)(2)
            .Cells.Copy
            Range("A1").PasteSpecial xlPasteFormats
            ActiveSheet.Cells.Columns.AutoFit
            'ActiveWorkbook.Close True
        End If
        .Activate
    Next i
End With
With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
    .Calculation = xlCalculationManual
End With
End Sub
 
Upvote 0
Hi John,

Thank you so much for helping me out.

The above code worked fine. I am facing a small problem - When it copy and paste names from column A in the new workbook, it does copy data present in column B, C, and D against column A . Otherwise, everything is fine.

Could you please help me out?
 
Upvote 0
Also I need to save workbook on the some other location. Example - C:\Users\aman\Desktop\visual\Learning process
 
Upvote 0
You're welcome. Try:

Rich (BB code):
Sub aman2059()
Dim ws As Worksheet, i As Long, x As String
With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
    .Calculation = xlCalculationAutomatic
End With
Set ws = ActiveSheet
With ws
    For i = .Range("A" & Rows.Count).End(3).row - 1 To 1 Step -1
    x = .Cells(i + 1, "A").Value
        If .Cells(i + 1, "A") <> .Cells(i, "A") Then
            Workbooks.Add
            ActiveWorkbook.SaveAs "C:\Users\aman\Desktop\visual\Learning process\" & x & ".xlsx"
            ActiveSheet.Name = x
            Rows(1).Value = .Rows(1).Value
            .Range("A1:A" & .Range("A" & Rows.Count).End(3).row).AutoFilter 1, .Cells(i + 1, "A")
            .Range("A2:D" & .Range("A" & Rows.Count).End(3).row).SpecialCells(12).Copy Range("A" & Rows.Count).End(3)(2)
            .Cells.Copy
            Range("A1").PasteSpecial xlPasteFormats
            ActiveSheet.Cells.Columns.AutoFit
            'ActiveWorkbook.Close True
        End If
        .Activate
    Next i
End With
With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
    .Calculation = xlCalculationManual
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,756
Messages
6,132,530
Members
449,733
Latest member
Nameless_

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