avatarChristian Martinez Founder of The Financial Fox

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1917

Abstract

Step 4. Under <b>Customize the Ribbon</b> and under <b>Main Tabs</b>, select the <b>Developer</b> check box.</p><figure id="9704"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*xRb7R4wq2B5QuHtCNCrL-g.jpeg"><figcaption></figcaption></figure><p id="f2d1"><b>Demonstration</b></p><p id="da7a">1. Combine different workbooks into one workbook (but in different sheets)</p><p id="df90">The code below is an example: <i>Sub GetSheets()</i></p><p id="2c82"><i>‘Combine different workbooks into one workbook(but in different sheets)</i></p><p id="5131"><i>Path = “C:\VBA_Practice\Combine_workbooks\”</i></p><p id="da99"><i>Filename = Dir(Path & “*.xls”)</i></p><p id="fd5f"><i>Do While Filename <> “”</i></p><p id="efd2"><i>Workbooks.Open Filename:=Path & Filename, ReadOnly:=True</i></p><p id="6328"><i>For Each Sheet In ActiveWorkbook.Sheets</i></p><p id="9d01"><i>Sheet.Copy After:=ThisWorkbook.Sheets(“Sheet1”)</i></p><p id="684a"><i>Next Sheet</i></p><p id="f3a2"><i>Workbooks(Filename).Close</i></p><p id="bed4"><i>Filename = Dir()</i></p><p id="ecd3"><i>Loop</i></p><p id="f7d6"><i>End Sub</i></p><p id="8edb">2. Combine different workbooks into one workbook (same header)</p><p id="3915">The code below is an example:</p><p id="34e7"><i>Sub LoopThroughDirectory()</i></p><p id="0280"><i>Dim MyFile As String</i></p><p id="87fb"><i>Dim erow ‘erow stands for empty row, if I don’t type the data type, Exel will use Variant as the data type</i></p><p id="e7b5"><i>Dim Filepath As String</i></p><p id="36cb"><i>Filepath = “C:\VBA_Practice\Combine_workbooks_same_header\”</i></p><p id="1691"><i>MyFile = Dir(Filepath)</i></p><p id="f03b"><i>Do While Len(MyFile) > 0</i></p><p id="7f3d"><i>If MyFile = “zmaster.xlsm” Then</i></p><p id="65ea"><i>Exit Sub</i></p><p id="a7ee"><i>End If</i></p><p id="5fbb"><i>‘the new file (in this case, zmaster.xlsm) needs to be the last file in the fold

Options

er, if it’s the first file Excel VBA will exit without copying anything</i></p><p id="e10a"><i>Workbooks.Open (Filepath & MyFile)</i></p><p id="bd00"><i>Range(“A2:D5”).Copy</i></p><p id="3b83"><i>ActiveWorkbook.Close</i></p><p id="7283"><i>erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row ‘the last Row returns the number</i></p><p id="8184"><i>ActiveSheet.Paste Destination:=Worksheets(“Sheet1”).Range(Cells(erow, 1), Cells(erow, 4))</i></p><p id="f218"><i>MyFile = Dir</i></p><p id="f014"><i>Loop</i></p><p id="8036"><i>End Sub</i></p><figure id="ff68"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*sqQi427Vqj82hkg8wDrcfg.jpeg"><figcaption></figcaption></figure><p id="63bd">We want to combine the data in File 1 to 4 into zmaster.xlsm.</p><p id="cf2c">Note that the above examples only work if you store all the files you want to merge in one folder.</p><p id="f24e" type="7">If you are not familiar with VBA and find the coding hard to understand, that’s okay!</p><h1 id="b32c">You can always find plenty of useful resources online.</h1><h2 id="33cb">Here are some good examples:</h2><p id="1fb8"><a href="http://www.homeandlearn.org/index.html">http://www.homeandlearn.org/index.html</a> This one is for complete beginners. Its detailed explanations are easy to understand.</p><p id="ce0f"><a href="http://www.excel-easy.com/vba.html">http://www.excel-easy.com/vba.html</a> There are 16 categories that can help you learn Excel VBA.</p><p id="63cf"><b>Some useful VBA editor shortcut keys</b></p><p id="1967">Alt + F11 Switch between the Visual Basic Editor and the application.</p><p id="bdc9">F5 Runs the current procedure</p><p id="b163"><b>More shortcut keys can be found here:</b></p><p id="bd30"><a href="https://bettersolutions.com/vba/visual-basic-editor/shortcut-keys.htm">https://bettersolutions.com/vba/visual-basic-editor/shortcut-keys.htm</a></p></article></body>

What Is Microsoft Excel VBA and How Can It Help You Work Smarter?

What is Excel VBA?

VBA stands for Visual Basic Applications. It’s a powerful and built-in programming language that allows you to write your own functions or commands in an Excel spreadsheet.

Advantages of using VBA

Excel VBA helps us automate tedious tasks. For example, it can help you combine multiple workbooks into one workbook. Imagine that you have 10 workbooks in a folder and for each file, you have 3 sheets. You want to copy and paste everything into a new workbook, but it’s time consuming to do it one by one. That’s when Excel VBA comes in handy.

Show the Developer tab

The Developer tab is not displayed by default, but you can add it to the ribbon when you want to write VBA code.

Step 1. Click the File tab.

Step 2. Click Options.

Step 3. Click Customize Ribbon.

Step 4. Under Customize the Ribbon and under Main Tabs, select the Developer check box.

Demonstration

1. Combine different workbooks into one workbook (but in different sheets)

The code below is an example: Sub GetSheets()

‘Combine different workbooks into one workbook(but in different sheets)

Path = “C:\VBA_Practice\Combine_workbooks\”

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(“Sheet1”)

Next Sheet

Workbooks(Filename).Close

Filename = Dir()

Loop

End Sub

2. Combine different workbooks into one workbook (same header)

The code below is an example:

Sub LoopThroughDirectory()

Dim MyFile As String

Dim erow ‘erow stands for empty row, if I don’t type the data type, Exel will use Variant as the data type

Dim Filepath As String

Filepath = “C:\VBA_Practice\Combine_workbooks_same_header\”

MyFile = Dir(Filepath)

Do While Len(MyFile) > 0

If MyFile = “zmaster.xlsm” Then

Exit Sub

End If

‘the new file (in this case, zmaster.xlsm) needs to be the last file in the folder, if it’s the first file Excel VBA will exit without copying anything

Workbooks.Open (Filepath & MyFile)

Range(“A2:D5”).Copy

ActiveWorkbook.Close

erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row ‘the last Row returns the number

ActiveSheet.Paste Destination:=Worksheets(“Sheet1”).Range(Cells(erow, 1), Cells(erow, 4))

MyFile = Dir

Loop

End Sub

We want to combine the data in File 1 to 4 into zmaster.xlsm.

Note that the above examples only work if you store all the files you want to merge in one folder.

If you are not familiar with VBA and find the coding hard to understand, that’s okay!

You can always find plenty of useful resources online.

Here are some good examples:

http://www.homeandlearn.org/index.html This one is for complete beginners. Its detailed explanations are easy to understand.

http://www.excel-easy.com/vba.html There are 16 categories that can help you learn Excel VBA.

Some useful VBA editor shortcut keys

Alt + F11 Switch between the Visual Basic Editor and the application.

F5 Runs the current procedure

More shortcut keys can be found here:

https://bettersolutions.com/vba/visual-basic-editor/shortcut-keys.htm

Excel
Vba
Finance
Financial Analysis
Microsoft Excel
Recommended from ReadMedium