Here in this article, I have tried to consolidate some most useful and more frequently used excel macro with examples. This is part 1 where I have provided 20 Excel Macros related to workbook and worksheets.

24 Useful Excel Macro Examples for VBA Beginners (Ready-to-use)

To use them, make sure that you change the Excel file path, name, sheet name etc. They should do the job which they are written for. If you have any question or feedback, write it in the comment below.

Open an existing Excel Workbook using Excel Macro 3. Close a workbook without saving the changes using Excel Macro 4. Close a workbook by saving the changes using Excel Macro 5. Save or SaveAs a workbook using Excel Macro 6. Delete a workbook using Excel Macro. Add a new worksheet in a workbook using Excel Macro 8. Add a worksheet at a specified position using Excel Macro 9.

Rename a worksheet using Excel Macro Delete a worksheet using Excel Macro Change the tab color of a worksheet using Excel Macro Copy a worksheet within same workbook using Excel Macro Copy a worksheet as a new Workbook using Excel Macro Copy a worksheet by providing sheet name of your choice using Excel Macro Hide a worksheet using Excel Macro Hide all worksheets except activeSheet using Excel Macro Unhide a worksheet using Excel Macro Unhide all worksheets in a workbook using Excel Macro Check if a sheet with particular name exists in a workbook using Excel Macro Sort all worksheet alphabetically using Excel Macro.

excel vba examples pdf

Use the following Excel VBA code to create a new Excel Workbook and save it as a given path as shown in the below code. Refer the following Excel VBA code to open an existing excel workbook which is saved at a given path.

To run the below code, do not forget to change the file path which I have provided. Note: Once you opened your workbook, you should set it to a variable of Workbook type, so that you can refer this workbook by this variable wherever you want to use in your program. It is logical that after working on your workbook, at the end of the progrma you want to keep closing the workbook which you VBA program is using.

So here is the example of closing your workbook. As you know on closing an opened workbook, there are two possibilities:. Close the workbook without saving all the changes which are not saved yet 2. Close the workbook without saving any of the unsaved changes. It is very simple to do using Excel VBA. While closing if set the SaveChanges parameter to true then changes will be saved and if it is set to false then changes will be ignored.

Refer the below code…. As you must be aware of the difference between Save and SaveAs. It is same here in Excel vba as well. Note: For saveAs you need to provide the complete path[including file name] for the new file where you want to save it. You can use the following example to delete a workbook.One way of getting the VBA code is to record the macro and take the code it generates.

However, that code by macro recorder is often full of code that is not really needed. Also macro recorder has some limitations. So it pays to have a collection of useful VBA macro codes that you can have in your back pocket and use it when needed. In this massive article, I am going to list some useful Excel macro examples that I need often and keep stashed away in my private vault.

I will keep updating this tutorial with more macro examples. If you think something should be on the list, just leave a comment. Now before I get into the Macro Example and give you the VBA code, let me first show you how to use these example codes. In case the example says that you need to paste the code in the worksheet code window, double click on the worksheet object and copy paste the code in the code window.

Once you have inserted the code in a workbook, you need to save it with a. XLSM or. XLS extension. Related Tutorial: Different ways to run a macro in Excel.

It will automatically run when the specified action occurs. In case you find any error in the article or the code, please be awesome and let me know. If you are working in a workbook that has multiple hidden sheets, you need to unhide these sheets one by one. This could take some time in case there are many hidden sheets. It then changes the visible property of the worksheet to visible. Here is a detailed tutorial on how to use various methods to unhide sheets in Excel.

If you have a workbook with many worksheets and you want to sort these alphabetically, this macro code can come in really handy.

This could be the case if you have sheet names as years or employee names or product names. If you have a lot of worksheets in a workbook and you want to protect all the sheets, you can use this macro code. It allows you to specify the password within the code. You will need this password to unprotect the worksheet.

If you have some or all of the worksheets protected, you can just use a slight modification of the code used to protect sheets to unprotect it.

Excel VBA Save As PDF: Step-By-Step Guide And 10 Code Examples To Export Excel To PDF

Note that the password needs to the same that has been used to lock the worksheets. While it does the work, when cells are merged you will not be able to sort the data. In case you are working with a worksheet with merged cells, use the code below to unmerge all the merged cells at one go. A lot of time, you may need to create versions of your work.

Porpoise fit 2020

These are quite helpful in long projects where you work with a file over time. Using timestamps will allow you to go back to a certain file to see what changes were made or what data was used. You need to specify the folder location where you want to save the file.

You can specify something related to your project or company.

How to build Interactive Excel Dashboards

If you work with data for different years or divisions or products, you may have the need to save different worksheets as PDF files. In the above code, I have specified the address of the folder location in which I want to save the PDFs. Also, each PDF will get the same name as that of the worksheet. Use this code when you have a worksheet that contains a lot of formulas and you want to convert these formulas to values.We have created a Setting worksheet wherein we are taking 2 folders inputs on range E11 and E12 respectively.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information. Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies.

It is mandatory to procure user consent prior to running these cookies on your website. I am a Microsoft Certified Professional. I have more than 10 years of experience in Data Visualization, Excel Automation and dashboard creation.

Excel is my passion and I like to do always something innovative in Excel and share it with other people. Related Articles. April 7, April 8, PK. Age Calculator template tells your exact age till today. You need to enter your date of birth in the DOB.

January 5, January 5, PK. In this article you will learn how to get the files information from a folder like File Name, File Type. July 12, December 16, PK.

Vortex starting deployment

Our daily Excel activities can be automated using VBA. Here we have created an example of creating the Name range. Close Privacy Overview This website uses cookies to improve your experience while you navigate through the website.

Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website.

These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience. Necessary Always Enabled.

Non-necessary Non-necessary.And today, I have listed some of the useful codes examples to help you become more productive in your day to day work. The idea behind using a macro code is to automate an action which you perform manually in Excel, otherwise. Before you use these codes, make sure you have your developer tab on your Excel ribbon to access VB editor. Just read the title and click on it to get the code. These VBA codes will help you to perform some basic tasks in a flash which you frequently do in your spreadsheets.

This macro code will help you to automatically add serial numbers in your Excel sheet which can be helpful for you if you work with large data. To use this code you need to select the cell from where you want to start the serial numbers and when you run this it shows you a message box where you need to enter the highest number for the serial numbers and click OK.

300 Examples

And once you click OK, it simply runs a loop and add a list of serial numbers to the cells downward. This code helps you to enter multiple columns in a single click.

When you run this code it asks you the number columns you want to add and when you click OK, it adds entered number of columns after the selected cell. If you want to add columns before the selected cell, replace the xlToRight to xlToLeft in the code. With this code, you can enter multiple rows in the worksheet. When you run this code, you can enter the number of rows to insert and make sure to select the cell from where you want to insert the new rows.

This code quickly auto fits all the columns in your worksheet. So when you run this code, it will select all the cells in your worksheet and instantly auto-fit all the columns. You can use this code to auto-fit all the rows in a worksheet. When you run this code it will select all the cells in your worksheet and instantly auto-fit all the row. This code will help you to remove text wrap from the entire worksheet with a single click. It will first select all the columns and then remove text wrap and auto fit all the rows and columns.

The benefit of using this code is you can add it to the QAT and unmerge all the cell in the selection. And if you want to un-merge a specific range you can define that range in the code by replacing the word selection. In Windows, there is a specific calculator and by using this macro code you can open that calculator directly from Excel.

Sanskrit shlok class 6

This macro adds a date to the header when you run it. You can also change it to the footer or change the side by replacing the "" with the date tag. When you run this code, it shows an input box that asks you to enter the text which you want to add as a header, and once you enter it click OK. If you see this closely you have six different lines of code to choose the place for the header or footer. These VBA codes will help you to format cells and ranges using some specific criteria and conditions.

This macro will check each cell of your selection and highlight the duplicate values. If you are not sure about how many named ranges you have in your worksheet then you can use this code to highlight all of them. Once you run this code it will ask you for the value from which you want to highlight all greater values.

Once you run this code it will ask you for the value from which you want to highlight all lower values.PDF was then and continues to be, a common file format for distributing documents. They can be used on their own or as part of larger processes. This is the best way to learn. The support files are available for FREE to newsletter subscribers. This section contains the base code to save Excel as PDF from different objects workbooks, worksheets, ranges and charts.

Sometimes, we only want to save a small part of a worksheet to a PDF. The following code saves only the selected cells. If more than one worksheet is active, the PDF created will include all the active sheets. The following code selects multiple worksheets from an array before saving the PDF. The following macro loops through each worksheet in the active workbook and saves each as its own PDF. The following macro loops through the selected worksheets in the active workbook and saves each as its own PDF.

The following code loops through each chart on the active sheet and saves each as a separate PDF. AutoMacro is a powerful VBA code generator that comes loaded with an extensive code library and many other time-saving tools and utilities. The code below demonstrates how to use all the options within a single macro. These options can be flexed to meet your requirements. It is rarely used in the real world, but is an option if required. Learning how to save Excel as PDF is a good time investment.

Each of these code snippets on there own are not that impressive. However, when combined with the automated e-mail processes and document controlthey are a real time saver. Do you need help adapting this to your needs? By taking the time to understand the techniques and principles in this post and elsewhere on this site you should be able to adapt it to your needs. What next? Check out the latest posts:.

excel vba examples pdf

It there another way to get VBA with Excel in a another product. I am very very disappointed, because it was the only reason I bought this product.

Name would give you the name of the workbook.In previous article i had published the first 20 examples. At the end of this article, you will have a link to download a FREE copy of all 40 useful excel macros collection. Do not forget to download and play around and do provide your feedback.

Use following piece of code to insert a single row or multiple rows in a worksheet. Note: Do not miss to read the comments specified inside the code. They are important to know more about the code. Save my name, email, and website in this browser for the next time I comment.

Gelatin allergy

This site uses Akismet to reduce spam. Learn how your comment data is processed. Buy a meal for a daily wage worker in India in this Lock down period Read my personal message Add a Sheet using Excel Macros rviv. Excel Tool : Folder Structure Creator rviv. Home Welcome to LearnExcelMacro.

Insert a row in a worksheet using Excel Macro 2. Insert a column in a worksheet using Excel Macro 3.

excel vba examples pdf

Delete a row in a worksheet using Excel Macro 4. Delete a column in a worksheet using Excel Macro 5. Hide a row in worksheet using Excel Macro 6.

Hide a column in worksheet using Excel Macro 7. Unhide a row in worksheet using Excel Macro 8. Unhide a column in worksheet using Excel Macro 9.

Mulank 2 2018

Copy and insert copied row before a specific row using Excel Macro Copy and insert copied column before a specific column using Excel Macro Protect a worksheet without any password using Excel Macro Protect a worksheet with a password using Excel Macro Unprotect a protected worksheet using Excel Macro Unprotect a password protected worksheet using Excel Macro Protect a workbook with password using Excel Macro Open a password protected workbook using Excel Macro Clear contents of a Range without clearing formatting using Excel Macro Clear content of a range with formatting using Excel Macro Clear contents of a worksheet using Excel Macro Clear all the comments using Excel Macro.

Download Now. How did you find this collection of 40 Excel Macro examples? Did you find them useful to you? Provide your feedback about this.

Did you like this article? Related Posts.Have you ever been tired of doing a task in Excel which you feel can be automated and save your time? Most of the time you must have encountered such tasks. However, in order to automate any task, you first need to have programming skills for that particular language.

In this article, we will see how we can do programming in Excel VBA.

excel vba examples pdf

VBA can be used to write a program that can automate the task for you. The piece of lines we write under VBA is called Macro which is written in such a way that, they instruct the Excel system about what to be done.

Watch our Demo Courses and Videos. The first thing that comes is enabling the developer tab that helps you to record and store a macro VBA Code. Let us see how we can get that enabled. This is the first step you need to follow before you start writing macros in Excel.

This option tab is not by default enabled and that is why we tried enabling it here first. As soon as you click OK, the system starts recording the macro and all the tasks you perform will be recorded and converted to Excel Program in the backend. The magic behind all this is, Excel has recorded my steps here and converted those into pieces of code so that this task can be automated.

All these steps are converted into a VBA code. This is a guide to Programming in Excel. Here we discuss how to Program in Excel along with practical examples and downloadable excel template. Forgot Password? This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy.

If you want to know more or withdraw your consent to all or some of the cookies, please refer to the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy. Programming in Excel. Login details for this Free course will be emailed to you.

thoughts on “Excel vba examples pdf

Leave a Reply

Your email address will not be published. Required fields are marked *