+ Reply to Thread
Results 1 to 6 of 6

Several Macros run from VBsccript, 1 throws compile error " Excpected End With"

  1. #1
    Registered User
    Join Date
    01-10-2018
    Location
    Oslo, Norway
    MS-Off Ver
    2016
    Posts
    3

    Several Macros run from VBsccript, 1 throws compile error " Excpected End With"

    I am not well versed in either vbscript or VBA, but I must admit I am a little bit annoyed by the macro recorder... Ok, enough about that.

    I have quite a few Excel charts that are created in a fairly similar manner. The macros all run, except 1 of the, which insists the is missing an "End With" statement.
    Please Login or Register  to view this content.
    There is no difference between this and the code for the other macros that I can see, but of course my language of choice is C++, so this doesn't quite compute in my brain....
    Anybody ?

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Re: Several Macros run from VBsccript, 1 throws compile error " Excpected End With"

    Good afternoon PushT

    ... and welcome to the forum!!

    Don't be too hard on the macro recorder - it's a great aid for learning, and without it you wouldn't have got this far.

    Each "With" statement requries an "End With", and all but the first two "Withs" - two consecutive lines early on beginning "With ActiveChart." - have them.

    You should be able to delete the "With" part of these two lines, just leaving the "ActiveChart" part.

    HTH

    DominicB

  3. #3
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Several Macros run from VBsccript, 1 throws compile error " Excpected End With"

    As the error states, you have some with statements without the closing end with statement.

    Correct:
    Please Login or Register  to view this content.
    Incorrect:
    Please Login or Register  to view this content.
    The recorder will do for the most simple of tasks. Its mostly useful for learning. It however generates terrible code. In has a habit of including alot of things not required (like .select and .activate) and is purely static (the exact range you did something to).

    You can for the time correct your issue by closing all the open with statements (those without a matching end with).
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  4. #4
    Registered User
    Join Date
    01-10-2018
    Location
    Oslo, Norway
    MS-Off Ver
    2016
    Posts
    3

    Re: Several Macros run from VBsccript, 1 throws compile error " Excpected End With"

    Tanks so much, both of you !! I am very new to this, so I promise to use the recorder only for learning ;-)
    It is grear functionality, so I do appreciate it :-)
    And I greatly appreciate that level of fast response :-)
    Last edited by PushT; 01-10-2018 at 12:43 PM.

  5. #5
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Several Macros run from VBsccript, 1 throws compile error " Excpected End With"

    Just to further elaborate on the recording of code. To my point regarding it recording things not needed:

    Please Login or Register  to view this content.
    You could almost 99.9% sure remove all of the lines like this above from your code. Essentially they recorder records EVERYTHING you do (that VBA has access to), including things like scrolling, clicking a cell (not required 99% to act upon it), etc.

    I wouldnt be surprised if the code you have could be re-written to be ~80% less code while doing the same thing, while possibly being more flexible (like finding the needed range instead of static ranges).

    VBA can do wonderful things, but it can also cause alot of issues if used incorrectly. As a programmer I often ask myself "Should I do this?" not "Can I do this?". If there is a feature or formula that can accomplish your goals its generally a better approach. VBA shines when: 1) the process needs to be repeated a tedious number of times or 2) you cannot accomplish the same result with built in formula/features.

    It looks like your macro does mostly formatting. Alot of people use macros to do that type of a thing without realizing features built in like styles, table styles, chart templates, etc. can accomplish the same thing. It may be that you are aiming to meet my point #1 about repetition with the macro and if your macro serves you well then keep using it. However as people get more comfortable with recording code and even writing code they tend to use it more and more even when they do not need to. Just be careful not to fall into the habit of trying to use code to do everything.

  6. #6
    Registered User
    Join Date
    01-10-2018
    Location
    Oslo, Norway
    MS-Off Ver
    2016
    Posts
    3

    Re: Several Macros run from VBsccript, 1 throws compile error " Excpected End With"

    Quote Originally Posted by Zer0Cool View Post
    Just to further elaborate on the recording of code. To my point regarding it recording things not needed:

    Please Login or Register  to view this content.
    You could almost 99.9% sure remove all of the lines like this above from your code. Essentially they recorder records EVERYTHING you do (that VBA has access to), including things like scrolling, clicking a cell (not required 99% to act upon it), etc.

    I wouldnt be surprised if the code you have could be re-written to be ~80% less code while doing the same thing, while possibly being more flexible (like finding the needed range instead of static ranges).

    VBA can do wonderful things, but it can also cause alot of issues if used incorrectly. As a programmer I often ask myself "Should I do this?" not "Can I do this?". If there is a feature or formula that can accomplish your goals its generally a better approach. VBA shines when: 1) the process needs to be repeated a tedious number of times or 2) you cannot accomplish the same result with built in formula/features.

    It looks like your macro does mostly formatting. Alot of people use macros to do that type of a thing without realizing features built in like styles, table styles, chart templates, etc. can accomplish the same thing. It may be that you are aiming to meet my point #1 about repetition with the macro and if your macro serves you well then keep using it. However as people get more comfortable with recording code and even writing code they tend to use it more and more even when they do not need to. Just be careful not to fall into the habit of trying to use code to do everything.
    That's valuable advice there, and I can absolutely relate to it. I will keep it in mind, sir.
    Your comment about repetition is spot on, as there are A LOT of data that will reuse the same set of macros to continuously produce charts with the same "template". Creating the charts by hand is fun the first time I do them, but interactive chart creation is not an option for me at all.

+ 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] Excel 2010 -- "Visual Basic" "Macros" and "Record Macro" all disabled.
    By NicholasL in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-07-2017, 06:11 AM
  2. [SOLVED] Choice between "Compile Error" and "Error 91 - Object Variable Not Defined"
    By JoeVanGeaux in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-09-2017, 02:56 PM
  3. [SOLVED] Insert Row throws error "Insert Method of Range class failed"
    By bg_enigma1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-09-2015, 11:30 AM
  4. [SOLVED] Error Msg: "Compile error: Method or data member not found" in VBA code
    By nenadmail in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-11-2012, 02:12 AM
  5. [SOLVED] emptyRow code giving Compile Error "Variable not defined"
    By colvinb in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-21-2012, 04:34 AM
  6. VBA - Formula generating "Compile error/ Syntax Error"
    By arvin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-03-2009, 03:28 PM
  7. Replies: 4
    Last Post: 01-21-2005, 08:06 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