Академический Документы
Профессиональный Документы
Культура Документы
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
'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:
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
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:
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:
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: