Wednesday, September 7, 2022

To Sort Data Ascending Or Descending with Header

Below code to sort data with header ascending or descending. For example data with 3 columns.

Option Explicit
Sub ToSortWithHeader()

    Columns("A:C").Select
    Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, _
    Key2:=Range("B2"), Order2:=xlAscending, _
    Key3:=Range("C2"), Order3:=xlAscending, _
    Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
    DataOption2:=xlSortNormal, DataOption3:=xlSortNormal
    Range("A1").Select
    
End Sub

Note: Selection must include header and for Descending just change xlAscending to xlDescending.

Thursday, August 18, 2022

How to open Visual Basic Editor under Microsoft Excel?

Visual Basic Editor (VBE) is the place to write and store all Visual Basic code and Macro recorded code. This VBE attached to all office application for example Microsoft Word, Microsoft PowerPoint and etc. but in this blog our focus only VBE in Microsoft Excel. To open this VBE:

Two ways to open Visual Basic Editor in Excel:

  1. 1st Method: Under Developer tab click at Visual Basic.









    Click here if Developer tab is not visible.

  2. 2nd Method: Short cut key by Pressing Alt + F11 button.

  3. Visual Basic Editor will open as below:



















 

Practice makes perfect. Thank You.

Get started with all Function and Formula in Excel

To be a good programmer in Microsoft Excel we should understand every function in excel itself. To open all these function:

  1. Click Home tab.













  2. Under Home tab Click at Arrow down button beside sum symbol.

















  3. Click at More Functions.

  4. Select a category.






















  5. Select a function and this function or formula will be inserted into selected cell.

All this functions already build in and can be use instantly or call using VBA code. To create new function we must create using VBA code inside Excel workbook and this will appeared under this insert function dialog box.

The most importance function is under Math & Trig which is related to number  and Text which is related to string.

Wednesday, August 17, 2022

Get started with recording macros to learn VBA code

Better way to learn VBA code is to record every move or action and check the recorded code. From here we can learn and modify the code to suit your need. 

To record macro:

  1. Under Developer tab Click at Record Macro.









  2. Under Record Macro give your Macro name.


















    Default name given is Macro1,2,3..... but we can rename to differentiate the function.
    Macro name must begin with letter, maximum character 255 and must not contains space, period (.), exclamation mark (!), or the characters @, &, $, # in the name.
    Detail we refer here:
    Microsoft Reference - Macro naming rules

    Note: Under this dialog box you can choose to store recorded macro under This workbook, New Workbook or Personal Macro Workbook and also create short cut key to run this macro later.

  3. Click OK to begin recording and continue your work in Excel as usual.

  4. Upon completed click at Stop Recording.


     






  5. Save this file under Excel Macro-Enabled Workbook(*.xlsm).
















 

 To playback recorded macro:

  1. Under Developer tab Click at Macros.









  2. Under Macro select Macro name: and click Run.






















    Note: Under this dialog box you can choose macros in This Workbook or All Open Workbook.

  3. All the step recorded before run automatically.

To check recorded code:

  1. Under Developer tab Click at Macros.









  2. Under Macro select Macro name: and click Edit.






















  3. Visual Basic Editor will open and show code.



















    The code will be under module, try to understand and edit the code and run again and again.

Practice makes perfect. Thank You.

Thursday, August 11, 2022

How to set security to enable macros in Microsoft Excel?

By default any Macro or VBA code inside or Excel file will be disable for security reason. Therefore we must understand the risk but for me as long we know the source then should be OK.

Follow this step to change or set the security to enable macros:

METHOD 1:

  1. Open any Excel file and click at File top left.










  2. Click at Options below left.











  3. Click at Trust Center under Excel Option.



















  4. Click at Trust Center Setting under Excel Option.

















  5. Click at Macro Settings under Trust Center.


























    Choose either one but since we know the source code prefer to choose no 4 which is
    "Enable all macros (not recommended; potentially dangerous code can run)" to ease our VBA code writing later on.

  6.  Click OK.

          OR

METHOD 2:

  1. Click at Developer tab on top follow by Macro Security.










  2. Repeat Step 5 and 6 in Method 1.

Macro security setting is completed.

Microsoft Reference