Showing posts with label VBA Tips. Show all posts
Showing posts with label VBA Tips. Show all posts

Monday, August 31, 2020

How to Assign a Macro

In this tutorial we are going to explain how you can Assign a Macro to the Command Button in the Microsoft Excel.

To assign a macro (one or more code lines) to the command button, execute the following steps:
  1. Right click CommandButton1 (make sure Design Mode is selected).
  2. Click View Code.

    View Code Image
    View Code
    The Visual Basic Editor appears.

  3. Place your cursor between Private Sub CommandButton1_Click() and End Sub.
  4. Add the code line shown below:
        Range("A1").Value = "Hello"
    
    Add Code Line Image
    Add Code Line
    Note: The window on the left with the name Sheet1 (Sheet1) and ThisWorkbook is called the Project Explorer. If the Project Explorer is not visible, click on View menu and then click on Project Explorer. If the Code window for Sheet1 is not visible, click Sheet1 (Sheet1). You can ignore the Option Explicit statement for now.

  5. Close the Visual Basic Editor.
  6. Click the command button on the sheet (make sure Design Mode is deselected).

Result:

Macro Result Image
Macro Result
Congratulations. You've just created a macro in Excel!



Saturday, August 29, 2020

How to Insert a Command Button

In this tutorial we are going to explain how you can Insert the Command Button in the Microsoft Excel.

To place a command button into your worksheet, execute the following steps:
  1. In the Developer tab, click on Insert option.
  2. In the ActiveX Controls group, click on Command Button.

    Insert Command Button Control Image
    Insert Command Button Control

  3. Drag a command button on your worksheet.


Wednesday, August 26, 2020

Create Your First VBA Macro

In this tutorial, we are going to explain you the Basics of a VBA Macro in the Microsoft Excel and step-by-step guide to create your first VBA Macro.

What is a Macro?

A series of commands or instructions that are combined to form a single command are known as Macros. Macros are getting created by writing the VBA code with the set of instruction in the Visual Basic Editor of Microsoft Excel.

Benefits of a Macro

Macros can save your time by letting you automate relatively simple tasks that you need to perform often, as well as complex procedures that consist of many steps. 

In this tutorial, you will learn how to create a simple macro which will be executed after clicking on a command button. To do so, First, turn on the Developer tab.

Click on the below respective link to know, how to Turn On the Developer Tab or Insert Command Button or Assign a Macro to the command button or how to open Visual Basic Editor in the Microsoft Excel.

Developer Tab | Command Button | Assign a Macro | Visual Basic Editor



Developer Tab:

In this tutorial we are going to explain how you can turn on the Developer Tab in the Microsoft Excel.

To turn on the Developer tab, execute the following steps.
  1. Right click anywhere on the ribbon, and then click Customize the Ribbon (as shown in the below screenshot).

    Customize Ribbon Image
    Customize Ribbon
    Alternatively, you can go to File Menu then Click on Options and then click on Customize the Ribbon option shown in the left pane of the Excel Options window, the same window will open as shown you in the below image.

  2. Under Customize the Ribbon, on the right side of the dialog box, select Main tabs (if necessary).
  3. Check the Developer check box.

    Turn On Developer Tab Image
    Turn On Developer Tab
  4. Click OK.
  5. You can find the Developer tab next to the View tab.

    Developer Tab Image
    Developer Tab


Command Button:

In this tutorial we are going to explain how you can Insert the Command Button in the Microsoft Excel.
 
To place a command button into your worksheet, execute the following steps:
  1. In the Developer tab, click on Insert option.
  2. In the ActiveX Controls group, click on Command Button.

    Insert Command Button Control Image
    Insert Command Button Control

  3. Drag a command button on your worksheet.


Assign a Macro:

In this tutorial we are going to explain how you can Assign a Macro to the Command Button in the Microsoft Excel.

To assign a macro (one or more code lines) to the command button, execute the following steps:
  1. Right click CommandButton1 (make sure Design Mode is selected).
  2. Click View Code.

    View Code Image
    View Code
    The Visual Basic Editor appears.

  3. Place your cursor between Private Sub CommandButton1_Click() and End Sub.
  4. Add the code line shown below:
        Range("A1").Value = "Hello"
    
    Add Code Line Image
    Add Code Line
    Note: The window on the left with the name Sheet1 (Sheet1) and ThisWorkbook is called the Project Explorer. If the Project Explorer is not visible, click on View menu and then click on Project Explorer. If the Code window for Sheet1 is not visible, click Sheet1 (Sheet1). You can ignore the Option Explicit statement for now.

  5. Close the Visual Basic Editor.
  6. Click the command button on the sheet (make sure Design Mode is deselected).

Result:

Macro Result Image
Macro Result
Congratulations. You've just created a macro in Excel!



Visual Basic Editor

In this tutorial we are going to explain how you can open the Visual Basic Editor in the Microsoft Excel.

To open the Visual Basic Editor, Go to the Developer tab and then click on Visual Basic.

Open Visual Basic Editor Image
Open Visual Basic Editor
The Visual Basic Editor appears.

Visual Basic Editor Image
Visual Basic Editor


Thursday, February 19, 2015

Separate Strings through VBA Code

In this tutorial we are going to explain you how you can separate the strings using the VBA Macro Code in Microsoft Excel.

Below Is the Situation in which you need to separate the Full Name string to the First Name and the Last Name string:

Separate Strings Excel Query Image

You can place a command button on your worksheet (as shown in the above screenshot) And add the following code lines: 

  1. We Declare a variable called Full_Name of Type String, a variable called Comma_Position of Type Integer, And a variable called i of Type Integer.
  2.     Dim Full_Name As String, Comma_Position As Integer, i As Integer
    

    The problem we are dealing With Is that we need To tell Excel VBA from where we want To separate the string. In Case of Smith, Mike the comma is at position 6 While in Case of Williams, Janet the comma is at position 9. 




  3. We use a loop to execute the operations on each name entered in Excel. First, we initialize the variable Full_Name. Next, we use the Instr function to find the position of the comma.
  4.     For i = 2 To 7
        Full_Name = Cells(i, 1).Value
        Comma_Position = InStr(Full_Name, ",")
    
    Note: Currently this code is working from Row No. 2 to 7. You can modify it as per your requirement.

  5. Now, we want to write the part after the comma to Column B and the part in front of the comma to column C. You can achieve this by adding below mentioned lines in you code:
  6. Cells(i, 2).Value = Mid(Full_Name, Comma_Position + 2)
    Cells(i, 3).Value = Left(Full_Name, Comma_Position - 1)
    
    Mid(Full_Name, Comma_Position + 2) means we want the part of Full Name starting at character 'Comma_Position + 2' (this is exactly the first name).

    Left(Full_Name, Comma_Position - 1) means we want the part of Full Name starting at the beginning until character 'Comma_Position - 1' (this is exactly the last name).

  7. Now, close the loop.
  8.     Next i
    


  9. Final Code will look like below:
  10. Sub Separate_Strings()
    
        Dim Full_Name As String, Comma_Position As Integer, i As Integer
    
        For i = 2 To 7 'You can modify it as per your requirement.
            Full_Name = Cells(i, 1).Value
            Comma_Position = InStr(Full_Name, ",")
        
        Cells(i, 2).Value = Mid(Full_Name, Comma_Position + 2)
        Cells(i, 3).Value = Left(Full_Name, Comma_Position - 1)
        
        Next i
    
    End Sub
    
  11. To test the program, Add six names separated by a comma and space to Range("A2:A7") as shown in the above screenshot.
  12. Test the program.

Result:

Separate Strings Excel Result Image