Excel vba datatypes

I am little confused here.
Is 'worksheet' and 'range' a datatype?
I have an example code that I am working on.
Apparently there are variables declared witgh this datatype

Can any one please explain a little on this to help me understand it

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Ingeborg Hawighorst (Microsoft MVP / EE MVE)Connect With a Mentor Microsoft MVP ExcelCommented:

worksheet and range are objects. You can declare variables for these objects and then assign values to them.

Dim ws as worksheet
Dim rng as Range

Then you can assign values to these variables.

Set ws = Worksheets("Sheet1")
Set rng = ws.Range("A1")

After that, you can use the rng variable to manipulate the range object's content

rng = "hello world"

The full code:

Sub test()
Dim ws As Worksheet
Dim rng As Range

Set ws = Worksheets("Sheet1")
Set rng = ws.Range("A1")

rng = "hello world"
rng.Font.ColorIndex = 4

End Sub

Open in new window

If you want to change what range the rng variable points to, you need to use the Set keyword. If you want to manipulate the properties of the range that the rng variable points to, e.g. content, formatting, etc., you use the rng variable like any other variable or like the object that the variable points to. So, instead of

Worksheets("Sheet1").Range("A1").Font.ColorIndex = 4

you can use

rng.Font.ColorIndex = 4

cheers, teylyn
SameerMirzaAuthor Commented:
All Courses

From novice to tech pro — start learning today.