976 Chapter 40
The macro ActiveCellDemo fi rst defi nes two string variables, one for
the title of the message box and one for the message itself. We set the
message variable in stages, adding bits to the string as we go along. In
the process we use the values of three properties of ActiveCell. The fi rst
property we use is Address, which holds the absolute address of a range
as a string. Obviously this property is read-only and cannot be changed.
1
The second property we use is Formula, which holds the text in the cell
as a string and can be changed. The third property we use is Value, which
holds the value in the cell as a variant and can be changed. We use the
fact that the concatenation operator (&) converts its operands to strings
to successively add values to the variable Msg. Once Msg is all set we
use MsgBox to display it on screen.
The last line in the macro uses the Formula property again, but this
time we change its value and add *2 to the end; this multiplies the last
element in the formula by 2. Thus, if you have a formula =A2+3 in the
active cell, the new formula will be =A2+3
*
2. If you run the macro again
on the same cell, the new formula will be = A2+3
*
2
*
2.
The Macro produces the following results.
Msg = Msg & Chr(13) ‘ Add a carriage Return
‘ The next line starts line two of message
Msg = Msg & “The Formula in the cell is:”
‘ The next line Adds the text of the formula
‘ to the message
Msg = Msg & ActiveCell.Formula
Msg = Msg & Chr(13) ‘ Add a carriage return
‘ The next line starts line three of Message
Msg = Msg & “The value of the cell is:”
Msg = Msg & ActiveCell.Value ‘ Add the value in
‘ the cell
MsgBox Msg, vbInformation, Title ‘ Report to user
‘ The next line adds “* 2” to the text in the
‘ cell
ActiveCell.Formula = ActiveCell.Formula & “*2”
End Sub
1. If A1 is an address, then $A$1 is an absolute address.