vba examples
DESCRIPTION
visual basic for application examplesTRANSCRIPT
In this example I am writing the data to first Cell of the Worksheet.
Sub sbWriteIntoCellData()
Cells(1, 1)="Hello World"'Here the first value is Row Value and the second one is column value 'Cells(1, 1) means first row first columnEnd Sub
In this example I am reading the data from Range B3 and Writing the data into C5 using Cell Object.
Sub sbReadWriteCellExample1()
'Using Cell ObjectCells(5, 3) = Cells(3, 2)MsgBox Cells(5, 3)End Sub
In this example I am reading the data from first Cell of the worksheet.
Sub sbGetCellData()
MsgBox Cells(1, 1)'Here the first value is Row Value and the second one is column value 'Cells(1, 1) means first row first columnEnd Sub
In this example I am selecting a Range using Select method of Range.
Sub sbSelectARange()'You can also use Range ObjectRange("C3").Select
'Collection of Cells OR Multiple Cells = RangeRange ("B2:C4").Select ' It will Select B2,B3,B4,C2,C3,C4
End Sub
'In this example I am Copying the Data from Range ("A1:B10") to Range(E1")Sub sbCopyRange()
'Method 1Range("A1:B10").Copy Destination:=Range("E1")'Here the first part is source range, 'and the second part is target range or destination.
'Target can be either one cell or same size of the source range.
'Method 2Range("A1:B10").CopyRange("E1").SelectActiveSheet.Paste
'In the second method, first we copy the source data range'Then we select the destination range'Target can be either one cell or same size of the source range.'Then we paste in the active sheet, so that it will paste from active range
Application.CutCopyMode = False
End Sub
Instructions:
1. Open an excel workbook2. Enter some data in Sheet1 at A1:B103. Press Alt+F11 to open VBA Editor4. Insert a Module for Insert Menu5. Copy the above code and Paste in the code window6. Save the file as macro enabled workbook7. Press F5 to run it
Now you should see the required data (from Range A1 to B10 ) is copied to the target range (Range E1 to F10).
Sub sbClearCells()Range("A1:C10").ClearEnd Sub
Clearing Only Data of a Range using ClearContents Method
This method will clear only clear the content or data of the range not formats (Formats remain same)
Sub sbClearCellsOnlyData()Range("A1:C10").ClearContentsEnd Sub
Clearing Entire Worksheet using Clear Method
This method will clear entire worksheet including formats.
Sub sbClearEntireSheet()Sheets("SheetName").Cells.ClearEnd Sub
Clearing Only Data from Worksheet using ClearContents Method
This method will clear only data of worksheet, not formats.
Sub sbClearEntireSheetOnlyData()Sheets("SheetName").Cells.ClearContentsEnd Sub
Instructions:
1. Open an excel workbook2. Enter some data in Sheet1 at A1:C103. Press Alt+F11 to open VBA Editor4. Insert a Module for Insert Menu5. Copy the above code and Paste in the code window6. Save the file as macro enabled workbook7. Press F5 to run it
'In this Example I am changing the Range B4 Font ColorSub sbChangeFontColor()
'Using Cell ObjectCells(4, 2).Font.ColorIndex = 3 ' 3 indicates Red Color
'Using Range ObjectRange("B4").Font.ColorIndex = 3
'--- You can use use RGB, instead of ColorIndex -->'Using Cell ObjectCells(4, 2).Font.Color = RGB(255, 0, 0)
'Using Range ObjectRange("B4").Font.Color = RGB(255, 0, 0)
End Sub
Instructions:
1. Open an excel workbook2. Enter some data in Ranges mentioned above3. Press Alt+F11 to open VBA Editor4. Insert a Module for Insert Menu5. Copy the above code and Paste in the code window6. Save the file as macro enabled workbook7. Press F5 to execute itit8. The following examples will show you how to change the font to bold in Excel using
VBA.9. 'In this Example I am changing the Range B4 Font to Bold10. Sub sbChangeFontToBold()11.12. 'Using Cell Object13. Cells(4, 2).Font.Bold = True14.15. 'Using Range Object
16. Range("B4").Font.Bold = True17.18. End Sub
Following are the examples to show you how to do this practically.
Sub sbChangeCASE() 'Upper Case Range("A3") = UCase(Range("A3")) 'Lower Case Range("A4") = LCase(Range("A4"))End Sub
Instructions:
Follow the below instructions to do it yourself.
1. Open an excel workbook2. Add some text at A3 and A4 ranges3. Press Alt+F11 to open VBA Editor4. Insert a Module for Insert Menu5. Copy the above code and Paste in the code window6. Save the file as macro enabled workbook7. Press F5 to execute it
n this Example below I am changing the Range B3 Background Color using Cell Object
Sub sbRangeFillColorExample1()
'Using Cell ObjectCells(3, 2).Interior.ColorIndex = 5 ' 5 indicates Blue Color
End Sub
Example 2
In this Example below I am changing the Range B3 Background Color using Range Object
Sub sbRangeFillColorExample2()
'Using Range ObjectRange("B3").Interior.ColorIndex = 5
End Sub
Example 3
We can also use RGB color format, instead of ColorIndex. See the following example:
Sub sbRangeFillColorExample3()'Using Cell ObjectCells(3, 2).Interior.Color = RGB(0, 0, 250)
'Using Range ObjectRange("B3").Interior.Color = RGB(0, 0, 250)
End Sub
Example 4
The following example will apply all the colorIndex form 1 to 55 in Activesheet.
Sub sbPrintColorIndexColors()Dim iCntr
For iCntr = 1 To 56 Cells(iCntr, 1).Interior.ColorIndex = iCntr Cells(iCntr, 1) = iCntrNext iCntr
End Sub
Instructions:
1. Open an excel workbook2. Press Alt+F11 to open VBA Editor3. Insert a new module from Insert menu4. Copy the above code and Paste in the code window5. Save the file as macro enabled workbook6. Press F5 to execute the procedure7. You can see the interior colors are changing as per our code
Following is the example to show you how to merge or UnMerge the cells in excel using VBA.
Code to merge Cells
Sub sbMergeRange()
Range("A1:B3").Merge
End Sub
Code to unmerge Cells
Sub sbUnMergeRange()
Range("A1:B3").UnMerge
End Sub
Instructions:
1. Open an excel workbook2. Press Alt+F11 to open VBA Editor3. Insert a Module for Insert Menu4. Copy the above code and Paste in the code window5. Save the file as macro enabled workbook6. Press F5 to execute it
The following code will show you how to add and clear comments using Excel VBA.
Code:
Sub sbAddComment() 'Deletes Existing Comments Range("A3").ClearComments 'Creates Comment Range("A3").AddComment Range("A3").Comment.Text Text:="This is Example Comment Text"
End Sub
Instructions:
1. Open an excel workbook2. Press Alt+F11 to open VBA Editor3. Insert a Module for Insert Menu4. Copy the above code and Paste in the code window5. Save the file as macro enabled workbook6. Press F5 to execute it