+ Reply to Thread
Results 1 to 3 of 3

Variable Declaration issue

  1. #1
    Registered User
    Join Date
    05-10-2012
    Location
    Pa
    MS-Off Ver
    Excel 2010
    Posts
    5

    Variable Declaration issue

    Hi All,

    I'm new to the forum and to code. I've been struggling a variant of the code below. When I try to declare a variable like “cnt” and equate it to “Range(ActiveCell, ActiveCell.End(xlToRight)).Columns.Count” in the code below I get an error- 1004. I've listed both the code that works and does not work below. Can someone explain why the error is occurring. Thanks so much in advance.

    Code that works
    Please Login or Register  to view this content.
    [b][b/]
    Code that does not work
    Please Login or Register  to view this content.
    Regards, Rich
    Last edited by Leith Ross; 05-10-2012 at 02:02 PM. Reason: Added Code Tags

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Variable Declaration issue

    For a first post, you did a great job in explaining exactly what the problem was and posed a clear question. Nice work.

    Variant is a catch all requiring the most memory and therefore, the least efficient. It has it's uses, but generally speaking, you should try to declare your variables as specific to their use as possible using the applicable object.

    In this case, the result of a columns.count would be a Long variable.

    What happens when you switch Variant to Long in the 'Code that does not work'?

    I would also suggest the following in furthering your education on the subject:

    http://www.ozgrid.com/VBA/variables.htm
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Registered User
    Join Date
    05-10-2012
    Location
    Pa
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Variable Declaration issue

    Thank you so much. That works perfectly!! I'll be sure to review the link you suggested.

    Rich

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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