{"id":14625,"date":"2018-03-17T23:50:00","date_gmt":"2018-03-17T23:50:00","guid":{"rendered":"http:\/\/learnexcelmacro.com\/wp\/?p=14625"},"modified":"2018-03-18T08:22:17","modified_gmt":"2018-03-18T08:22:17","slug":"what-is-immediate-window-and-how-to-use-it-in-excel-vba","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2018\/03\/what-is-immediate-window-and-how-to-use-it-in-excel-vba\/","title":{"rendered":"What is Immediate Window and how to use it in Excel VBA"},"content":{"rendered":"
Dear Friends,<\/p>\n
Have you heard of Immediate Window in Excel VBA programming? Do you know how to make use of it? <\/p>\n
If you have not been using this while doing vba programming then trust me you were missing something really cool stuff which excel provided to the programmers.<\/p>\n
In this article I am going to explain you the following<\/p>\n
1. What is immediate window in Excel VBA and how to open it?
\n2. What is the use of Immediate windows?
\n3. Top 5 Usage of immediate window with examples\n<\/p><\/div>\n
Immediate window is a plane white background simple window in Visual Basic Editor, which can be used for different purposes. By Default, this window is not visible when you open the VBE in your excel. To view this window in your Excel VBE Click on View –> Immediate Window ( or Use Shortcut key Ctrl + G) <\/em><\/strong>to view this window as shown in below picture<\/p>\n Excel VBA – View Immediate Window<\/p><\/div>\n This simple looking window is not as simple as it looks. It is powerful. Excel VBA – Immediate-Window<\/p><\/div><\/p>\n This is the screen where you can see the result of statement Debug.print xyz<\/i> statement which is executed while running your VBA program. Immediate Window – DebugPrint<\/p><\/div>\n Immediate window can be used to execute excel vba statements directly here. You do not need to write the complete function structure to execute. Now when you execute this function then you get the sheets count in a message box. <\/p>\n Using immediate window, you can execute this statement Worksheets.Count<\/em><\/strong> directly and get the count in immediate window itself like shown in below image:<\/p>\n Execute VBA Statement in Immediate Window<\/p><\/div>\n Similarly you can execute any statement here in this window.<\/p>\n If you want to test a function, especially which expects some input parameters, you can call them by using this immediate window.
\nAs the name of this window itself suggests… this helps in doing many thing while VBA programming very fast (immediately). That could be the main reason why this window was named as “Immediate Window”<\/strong>
\n Usage of this immediate window <\/h1>\n
#1 – To display the result of Debug.print xyz<\/i> statement in VBA <\/h1>\n
\nDebug.print variable, data etc.<\/em> <\/strong> is the statement which is used to print value of any variable, object etc. <\/p>\n #2 – Simplest way to execute any Excel VBA statement <\/h1>\n
\nExample:<\/em><\/strong>To get the count of all sheets in a Workbook, you need to write the complete function in VBE like this<\/p>\n\r\n
\r\n\r\nSub getSheetCount()\r\n\r\nMsgBox Worksheets.Count\r\n\r\nEnd Sub\r\n\r\n<\/code>\r\n<\/pre>\n
#3 – You can call any Macro\/Function from this immediate window<\/h1>\n
\nExample:<\/em><\/strong> You have written a simple business function to calculate factorial of given number (input parameter). To test this function, you can not run it without passing the input parameter to this function. To do this, you can simply call this function from immediate window by providing the input parameter.<\/p>\n