+ Reply to Thread
Results 1 to 6 of 6

Compile error. Expected #.

  1. #1
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Compile error. Expected #.

    This code works that I recorded.

    Please Login or Register  to view this content.
    This code does NOT work. I get Compile Error: Expected #. WHY?

    Please Login or Register  to view this content.
    Ignore the redundancy in the code, doesn't matter what columns are used. I was just troubleshooting trying to figure out why the error.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Compile error. Expected #.

    Because Width is a designated name in VBA. For instance change sub name to cWidth and you're good to go.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Compile error. Expected #.

    You mean the Subs name? So Sub cWidth?

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,855

    Re: Compile error. Expected #.

    Sub myWidth() or Sub cWidth() instead of Sub Width().

    While VBA does not explicitly disallow use of designated/reserved words in sub names. It will create ambiguity and will often cause unexpected errors.
    Best to avoid them. There isn't comprehensive list that I'm aware of. But best to avoid names/words found in links below.
    https://docs.microsoft.com/en-us/pre...7h19t(v=vs.90)
    https://msdn.microsoft.com/en-us/lib...ffice.14).aspx

    Also, never name Sub, with alphanumeric string, that can stand for column/cell reference. (ex: Sub AB123())
    This will also cause ambiguity in some circumstances.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  5. #5
    Forum Contributor
    Join Date
    01-09-2016
    Location
    USA,USA
    MS-Off Ver
    2016
    Posts
    1,192

    Re: Compile error. Expected #.

    Gotcha. Thanks!

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Compile error. Expected #.

    Glad to help and thanks for rep+.

+ 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. Compile Error - Expected End Sub
    By chrs17sim in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-20-2018, 07:58 AM
  2. [SOLVED] compile error expected sub
    By citypro in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-04-2015, 11:06 AM
  3. Compile Error: Expected End Sub
    By Dcauth in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-31-2011, 01:56 PM
  4. Compile error: Expected End Sub
    By Leeboy in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-31-2011, 09:44 AM
  5. Compile Error: Expected End Sub
    By Lothar69 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-27-2011, 10:55 AM
  6. Compile Error: Expected End Sub
    By marianmix in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-28-2011, 05:31 AM
  7. compile error: Expected: = ...Why?
    By cesw in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-09-2005, 08:05 PM

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