You are on page 1of 3

Change cell content:

Range("A1").Value = "Hello"

declare a variable called temp of type Double:


Dim Temp As Double
Temp = Range("A1").Value

Change cell color


Cells.Interior.ColorIndex = 28

Add formula
Range("D4").Formula = "=B3*10"
Range("D4").FormulaR1C1 = "=R3C2*10"
- Explanation: cell D4 references cell B3 (row 3, column 2). This is an absolute
reference ($ symbol in front of the row number and column letter
Range("D4").FormulaR1C1 = "=R[-1]C[-2]*10"
- Explanation: cell D4 references cell B3 (one row above and 2 columns to the
left). This is a relative reference. This code line gives the exact same result as the
code line used at step 1.
Prompt msg box:
MsgBox "this is fun"

Take content at cell A1 and show at msg box:


MsgBox "Entered value is " & Range("A1").Value

Command a new line at msg box:


MsgBox "Line 1" & vbNewLine & "Line 2"

Yes or No msg box


//Declare answer as integer
Dim answer As Integer
//prompt msg box to ask user question
answer = MsgBox("Are you sure want to empty the sheet?", vbYesNo + vbQuestion,
"Empty Sheet")
//vbYesNo is msg box type
//vbQuestion is the symbol
//Empty Sheet is the msg box name

//Action
If answer = vbYes Then
Cells.ClearContents
Else
'do nothing
End If

Input msg box


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.

myValue = InputBox("Give me some input")


Range("A1").Value = myValue
Different object reference
Control different sheet
1. Using the worksheet name.
Worksheets("Sales").Range("A1").Value = "Hello"
2. Using the index number (1 is the first worksheet starting from the left).
Worksheets(1).Range("A1").Value = "Hello"
3. Using the CodeName.
Sheet1.Range("A1").Value = "Hello"

You might also like