ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Set or not to Set (https://www.excelbanter.com/excel-programming/278600-re-set-not-set.html)

Chip Pearson

Set or not to Set
 
Mike,

You use Set when you are setting one object type variable to another object.
The simple variables, those that are not objects, include Integer, Long,
String, Boolean, etc, and you don't use Set when assigning a value to the
variable. You use Set with variables declared As Object, or with specific
object type variables, such as Workbook, Worksheet, Range, etc.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com



"Mike" wrote in message
...
Hello,

In setting variables, I am mystified as to why certain
variables need to be "set" while other do not.

For example, in the following macro, the variable n is
shown as n = "A" but the variable ws has to be shown as
Set ws = Worksheets("Sheet1"). If I don't use Set, I get
a "Run-time error '91' - Object Variable or With block
variable not set".

The Help file states that variables can be declared as
one of the following data types: Boolean, Byte, Integer,
Long etc. "Worksheet" is not one of the data types - is
this the reason that I have to use Set (????).

The macro (the full macro works):

Option Explicit
Sub DeleteRows()
Dim n As String
Dim ws As Worksheet

n = "A"
Set ws = Worksheets("Sheet1")

Set Rng = ws.Range(n & "1", Range(n & "65536").End(xlUp))

*** rest of macro *****

End Sub

TIA

Mike





All times are GMT +1. The time now is 08:56 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com