Posts

Showing posts from September, 2019

Things I wished I know when I Started programming VBA

1.        Use auto complete word: Control + Space This invokes intellisense A further press of the Tab key will select the highlighted word If the Spacebar key instead of the Tab key is pressed it will add a space after selecting the highlighted word. 2.        View the definition: Shift + F2  and back to View the last position: Control + Shift + F2 2.1          If we have the following code in our module: Sub Calc()                   Dim result as Long                 Result = PerformCalc(5, 10)                 MsgBox “Result is “ & result                 End...

Most feasible way to work with Worksheet names in VBA code

1.        When you look in your VBA project screen it will represent a worksheet name   “Sheet1” (which is the default name when you open a workbook) as follows in the VBA project explorer: Microsoft Excel Objects Sheet1(Sheet1)                 ß The name in brackets is the name as you see it in the workbook and is the worksheet name.                                                 ß The name on the left is the code name. The worksheet name can be changed in the workbook or in the code module by selecting the sheet object and then changing the Name property in the properties window. The code name can only be changed in the properties window under the (Name) property. 2.        To refer to the worksheet name in code w...

Shortcut keys for VBA coding

1. Display the immediate window: Control + G. This window is giving the result of debug.print commands 2. Auto Complete Word: Control + Space This invokes intellisense A further press of the Tab key will select the highlighted word If the Spacebar key instead of the Tab key is pressed it will add a space after selecting the highlighted word. 3. View the Watch Window: Alt + V + H 4. Tab: To move lines of code to the right (Indent). 5. Shift + Tab: To move lines of code to the left (Out dent). 6. Shift + F2: Get the definition of the item under the cursor. 7. Ctrl + Shift + F2: Go to the last cursor position. 8. Alt + F11: Switch between Excel and the VBA Editor. 9. Ctrl + R: View the Project Explorer Window. 10. Ctrl + Shift + 8 (or Ctrl + *): Get the current region on a worksheet. 11. F4: View the Properties Window. 12. F5: Run the code from the current sub. 13. F8: Step into the code. 14. F9 (or click left margin): Add a breakpoint to pause the code. ...

Basic practices for easy readable VBA code

1        Use option explicit This will force you to declare variables. Option explicit can be typed in at the top of your module before any code or it can be automatically placed there by selecting the following items from the menu: à Tools Options and then select “require variable declaration”    2        Declare the variables as you use them If you declare the variables at the top of the code it becomes very messy 3        Indent your code and use blank lines Shift + Tab align all the code up Tab can then be used to indent the code line by line where appropriate                between loops and other programming statements. 4        Give variables nice names Use long descriptive names. You don’t have to use e.g. ‘lng’ in front if it is a long variable. Control + Space will help to complet...