Vbcopy: Range .Activate
Vbcopy: Range .Activate
For example,
the following procedure makes B5 the active cell and then formats it as bold.
VBCopy
Sub SetActive_MakeBold()
Worksheets("Sheet1").Activate
Worksheets("Sheet1").Range("B5").Activate
ActiveCell.Font.Bold = True
End Sub
You can use the Offset property to move the active cell. The following procedure inserts text
into the active cell in the selected range and then moves the active cell one cell to the right
without changing the selection.
VBCopy
Sub MoveActive()
Worksheets("Sheet1").Activate
Range("A1:D10").Select
ActiveCell.Value = "Monthly Totals"
ActiveCell.Offset(0, 1).Activate
End Sub
ThisWorkbook.Activate
MsgBox selectedCell.Value
You can make a cell active either by using the activate method or by using the select
method on the range you want to activate
ThisWorkbook.Activate
Worksheets("Sheet1").Range("B4").Activate
which is equivalent to
ThisWorkbook.Activate
Worksheets("Sheet1").Range("B4").Select
If you want to select the last cell that has data in this active column, you can use the code
snippet below.
ThisWorkbook.Activate
Application.activecell.End(XlDirection.xlDown).Select
Sub DoWhileDemo()
Do While ActiveCell.value <> Empty
ActiveCell.value = ActiveCell.value * 2
ActiveCell.offset(1, 0).Select
Loop
End Sub
Repeating Actions with a Loop
Sub FormatAllCellsInColumn()
Do Until ActiveCell.Value = ""
ActiveCell.Rows.EntireRow.Select
Selection.Interior.ColorIndex = 35
Selection.Interior.Pattern = xlSolid
ActiveCell.Offset(2, 0).Select
Loop
End Sub