0% found this document useful (0 votes)
222 views

Excel Vba Code To Combine, Split

This document contains code snippets for several VBA macros that perform common Excel tasks: combining sheets from multiple workbooks into one workbook, splitting a workbook into individual workbooks by sheet, deleting all sheets except the first in a workbook, and printing all active sheets in open workbooks. Instructions are provided at the end for running the CombineFiles macro.

Uploaded by

castingforge4875
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views

Excel Vba Code To Combine, Split

This document contains code snippets for several VBA macros that perform common Excel tasks: combining sheets from multiple workbooks into one workbook, splitting a workbook into individual workbooks by sheet, deleting all sheets except the first in a workbook, and printing all active sheets in open workbooks. Instructions are provided at the end for running the CombineFiles macro.

Uploaded by

castingforge4875
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

FOR COMBINING

Sub GetSheets()
Path = "C:\Users\dt\Desktop\dt kte\"
Filename = Dir(Path & "*.xls")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub

SPLITING WORK BOOKS


Sub Splitbook()
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
sht.Copy
ActiveSheet.Cells.Copy
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
ActiveWorkbook.SaveAs _
Filename:=MyPath & "\" & sht.Name & ".xls"
ActiveWorkbook.Close savechanges:=False
Next sht
End Sub

DELETE ALL THE SHEETS IN A WORKBOOK EXCEPT THE FIRST SHEET

PROMPTING MESSAGE
Sub SheetRemover()
k = Sheets.Count
Application.DisplayAlerts = False
For i = k To 2 Step -1
Sheets(i).Delete
Next
Application.DisplayAlerts = True
End Sub

WITH OUT

ONLY PRINT THE ACTIVE WORKSHEETS OF EACH WORKBOOK


Sub PrintAllSheets()
Dim wb As Workbook, sht As Object
For Each wb In Excel.Workbooks
For Each sht In wb.Sheets
sht.PrintOut
Next sht
Next wb
End Sub

COMBINEFILES
OptionExplicit

SubCombineFiles()

DimPathAsString
DimFileNameAsString
DimWkbAsWorkbook
DimWSAsWorksheet

Application.EnableEvents=False
Application.ScreenUpdating=False
Path="C:\"'Changeasneeded
FileName=Dir(Path&"\*.xls",vbNormal)
DoUntilFileName=""
SetWkb=Workbooks.Open(FileName:=Path&"\"&
FileName)
ForEachWSInWkb.Worksheets

WS.Copy
After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
NextWS
Wkb.CloseFalse
FileName=Dir()
Loop
Application.EnableEvents=True
Application.ScreenUpdating=True

EndSub
1.
2.
3.
4.
5.
6.

Open Excel.
Alt + F11 to open the VBE.
Insert | Module.
Paste the code in the Code Window that opens up.
Change the Path as needed.
Close the VBE (Alt + Q or press the X in the top right corner).

You might also like