{"id":14834,"date":"2018-06-28T21:44:10","date_gmt":"2018-06-28T21:44:10","guid":{"rendered":"http:\/\/learnexcelmacro.com\/wp\/?p=14834"},"modified":"2022-08-17T19:17:00","modified_gmt":"2022-08-17T19:17:00","slug":"40-useful-excel-macro-examples-for-beginners-part-2-of-2","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2018\/06\/40-useful-excel-macro-examples-for-beginners-part-2-of-2\/","title":{"rendered":"40 Useful Excel Macro [VBA] examples – Part 2 of 2 [ FREE DOWNLOAD ]"},"content":{"rendered":"
[et_pb_section fb_built=”1″ _builder_version=”4.17.6″ custom_padding=”0px|0px|0px|0px|false|false” da_disable_devices=”off|off|off” global_colors_info=”{}” da_is_popup=”off” da_exit_intent=”off” da_has_close=”on” da_alt_close=”off” da_dark_close=”off” da_not_modal=”on” da_is_singular=”off” da_with_loader=”off” da_has_shadow=”on”][et_pb_row use_custom_gutter=”on” gutter_width=”1″ _builder_version=”4.17.6″ background_size=”initial” background_position=”top_left” background_repeat=”repeat” width=”100%” custom_padding=”0px|0px|0px|0px|false|false” global_colors_info=”{}”][et_pb_column type=”4_4″ _builder_version=”4.16″ custom_padding=”|||” global_colors_info=”{}” custom_padding__hover=”|||”][et_pb_text _builder_version=”4.17.6″ background_size=”initial” background_position=”top_left” background_repeat=”repeat” custom_padding=”0px|0px|0px|0px|false|false” global_colors_info=”{}”]<\/p>\n
Dear friends,<\/p>\n
As you see this is the second and last part of the tutorial 40 Useful Excel Macro [VBA] examples<\/a><\/strong>. In a previous article,<\/strong><\/a> I published the first 20 examples.<\/p>\n At the end of this article, you will have a link to download a FREE copy of all 40 useful excel macros collections. Do not forget to download and play around and do provide your feedback.<\/p>\n Here in this last part remaining 20 examples are specified here.<\/p>\n Use the following piece of code to insert a single row or multiple rows in a worksheet. Following code can be used to insert a single column or multiple columns in a worksheet.<\/p>\n <\/p>\n Using this code you can delete a single or multiple rows.<\/p>\n <\/p>\n Using the below piece of code, you can delete a single or multiple columns.<\/p>\n <\/p>\n Using the below piece of code, you can hide a single or multiple rows.<\/p>\n <\/p>\n Using the below piece of code, you can hide a single or multiple columns.<\/p>\n <\/p>\n Using the below piece of code, you can unhide a single or multiple rows.<\/p>\n <\/p>\n Using the below piece of code, you can unhide a single or multiple columns.<\/p>\n <\/p>\n Using the below piece of code, you can copy and insert any number of rows.<\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n [\/et_pb_text][et_pb_cta title=”How did you find this collection of 40 Excel Macro examples?” button_url=”\/excel\/wp-content\/downloads\/Top-40-Useful-ExcelMacro-Examples.xlsm” button_text=”Download FREE Workbook” _builder_version=”4.17.6″ _module_preset=”a50a16dd-d05f-4ea2-acab-1468d2e4010e” global_colors_info=”{}”]Did you find them useful to you? Provide your feedback about this. I will write more of such articles with more and more useful and simple Excel VBA Macro examples.<\/p>\n [\/et_pb_cta][et_pb_blurb title=”You may like reading them too” use_icon=”on” font_icon=”||fa||900″ _builder_version=”4.17.6″ _module_preset=”0249c68e-4de8-4f44-84ff-a9b1850785b6″ hover_enabled=”0″ global_colors_info=”{}” sticky_enabled=”0″]<\/p>\n [\/et_pb_blurb][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":" Dear friends, As you see this is the second and last part of the tutorial 40 Useful Excel Macro [VBA] examples. In a previous article, I published the first 20 examples. Download a FREE Excel Workbook with all 40 Examples At the end of this article, you will have a link to download a FREE […]<\/p>\n","protected":false},"author":45,"featured_media":242874,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":" Dear friends,<\/p> As you see this is the second and last part of the tutorial 40 Useful Excel Macro [VBA] examples<\/a><\/strong>. In a previous article,<\/strong><\/a> I published the first 20 examples.<\/p> At the end of this article, you will have a link to download a FREE copy of all 40 useful excel macros collections. Do not forget to download and play around and do provide your feedback.<\/p> Here in this last part remaining 20 examples are specified here.<\/p> Use the following piece of code to insert a single row or multiple rows in a worksheet. Following code can be used to insert a single column or multiple columns in a worksheet.<\/p> \u00a0<\/p> Using this code you can delete a single or multiple rows.<\/p> \u00a0<\/p> Using the below piece of code, you can delete a single or multiple columns.<\/p> \u00a0<\/p> Using the below piece of code, you can hide a single or multiple rows.<\/p> \u00a0<\/p> Using the below piece of code, you can hide a single or multiple columns.<\/p> \u00a0<\/p> Using the below piece of code, you can unhide a single or multiple rows.<\/p> \u00a0<\/p> Using the below piece of code, you can unhide a single or multiple columns.<\/p> [widget id=\"text-48\"]<\/p> \u00a0<\/p> Using the below piece of code, you can copy and insert any number of rows.<\/p> \u00a0<\/p> \u00a0<\/p> \u00a0<\/p> \u00a0<\/p> \u00a0<\/p> \u00a0<\/p> \u00a0<\/p> \u00a0<\/p> \u00a0<\/p> \u00a0<\/p> \u00a0<\/p> \u00a0<\/p> How did you find this collection of 40 Excel Macro examples? Did you find them useful to you? Provide your feedback about this. I will write more of such articles with more and more useful and simple Excel VBA Macro examples.<\/p> \u00a0<\/p> [button link=\"\/excel\/wp-content\/downloads\/Top-40-Useful-ExcelMacro-Examples.xlsm\" color=\"default\" size=\"\" stretch=\"\" type=\"\" shape=\"\" target=\"_blank\" title=\"\" gradient_colors=\"|\" gradient_hover_colors=\"|\" accent_color=\"\" accent_hover_color=\"\" bevel_color=\"\" border_width=\"\" icon=\"fa-download\" icon_position=\"right\" icon_divider=\"yes\" modal=\"\" animation_type=\"0\" animation_direction=\"left\" animation_speed=\"1\" animation_offset=\"\" alignment=\"center\" class=\"\" id=\"\"]DOWNLOAD -TOP 40 Excel Macro Examples Collection Workbook[\/button]<\/p><\/div>","_et_gb_content_width":"","footnotes":""},"categories":[1246,1679,1675,1682],"tags":[],"class_list":["post-14834","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-macro","category-excel-macro-beginner","category-excel-macro-for-beginners","category-popular-articles"],"yoast_head":"\nDownload a FREE Excel Workbook with all 40 Examples<\/h3>\n
\n
1. Excel Macro to insert a row in a worksheet<\/h2>\n
Note:<\/em><\/strong> Do not miss reading the comments specified inside the code. They are important to know more about the code.<\/p>\n\n\n Sub insertRowInWorksheet()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' insert row at a specific row number\n sh.Rows(4).Insert\n \n ' insert more than 1 row starting from a specific\n ' row. In below example there will be 3 rows\n ' inserted starting from row 3\n ' existing row 3rd will be shifted to 6th position\n sh.Rows(\"3:5\").EntireRow.Insert\n \n ' insert row below the selected cell\n ActiveCell.Rows.Insert\n \n End Sub\n<\/code><\/pre>\n
2. Excel Macro to insert a column in a worksheet<\/h2>\n
\n Sub insertColumnInWorksheet()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' insert column at a specific column name\n sh.Columns(B).Insert\n \n ' insert more than 1 column starting from a specific\n ' column. In below example there will be 3 columns\n ' inserted starting from column A\n sh.Columns(\"A:C\").Insert\n \n End Sub\n<\/code><\/pre>\n
3. Excel Macro to delete a row in a worksheet<\/h2>\n
\n Sub deleteRowInWorksheet()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' delete a specific row \n\t' row no:2 will be deleted\n sh.Rows(2).Delete\n \n ' delete more than one row\n\t' below statement will delete\n\t' all the rows 3, 4 and 5\n sh.Rows(\"3:5\").Delete\n \n End Sub\n<\/code><\/pre>\n
4. Excel Macro to delete a column in a worksheet<\/h2>\n
\n Sub deleteColumnInWorksheet()\n\n End Sub\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' delete a specific column\n sh.Columns(B).Delete\n \n ' delete more than one column\n\t' All the columns A, B and C will be deleted\n\t' at once by below statement\n sh.Columns(\"A:C\").Delete\n End Sub\n <\/code><\/pre>\n
5. Excel Macro to hide a row in worksheet<\/h2>\n
\n Sub hideRowInWorksheet()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' hide a specific row by providing the row number\n sh.Rows(2).Hidden = True\n \n ' hide more than one row at once\n\t' following statement will hide all\n\t' the rows 3, 4 and 5\n sh.Rows(\"3:5\").Hidden = True\n \n End Sub\n<\/code><\/pre>\n
6. Excel Macro to hide a column in worksheet<\/h2>\n
\n Sub hideColumnInWorksheet()\n\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' hide a specific column\n\t' column C will be hidden by this statement\n sh.Columns(\"C\").Hidden = True\n \n ' hide multiple columns using the below statement\n\t' below statement will hide all the columns\n\t' A, B and C\n sh.Columns(\"A:C\").Hidden = True\n End Sub\n <\/code><\/pre>\n
7. Excel Macro to unhide a row in worksheet<\/h2>\n
\n Sub unhideRowInWorksheet()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' unhide a specific hidden row\n sh.Rows(2).Hidden = False\n \n ' unhide more than 1 hidden rows \n\t' following statement will unhide \n\t' all the rows from 3 to 5\n sh.Rows(\"3:5\").Hidden = False\n \n End Sub\n<\/code><\/pre>\n
8. how to unhide a column in worksheet<\/h2>\n
\n Sub unhideColumnInWorksheet()\n\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' unhide a specific hidden column\n sh.Columns(\"C\").Hidden = False\n \n ' unhide multiple columns at once\n\t' multiple columns will be made visible \n\t' by the below statement - A, B and C\n sh.Columns(\"A:C\").Hidden = False\n End Sub\n<\/code><\/pre>\n\n
9. Excel Macro to copy and insert copied single or multiple rows before a specific row<\/h2>\n
\n Sub CopyAndInsertCopiedRow()\n\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' Copy 2nd row and insert this copied row at 10th row\n sh.Rows(2).EntireRow.Copy\n ' below statement by default paste the copied row\n ' exactly at the 10th row and rest of the rows\n ' will be shifted down\n sh.Rows(10).Insert\n \n ' copy more than one row and insert them all\n ' at a specific row\n ' Copy rows from 2 to 5 and paste them\n ' on 10th row. Excel will by default automatically\n ' shift that many rows down\n sh.Rows(\"2:5\").EntireRow.Copy\n sh.Rows(10).Insert\n End Sub\n<\/code><\/pre>\n
10. Excel macro to copy and insert copied column before a specific column<\/h2>\n
\n Sub CopyAndInsertCopiedColumn()\n\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' Copy Column A and insert this copied column at Column D\n sh.Columns(\"A\").EntireColumn.Copy\n ' below statement by default paste the copied column\n ' exactly at the Column - D and rest of the columns\n ' will be shifted right\n sh.Columns(\"D\").Insert\n \n ' copy more than one column and insert them all\n ' at a specific column\n ' Example: Copy columns from A to D and paste them\n ' on column F. Excel will by default automatically\n ' shift that many columns right\n sh.Columns(\"A:D\").EntireColumn.Copy\n sh.Columns(\"F\").Insert\n End Sub\n <\/code><\/pre>\n
11. Excel Macro to protect a worksheet without any password<\/h2>\n
\n Sub protectSheetWithoutPassword()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' protect this one sheet sh without any password\n sh.Protect\n End Sub\n <\/code><\/pre>\n
12. Excel Macro to protect a worksheet with a password<\/h2>\n
\n Sub protectSheetWithPassword()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' protect this one sheet sh without a very strong password\n ' like i have given below ;)\n sh.Protect Password:=\"password123\"\n End Sub\n <\/code><\/pre>\n
13. Excel Macro to unprotect a protected worksheet<\/h2>\n
\n Sub unprotectSheetWithoutPassword()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' unprotect a protected sheet which is not\n ' protected without giving any password\n sh.Unprotect\n End Sub\n <\/code><\/pre>\n
14. Excel Macro to unprotect a password protected worksheet<\/h2>\n
\n Sub unprotectSheetWithPassword()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' unprotect a protected sheet which is not\n ' protected without giving any password\n sh.Unprotect Password:=\"password123\"\n End Sub\n <\/code><\/pre>\n
15. Excel Macro to protect a workbook with password<\/h2>\n
\n Sub protectWorkbookWithPassword()\n Dim wb As Workbook\n Dim fPath As String\n Dim newFileName As String\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n newFileName = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n \n ' .saveAs provides a feature in excel vba\n ' to provide a password which will be asked\n ' when you try to open it again\n wb.SaveAs Filename:=newFileName, Password:=\"password123\"\n \n End Sub\n<\/code><\/pre>\n
16. Excel Macro to open a password protected workbook<\/h2>\n
\n Sub OpenProtectedWorkbookWithPassword()\n Dim wb As Workbook\n Dim fPath As String\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath, Password:=\"password123\")\n ' now you can use this workbook as normal\n End Sub\n<\/code><\/pre>\n
17. Excel Macro to clear contents of a Range without clearing formatting<\/h2>\n
\n Sub ClearContentOfRangeWithoutClearingFormatting()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' following statement will clear all the contents\n ' of 1st Range A1 to X5. This will keep the\n ' formatting as it is\n sh.Range(\"A1:X5\").ClearContents\n End Sub\n <\/code><\/pre>\n
18. Excel Macro to clear content of a range with formatting<\/h2>\n
\n Sub ClearContentAndFormatting()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' following statement will clear all the contents\n ' as well as any formatting done on these cells\n sh.Range(\"A1:P27\").Clear\n End Sub\n <\/code><\/pre>\n
19. Excel Macro to clear contents of a worksheet<\/h2>\n
\n Sub ClearContentOfWorksheet()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' To clear all contents + formatting together\n sh.UsedRange.Clear\n \n ' To clear all contents ONLY\n sh.UsedRange.ClearContents\n End Sub\n<\/code><\/pre>\n
20. Excel Macro to clear all the comments<\/h2>\n
\n\n Sub ClearAllComments()\n Dim wb As Workbook\n Dim fPath As String\n Dim sh As Worksheet\n \n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\n Set wb = Workbooks.Open(Filename:=fPath)\n Set sh = wb.Worksheets(1)\n ' following statement will clear all the contents\n ' as well as any formatting done on these cells\n sh.Range(\"A1:P27\").ClearComments\n End Sub\n<\/code><\/pre>\n
Download your Excel File with all 40 Useful Macro Collection<\/h4>\n
\n
\n<\/a><\/li>\nDownload a FREE Excel Workbook with all 40 Examples<\/h3>
1. Excel Macro to insert a row in a worksheet<\/h2>
Note:<\/em><\/strong> Do not miss reading the comments specified inside the code. They are important to know more about the code.<\/p>\r\n\r\n Sub insertRowInWorksheet()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' insert row at a specific row number\r\n sh.Rows(4).Insert\r\n \r\n ' insert more than 1 row starting from a specific\r\n ' row. In below example there will be 3 rows\r\n ' inserted starting from row 3\r\n ' existing row 3rd will be shifted to 6th position\r\n sh.Rows(\"3:5\").EntireRow.Insert\r\n \r\n ' insert row below the selected cell\r\n ActiveCell.Rows.Insert\r\n \r\n End Sub\r\n<\/code><\/pre>
2. Excel Macro to insert a column in a worksheet<\/h2>
\r\n Sub insertColumnInWorksheet()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' insert column at a specific column name\r\n sh.Columns(B).Insert\r\n \r\n ' insert more than 1 column starting from a specific\r\n ' column. In below example there will be 3 columns\r\n ' inserted starting from column A\r\n sh.Columns(\"A:C\").Insert\r\n \r\n End Sub\r\n<\/code><\/pre>
3. Excel Macro to delete a row in a worksheet<\/h2>
\r\n Sub deleteRowInWorksheet()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' delete a specific row \r\n\t' row no:2 will be deleted\r\n sh.Rows(2).Delete\r\n \r\n ' delete more than one row\r\n\t' below statement will delete\r\n\t' all the rows 3, 4 and 5\r\n sh.Rows(\"3:5\").Delete\r\n \r\n End Sub\r\n<\/code><\/pre>
4. Excel Macro to delete a column in a worksheet<\/h2>
\r\n Sub deleteColumnInWorksheet()\r\n\r\n End Sub\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' delete a specific column\r\n sh.Columns(B).Delete\r\n \r\n ' delete more than one column\r\n\t' All the columns A, B and C will be deleted\r\n\t' at once by below statement\r\n sh.Columns(\"A:C\").Delete\r\n End Sub\r\n <\/code><\/pre>
5. Excel Macro to hide a row in worksheet<\/h2>
\r\n Sub hideRowInWorksheet()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' hide a specific row by providing the row number\r\n sh.Rows(2).Hidden = True\r\n \r\n ' hide more than one row at once\r\n\t' following statement will hide all\r\n\t' the rows 3, 4 and 5\r\n sh.Rows(\"3:5\").Hidden = True\r\n \r\n End Sub\r\n<\/code><\/pre>
6. Excel Macro to hide a column in worksheet<\/h2>
\r\n Sub hideColumnInWorksheet()\r\n\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' hide a specific column\r\n\t' column C will be hidden by this statement\r\n sh.Columns(\"C\").Hidden = True\r\n \r\n ' hide multiple columns using the below statement\r\n\t' below statement will hide all the columns\r\n\t' A, B and C\r\n sh.Columns(\"A:C\").Hidden = True\r\n End Sub\r\n <\/code><\/pre>
7. Excel Macro to unhide a row in worksheet<\/h2>
\r\n Sub unhideRowInWorksheet()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' unhide a specific hidden row\r\n sh.Rows(2).Hidden = False\r\n \r\n ' unhide more than 1 hidden rows \r\n\t' following statement will unhide \r\n\t' all the rows from 3 to 5\r\n sh.Rows(\"3:5\").Hidden = False\r\n \r\n End Sub\r\n<\/code><\/pre>
8. how to unhide a column in worksheet<\/h2>
\r\n Sub unhideColumnInWorksheet()\r\n\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' unhide a specific hidden column\r\n sh.Columns(\"C\").Hidden = False\r\n \r\n ' unhide multiple columns at once\r\n\t' multiple columns will be made visible \r\n\t' by the below statement - A, B and C\r\n sh.Columns(\"A:C\").Hidden = False\r\n End Sub\r\n<\/code><\/pre>
9. Excel Macro to copy and insert copied single or multiple rows before a specific row<\/h2>
\r\n Sub CopyAndInsertCopiedRow()\r\n\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' Copy 2nd row and insert this copied row at 10th row\r\n sh.Rows(2).EntireRow.Copy\r\n ' below statement by default paste the copied row\r\n ' exactly at the 10th row and rest of the rows\r\n ' will be shifted down\r\n sh.Rows(10).Insert\r\n \r\n ' copy more than one row and insert them all\r\n ' at a specific row\r\n ' Copy rows from 2 to 5 and paste them\r\n ' on 10th row. Excel will by default automatically\r\n ' shift that many rows down\r\n sh.Rows(\"2:5\").EntireRow.Copy\r\n sh.Rows(10).Insert\r\n End Sub\r\n<\/code><\/pre>
10. Excel macro to copy and insert copied column before a specific column<\/h2>
\r\n Sub CopyAndInsertCopiedColumn()\r\n\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' Copy Column A and insert this copied column at Column D\r\n sh.Columns(\"A\").EntireColumn.Copy\r\n ' below statement by default paste the copied column\r\n ' exactly at the Column - D and rest of the columns\r\n ' will be shifted right\r\n sh.Columns(\"D\").Insert\r\n \r\n ' copy more than one column and insert them all\r\n ' at a specific column\r\n ' Example: Copy columns from A to D and paste them\r\n ' on column F. Excel will by default automatically\r\n ' shift that many columns right\r\n sh.Columns(\"A:D\").EntireColumn.Copy\r\n sh.Columns(\"F\").Insert\r\n End Sub\r\n <\/code><\/pre>
11. Excel Macro to protect a worksheet without any password<\/h2>
\r\n Sub protectSheetWithoutPassword()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' protect this one sheet sh without any password\r\n sh.Protect\r\n End Sub\r\n <\/code><\/pre>
12. Excel Macro to protect a worksheet with a password<\/h2>
\r\n Sub protectSheetWithPassword()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' protect this one sheet sh without a very strong password\r\n ' like i have given below ;)\r\n sh.Protect Password:=\"password123\"\r\n End Sub\r\n <\/code><\/pre>
13. Excel Macro to unprotect a protected worksheet<\/h2>
\r\n Sub unprotectSheetWithoutPassword()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' unprotect a protected sheet which is not\r\n ' protected without giving any password\r\n sh.Unprotect\r\n End Sub\r\n <\/code><\/pre>
14. Excel Macro to unprotect a password protected worksheet<\/h2>
\r\n Sub unprotectSheetWithPassword()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' unprotect a protected sheet which is not\r\n ' protected without giving any password\r\n sh.Unprotect Password:=\"password123\"\r\n End Sub\r\n <\/code><\/pre>
15. Excel Macro to protect a workbook with password<\/h2>
\r\n Sub protectWorkbookWithPassword()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim newFileName As String\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n newFileName = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n \r\n ' .saveAs provides a feature in excel vba\r\n ' to provide a password which will be asked\r\n ' when you try to open it again\r\n wb.SaveAs Filename:=newFileName, Password:=\"password123\"\r\n \r\n End Sub\r\n<\/code><\/pre>
16. Excel Macro to open a password protected workbook<\/h2>
\r\n Sub OpenProtectedWorkbookWithPassword()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath, Password:=\"password123\")\r\n ' now you can use this workbook as normal\r\n End Sub\r\n<\/code><\/pre>
17. Excel Macro to clear contents of a Range without clearing formatting<\/h2>
\r\n Sub ClearContentOfRangeWithoutClearingFormatting()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' following statement will clear all the contents\r\n ' of 1st Range A1 to X5. This will keep the\r\n ' formatting as it is\r\n sh.Range(\"A1:X5\").ClearContents\r\n End Sub\r\n <\/code><\/pre>
18. Excel Macro to clear content of a range with formatting<\/h2>
\r\n Sub ClearContentAndFormatting()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' following statement will clear all the contents\r\n ' as well as any formatting done on these cells\r\n sh.Range(\"A1:P27\").Clear\r\n End Sub\r\n <\/code><\/pre>
19. Excel Macro to clear contents of a worksheet<\/h2>
\r\n Sub ClearContentOfWorksheet()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' To clear all contents + formatting together\r\n sh.UsedRange.Clear\r\n \r\n ' To clear all contents ONLY\r\n sh.UsedRange.ClearContents\r\n End Sub\r\n<\/code><\/pre>
20. Excel Macro to clear all the comments<\/h2>
\r\n\r\n Sub ClearAllComments()\r\n Dim wb As Workbook\r\n Dim fPath As String\r\n Dim sh As Worksheet\r\n \r\n fPath = \"C:\\Users\\vmishra\\Desktop\\myfile.xlsx\"\r\n Set wb = Workbooks.Open(Filename:=fPath)\r\n Set sh = wb.Worksheets(1)\r\n ' following statement will clear all the contents\r\n ' as well as any formatting done on these cells\r\n sh.Range(\"A1:P27\").ClearComments\r\n End Sub\r\n<\/code><\/pre>
Download your Excel File with all 40 Useful Macro Collection<\/h2>