+ Reply to Thread
Results 1 to 13 of 13

This simple (?) code crashes

  1. #1
    Registered User
    Join Date
    09-22-2014
    Location
    California
    MS-Off Ver
    2007
    Posts
    5

    This simple (?) code crashes

    I am very much a beginner at coding in VBA. However I believe the follow code is fairly straightforward but it always crashes in
    Sub Cleanup() at the line "InTextRange.Select"
    with "Run time error 1004"

    Please Login or Register  to view this content.
    Any help much appreciated.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: This simple (?) code crashes

    Not sure why you've separated the code out like you have, 2 subs, global declarations etc.

    Anyway, try this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: This simple (?) code crashes

    Hi

    Works OK for me.

    Is the INPUT-sRGB sheet hidden by any chance?
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: This simple (?) code crashes

    Richard

    How did you run the code?

    Which sub to you execute first?

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: This simple (?) code crashes

    Nice job Norie.

    blumesan,

    Your code deletes cells and moves other cells to take the place of the deleted cells.
    If that is NOT what you want, you may want:
    Please Login or Register  to view this content.
    Lewis

    BTW, the poster's code worked for me in Excel 2003. Ran 'Define_Variables(), then ran Cleanup().
    Last edited by LJMetzger; 09-22-2014 at 05:37 PM.

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: This simple (?) code crashes

    Hi Norie,

    I ran the Define_Variables first, then the Cleanup.

    It only tripped up with the error mentioned when I hid the sheet in question.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: This simple (?) code crashes

    I got this to work.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-22-2014
    Location
    California
    MS-Off Ver
    2007
    Posts
    5

    Re: This simple (?) code crashes

    You guys are terrific. So much help in only a couple of hours.

    First, Norie: The code is written this way because there are (or will be) many other subroutines which use the global variables. Your code using With statement in the "Define_Variables()" is cleaner. Also it works without crashing BUT trying both IntextRange.Delete or IntextRange.ClearContents has no effect on the spreadsheet.

    Other questions: the Sub Define_Variables is always run first. No sheets are hidden.

    If I may impose on your kindness, let me explain a bit more about the project:

    It begins by importing a text file into the spreadsheet; then goes on to make several calculations. When this finishes and results are saved, I wish to repeat the process with another text file, BUT: A connection has already been established and this connection must be terminated before I can import another text file into the same space. I have managed to accomplish this with the following macro, but it won't work when I try to use the macro code in a VBA module.

    Please Login or Register  to view this content.
    So again, thanks and any additional help greatly appreciated.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: This simple (?) code crashes

    How are you importing the data?

    What calculations are you performing on the data?

    How are you saving the results?

  10. #10
    Registered User
    Join Date
    09-22-2014
    Location
    California
    MS-Off Ver
    2007
    Posts
    5

    Re: This simple (?) code crashes

    Quote Originally Posted by Norie View Post
    How are you importing the data?

    What calculations are you performing on the data?

    How are you saving the results?
    Importing is done via the Data > Import from Text with code snippet:

    Please Login or Register  to view this content.

    Many calculation which, at this point, are not relevant.

    Saving is done by selecting data in worksheet and Save As PDF file.

    I should have mentioned earlier, I am using Microsoft Office Excel 2007

  11. #11
    Registered User
    Join Date
    09-22-2014
    Location
    California
    MS-Off Ver
    2007
    Posts
    5

    Re: This simple (?) code crashes

    To all who responded, many thanks.
    It appears that your interest has now petered out, so I take the liberty of bumping this thread.

    Please understand that I am not here just to find a solution to this particular problem; I also wish to learn something. Twenty years ago I was a pretty good C++ coder, although I never did it for a living. Time and age (82 yrs) have much dimmed my knowledge; but given what I do remember, VBA by comparison is a very opaque language. Why, for example, does Norie's first revision of my code succeed in preventing an error? And why does it not have the desired effect?

  12. #12
    Registered User
    Join Date
    09-22-2014
    Location
    California
    MS-Off Ver
    2007
    Posts
    5

    Re: This simple (?) code crashes

    To all who responded, many thanks.
    It appears that your interest has now petered out, so I take the liberty of bumping this thread.

    Please understand that I am not here just to find a solution to this particular problem; I also wish to learn something. Twenty years ago I was a pretty good C++ coder, although I never did it for a living. Time and age (82 yrs) have much dimmed my knowledge; but given what I do remember, VBA by comparison is a very opaque language. Why, for example, does Norie's first revision of my code succeed in preventing an error? And why does it not have the desired effect?

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: This simple (?) code crashes

    I don't think it's our interest that's petered out, more like the forum is petering out, for us all.

    As for why the code isn't working, it's kind of hard to tell without some sample files to work with.

    One thought I just had was that instead of using QueryTables you could use OpenTextFile.

+ 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. Simple Macro crashes Excel
    By ibsteveog in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-28-2010, 06:48 PM
  2. Simple autofit macro crashes.
    By peter233 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-12-2006, 08:00 AM
  3. [SOLVED] Excel crashes on simple procedure
    By Daves_Solutions in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-14-2006, 01:10 AM
  4. [SOLVED] inserting code into code module crashes
    By Kris in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-25-2006, 01:30 PM
  5. Simple recorded macro crashes on playback. Why?
    By Paul in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-09-2005, 04:10 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