+ Reply to Thread
Results 1 to 6 of 6

Problem using 'Set' to set variable

  1. #1
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    548

    Problem using 'Set' to set variable

    This has me baffled. The following code works fine. But I've been trying to get into the habit of using Set when setting a variable.

    Please Login or Register  to view this content.
    So I changed the bold line accordingly, to:

    Please Login or Register  to view this content.
    But to my surprise it then failed, highlighting 'dte =' with the error "Compile error: Object required".
    Terry, East Grinstead, UK
    Excel 365, Win 10 Pro, i7 PC, 4.0 GHz

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Problem using 'Set' to set variable

    You can't set a variable that isn't an object.

    Please Login or Register  to view this content.
    Is a value and not object and should not be set.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    548

    Re: Problem using 'Set' to set variable

    Thanks @CK76. So as a novice I guess my safest approach is to never use Set which is what I see in most code?

    Also, what do you call the command "dte = .....", if not 'setting dte'?

    Must admit I'm confused that you can't set a value? I thought that was the common use?
    Last edited by terrypin; 01-06-2020 at 01:10 PM.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,525

    Re: Problem using 'Set' to set variable

    Also, what do you call the command "dte = .....", if not 'setting dte'?
    With normal "value" (not object) variables, it is called a "Let" statement: https://docs.microsoft.com/en-us/off.../let-statement Note that, except for some of the earliest iterations of the BASIC, the Let keyword has long been optional, and most programmers omit the Let keyword. So, "Let a=2" and "a=2" are equivalent. As has already been noted, the Set statement is required and is used when assigning objects to an appropriate object variable.

    Even though explicit use of the Let keyword is rare, if it helps you as the programmer keep track of when you are working with normal variable types and object variable types, I see no reason why you could not also include the Let keyword in your programming.
    Please Login or Register  to view this content.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Problem using 'Set' to set variable

    So as a novice I guess my safest approach is to never use Set which is what I see in most code?
    Not quite. Objects must be Set without exception, and as MrShorty noted, you can Let non-object variables (values).

    It's bit hard to explain what/when to use Set, but you can find lists and explanation in below link. Worksheet, Range, Shapes etc are objects and need to be Set.
    https://docs.microsoft.com/en-us/off...l/object-model

    EDIT: You may find below article easier to follow.
    https://excelmacromastery.com/vba-objects/

  6. #6
    Forum Contributor terrypin's Avatar
    Join Date
    01-06-2010
    Location
    East Grinstead, UK
    MS-Off Ver
    MS Office 365
    Posts
    548

    Re: Problem using 'Set' to set variable

    Thanks both, understood in general, although I clearly need to study further as suggested. Not exactly intuitive - but then I suppose that’s true of any programming language! Well, maybe since FORTRAN and BASIC?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] object variable or with block variable not set, Problem
    By nordicdust in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-17-2019, 10:03 AM
  2. Object variable or with block variable not set problem
    By Wilftiger in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-25-2019, 06:56 AM
  3. Vlookup problem in a loop with cell property and variable cell problem (long title sry)
    By ExcelsiorLux in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-13-2013, 10:38 AM
  4. variable problem
    By skaswani in forum Excel General
    Replies: 1
    Last Post: 07-06-2007, 07:19 AM
  5. [SOLVED] Variable Problem.
    By Tim in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-23-2006, 12:15 PM
  6. Variable problem
    By Rob Hargreaves in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-10-2005, 05:07 PM
  7. variable/Input box problem
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-13-2005, 09:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1