Вы находитесь на странице: 1из 7

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 column
End 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 Object
Cells(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 column
End Sub

In this example I am selecting a Range using Select method of Range.


Sub sbSelectARange()
'You can also use Range Object
Range("C3").Select
'Collection of Cells OR Multiple Cells = Range
Range ("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 1
Range("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 2
Range("A1:B10").Copy
Range("E1").Select
ActiveSheet.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 workbook


2. Enter some data in Sheet1 at A1:B10
3. Press Alt+F11 to open VBA Editor
4. Insert a Module for Insert Menu
5. Copy the above code and Paste in the code window
6. Save the file as macro enabled workbook
7. 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").Clear
End 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").ClearContents
End Sub
Clearing Entire Worksheet using Clear Method

This method will clear entire worksheet including formats.


Sub sbClearEntireSheet()
Sheets("SheetName").Cells.Clear
End Sub
Clearing Only Data from Worksheet using ClearContents Method

This method will clear only data of worksheet, not formats.


Sub sbClearEntireSheetOnlyData()
Sheets("SheetName").Cells.ClearContents
End Sub
Instructions:

1. Open an excel workbook


2. Enter some data in Sheet1 at A1:C10
3. Press Alt+F11 to open VBA Editor
4. Insert a Module for Insert Menu
5. Copy the above code and Paste in the code window
6. Save the file as macro enabled workbook
7. Press F5 to run it
'In this Example I am changing the Range B4 Font Color
Sub sbChangeFontColor()
'Using Cell Object
Cells(4, 2).Font.ColorIndex = 3 ' 3 indicates Red Color
'Using Range Object
Range("B4").Font.ColorIndex = 3
'--- You can use use RGB, instead of ColorIndex -->
'Using Cell Object
Cells(4, 2).Font.Color = RGB(255, 0, 0)
'Using Range Object
Range("B4").Font.Color = RGB(255, 0, 0)
End Sub
Instructions:

1. Open an excel workbook

2. Enter some data in Ranges mentioned above


3. Press Alt+F11 to open VBA Editor
4. Insert a Module for Insert Menu
5. Copy the above code and Paste in the code window
6. Save the file as macro enabled workbook
7. Press F5 to execute itit
8. 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 Bold
10. Sub sbChangeFontToBold()
11.
12. 'Using Cell Object
13. Cells(4, 2).Font.Bold = True
14.
15. 'Using Range Object
16. Range("B4").Font.Bold = True
17.
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 workbook
2. Add some text at A3 and A4 ranges
3. Press Alt+F11 to open VBA Editor
4. Insert a Module for Insert Menu
5. Copy the above code and Paste in the code window

6. Save the file as macro enabled workbook


7. Press F5 to execute it
n this Example below I am changing the Range B3 Background Color using Cell Object
Sub sbRangeFillColorExample1()
'Using Cell Object
Cells(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 Object
Range("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 Object
Cells(3, 2).Interior.Color = RGB(0, 0, 250)
'Using Range Object
Range("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) = iCntr
Next iCntr
End Sub
Instructions:

1. Open an excel workbook


2. Press Alt+F11 to open VBA Editor
3. Insert a new module from Insert menu
4. Copy the above code and Paste in the code window
5. Save the file as macro enabled workbook
6. Press F5 to execute the procedure
7. 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 workbook


2. Press Alt+F11 to open VBA Editor
3. Insert a Module for Insert Menu
4. Copy the above code and Paste in the code window
5. Save the file as macro enabled workbook
6. 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 workbook


2. Press Alt+F11 to open VBA Editor
3. Insert a Module for Insert Menu
4. Copy the above code and Paste in the code window
5. Save the file as macro enabled workbook
6. Press F5 to execute it

Вам также может понравиться