VBA - Create Range

In probably every VBA code you create, you will need to create at least 1 range for your code to reference. This is just a quick example of how to create a range.
We will call the range "rng" - we first need to Dim rng as Range and then Set rng's value.
Here is an example (you can paste this into your own VBA - just insert random values into cells A1:E1)

Sub set_range()
    Dim rng As Range
    Dim cell As Range
    Set rng = Range("A1:E1")
    For Each cell In rng
        If cell.Value = 0 Then
            MsgBox (cell.Address & " = 0!")
        End If
    Next cell
End Sub

I have a "0" in cell B1 - your result will then be the following, showing that cell B1 = 0


No comments:

Post a Comment

Open Your Business to the World! $7.99 .COMS at Go Daddy