{"id":12177,"date":"2013-06-18T15:09:53","date_gmt":"2013-06-18T15:09:53","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=3035"},"modified":"2023-05-21T17:55:29","modified_gmt":"2023-05-21T17:55:29","slug":"few-very-useful-and-small-macros","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2013\/06\/few-very-useful-and-small-macros\/","title":{"rendered":"5 very useful and small macros"},"content":{"rendered":"
In the first part of this article, I am going to share very small but useful VBA codes which are most frequently used in day-to-day VBA programming. Many of you had requested them so I have clubbed all of them together and made one article \ud83d\ude42 Enjoy reading this.<\/p>\n
You can call the below function to close all open workbooks:<\/p>\n
\r\n\r\nPublic Sub CloseAllWorkbooks()\r\nDim Wb As Workbook\r\n' Workbooks is the collection of all open workbooks\r\nFor Each Wb In Workbooks\r\n If Wb.Name <> ThisWorkbook.Name Then\r\n Wb.Close savechanges:=True 'Set False if you want them to close without saving it\r\n End If\r\nNext Wb\r\n' This statement will close the current workbook \r\n' where vba code is running. Below stattement can \r\n' not be written before\r\nThisWorkbook.Close savechanges:=True\r\n\r\nEnd Sub\r\n\r\n<\/code><\/pre>\nClosing All Inactive Workbooks<\/h1>\n
You can call the below function to close all inactive workbooks:<\/p>\n
\r\n\r\nPublic Sub CloseAllInactiveWorkbooks()\r\nDim Wb As Workbook\r\nDim WbName As String\r\nWbName = ActiveWorkbook.Name\r\n ' Workbooks is the collection of all open Workbooks\r\nFor Each Wb In Workbooks\r\n If Wb.Name <> WbName Then\r\n Wb.Close savechanges:=True\r\n End If\r\nNext Wb\r\n\r\nEnd Sub\r\n\r\n<\/code><\/pre>\nSaving All Open Workbooks<\/h1>\n
Below code will save all the open workbooks:
\n <\/p>\n
\r\nPublic Sub SaveAllWorkbook()\r\nDim WB As Workbook\r\nFor Each WB In Workbooks\r\n WB.Save\r\nNext WB\r\nEnd Sub\r\n\r\n<\/code><\/pre>\nGet the name of First Sheet in a Workbook<\/h1>\n
Keyword Sheets<\/strong> is a collection of all Sheets and WorkSheets of a workbook. Each sheet can be referred by passing the index number. <\/p>\nFor example:<\/strong> First sheet can be referred as Sheets(1), Second as Sheets(2) and so on…<\/p>\nBelow code will give you name of the first sheet in your workbook. <\/p>\n
\r\nPublic Sub FirstSheetName()\r\n Dim firstSheet As String\r\n firstSheetName = Sheets(1).Name\r\nEnd Sub\r\n\r\n<\/code><\/pre>\nGet the name of Last Sheet in a Workbook<\/h1>\n
Below code will give you name of the last sheet in your workbook. <\/p>\n
Note: Count is the property of Object Sheets which returns the total number of sheets available in the workbook<\/i><\/span><\/p>\n\r\nPublic Sub LastSheetName()\r\n Dim firstSheet As String\r\n firstSheetName = Sheets(Sheets.Count).Name\r\nEnd Sub\r\n\r\n<\/code><\/pre>\n<\/span>","protected":false},"excerpt":{"rendered":"In the first part of this article, I am going to share very small but useful VBA codes which are most frequently used in day-to-day VBA programming. Many of you had requested them so I have clubbed all of them together and made one article \ud83d\ude42 Enjoy reading this. Index Closing All Open Workbooks Closing […]<\/p>\n","protected":false},"author":45,"featured_media":242690,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1679,1676,1678,1682],"tags":[],"class_list":["post-12177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-excel-macro-beginner","category-excel-tips","category-interesting-vba-functions","category-popular-articles"],"yoast_head":"\n
5 very useful and small macros - Let's excel in Excel<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n\n\n\n\t\n\t\n\t\n