Anda di halaman 1dari 5

1.

Export and save worksheets as new workbook with VBA code


This following code will export every visible worksheet in a new workbook
and save the workbook with the name of the original sheet in a newly
created folder in the same path as the active workbook. Please do as
following steps:
Step 1: Hold down the ALT + F11 keys, and it opens the Microsoft Visual
Basic for Applications window.
Step 2: Click Insert > Module, and paste the following macro in the Module
Window:
VBA : Export and save worksheets as new workbook in a new folder.
Sub SplitWorkbook()
'Updateby20140612
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim xWs As Worksheet
Dim xWb As Workbook
Dim FolderName As String
Application.ScreenUpdating = False
Set xWb = Application.ThisWorkbook
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = xWb.Path & "\" & xWb.Name & " " & DateString
MkDir FolderName
For Each xWs In xWb.Worksheets
xWs.Copy
If Val(Application.Version) < 12 Then
FileExtStr = ".xls": FileFormatNum = -4143
Else
Select Case xWb.FileFormat
Case 51:
FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If Application.ActiveWorkbook.HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56:
FileExtStr = ".xls": FileFormatNum = 56
Case Else:
FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
xFile = FolderName & "\" & Application.ActiveWorkbook.Sheets(1).Name & FileExtStr
Application.ActiveWorkbook.SaveAs xFile, FileFormat:=FileFormatNum
Application.ActiveWorkbook.Close False
Next
MsgBox "You can find the files in " & FolderName
Application.ScreenUpdating = True
End Sub

Step 3: Press the F5 key to run this code. And a prompt box will pop up to
tell you the location of the new exported workbooks, and all of the
worksheets of the original workbook have been exported to some new
separate workbooks which named original sheets in a new specific folder.
See screenshots:

2. How to split data into multiple worksheets based on column in


Excel?

Supposing you have a worksheet with huge rows of data, and now, you need to split the data into
multiple worksheets based on the Name column (see following screenshots), and the names are entered
randomly. Maybe you can sort them first, and then copy and paste them one by one into other new
worksheets. But this will need your patience to copy and paste repeatedly. Today, I will talk about some
quick tricks to solve this task.

If you want to split the data based on column value quickly and automatically, the following VBA
code is a good choice. Please do as this:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.
Sub parse_data()

Dim lr As Long

Dim ws As Worksheet

Dim vcol, i As Integer

Dim icol As Long

Dim myarr As Variant

Dim title As String

Dim titlerow As Integer

vcol = 3

Set ws = Sheets("aprilie")

lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row

title = "A1:D1"

titlerow = ws.Range(title).Cells(1).Row

icol = ws.Columns.Count

ws.Cells(1, icol) = "Unique"

For i = 2 To lr

On Error Resume Next

If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0)


= 0 Then

ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)

End If
Next

myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))

ws.Columns(icol).Clear

For i = 2 To UBound(myarr)

ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""

If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then

Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = myarr(i) & ""

Else

Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.Count)

End If

ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")

Sheets(myarr(i) & "").Columns.AutoFit

Next

ws.AutoFilterMode = False

ws.Activate

End Sub

Note: In the above code:

vcol =1, the number 1 is the column number that you want to split
the data based on.

Set ws = Sheets("Sheet1"), Sheet1 is the sheet name that you want


to apply this code.

title = "A1:C1", A1:C1 is the range of the title.

All of them are variables, you can change them as your need.
Then press F5 key to run the code, all data in the active worksheet are
split into multiple worksheets by the column value. And the split
worksheets are named with the split cell names. See screenshot:

Note: The split worksheets are placed in the end of the workbook
where the master worksheet is in.

Anda mungkin juga menyukai