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 



InputBox function in Excel VBA Code

InputBox Function

You can use the InputBox function in Excel VBA to prompt the user to enter a value. Place a command button on your worksheet and add the following code lines:

1. First, declare the variable myValue of type Variant.
Dim myValue As Variant

Note: we use a variable of type Variant here because a Variant variable can hold any type of value. This way the user can enter text, numbers, etc.

2. Add the following code line to show the input box.
myValue = InputBox("Give me some input")

InputBox Function in Excel VBA

3. Write the value of myValue to cell A1.
Range("A1").Value = myValue

Result when the user enters the value 5 and clicks the OK button.

InputBox Function Result

4. The InputBox function has more optional arguments. The following code line shows an input box with a title displayed in the title bar and has a default value. The default value will be used if no other input is provided.
myValue = InputBox("Give me some input", "Hi", 1)

InputBox Function in Excel VBA 







Result when the user only clicks the OK button.

InputBox Function Result

Note: Place your cursor on InputBox in the Visual Basic Editor and click F1 for help on the other optional arguments.

VBA Code for excel to send multiple emails using Outlook

In this tutorial we will explain you, how you can send the multiple e-mails from Microsoft Excel using Outlook. The best part of this code is, each recipient will receive the email individually. Before proceeding further there are some prerequisite:

You need to make the excel sheet format as defined below:

  1. Place the list of e-mail addresses under column "A"
  2. Place the e-mail subject line in cell "B1"
  3. Place the e-mail body message in cell "B2" and then,
  4. Use the following VBA code to send the email to each individual email address using MS outlook.

Note: This code will send the e-mail to each e-mail address listed in the Column "A" starting at row 2 down to the last row.

At the moment this code is set to display before sending. If you don't want to see or review each e-mail before sending then, you can disable or comment the display line and enable or un-comment the Send line.

Code:

Dim i As Integer, Mail_Object, Email_Subject, o As Variant, lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
Set Mail_Object = CreateObject("Outlook.Application")
For i = 2 To lr
       With Mail_Object.CreateItem(o)
           .Subject = Range("B1").Value
           .To = Range("A" & i).Value
           .Body = Range("B2").Value
           '.Send
           .display 'disable display and enable send to send automatically
    End With
Next i
       MsgBox "E-mail successfully sent", 64
       Application.DisplayAlerts = False
Set Mail_Object = Nothing
End Sub


VBA Code to show MsgBox in MS Excel

In this tutorial we are going to explain you how you show the MsgBox in Microsoft Excel using the VBA Code. The MsgBox is a dialog box in Excel VBA, you can use To inform the users of your program. Place a command button On your worksheet and add the following code lines: 

  1. Code To show a simple message in the MsgBox:
  2.     MsgBox "This Is fun"
    

    Result when you click the command button On the sheet: 

    msg box image

  3. Code To show a little more advanced message. First, enter a number into cell A1 And Then run the below code:
  4.     MsgBox "Entered value Is " & Range("A1").Value
    
    Result when you click the command button On the sheet:



    msg box with value image


    Note: we used the & operator To concatenate (join) two strings. Although Range("A1").value Is Not a string, it works here.

  5. To start a new line in a message, use vbNewLine.
  6.     MsgBox "Line 1" & vbNewLine & "Line 2"
    
    Result when you click the command button On the sheet:
msg box in multiple lines image


Sunday, February 8, 2015

VBA Code to Remove Specific Value from a table in MS Excel

Use the Below VBA Code to Remove Specific Value from a table in MS Excel from your active worksheet


 Sub RemoveSpecificValue()  
 'Removing Specific Values whose begins with "FR0"  
    ActiveSheet.Range("A:G").AutoFilter Field:=1, Criteria1:="=FR0*" _  
     , Operator:=xlAnd  
   ' Delete AutoFiltered rows except the header  
   lr = Cells(Rows.Count, 1).End(xlUp).Row  
   If lr > 1 Then  
     Range("A2:A" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete  
   End If  
 End Sub  


VBA Code to Delete Multiple Rows in MS Excel

Use Below VBA Code to Delete Multiple Rows in MS Excel from your active worksheet

Sub DeleteRows()
'Delete the Row 2 and 4 in active worksheet
    Rows("2:2,4:4").Select
    Selection.Delete Shift:=xlUp
End Sub

VBA Code to Delete Multiple Columns in MS Excel

Use Below VBA Code to Delete Multiple Columns in MS Excel from your active worksheet
Sub DeleteColumns()

'Delete the Column B and D in the active worksheet

    Columns("B:B, D:D").Select 

    Selection.Delete Shift:=xlToLeft

End Sub


VBA Code to autofit columns in MS Excel

Use Below VBA Code to autofit the columns in MS Excel as per you requirement

Sub AutoFitColumns()
     Dim sht As Worksheet

'AutoFit all the Columns in active worksheet
    Cells.Select
    Cells.EntireColumn.AutoFit

'AutoFit One Column
    ThisWorkbook.Worksheets("Sheet1").Columns("O:O").EntireColumn.AutoFit
  
'AutoFit Multiple Columns
    ThisWorkbook.Worksheets("Sheet1").Range("I:I,L:L").EntireColumn.AutoFit 'Columns I & L
    ThisWorkbook.Worksheets("Sheet1").Range("I:L").EntireColumn.AutoFit 'Columns I to L
  
'AutoFit All Columns on Worksheet
    ThisWorkbook.Worksheets("Sheet1").Cells.EntireColumn.AutoFit

'AutoFit Every Worksheet Column in a Workbook
    For Each sht In ThisWorkbook.Worksheets
        sht.Cells.EntireColumn.AutoFit
    Next sht


End Sub