+ Reply to Thread
Results 1 to 7 of 7

Issue Converting Sub to Function to End Main Sub after Called Sub Exits

  1. #1
    Registered User
    Join Date
    12-11-2013
    Location
    auckland
    MS-Off Ver
    Excel 2010
    Posts
    56

    Lightbulb Issue Converting Sub to Function to End Main Sub after Called Sub Exits

    Hello,

    I have a main sub that is calling a sub to Import some Data and if it fails or returns a false, i want it to exit the Main Sub. I have read deep into forums and have found that the way to do this is by converting the called sub to a function then calling the value e.g.
    Please Login or Register  to view this content.
    . But I am having difficulties with my Function.

    It is currently running the first part up to the 'else' or so where it opens the file but it will no longer copy the sheet to the workbook where the macro was called from it just leaves it open and will only copy the sheet if i leave the workbook open in the background and run it again. Please see the Function below and advise where this is wrong or if there is a way to achieve what I am trying to do with it as a sub.

    Please Login or Register  to view this content.


    Please let me know if i haven't included enough details.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Issue Converting Sub to Function to End Main Sub after Called Sub Exits

    I have never tried to get a function to write data to a worksheet. Why would the programmers provide such functionality.

    I will write a simple UDF and see it it does what you ask.

    No I tried several tests. They all failed.

    Please Login or Register  to view this content.
    In the Meantime

    I would have approached this differrently

    Please Login or Register  to view this content.
    Last edited by mehmetcik; 08-20-2017 at 06:59 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    12-11-2013
    Location
    auckland
    MS-Off Ver
    Excel 2010
    Posts
    56

    Re: Issue Converting Sub to Function to End Main Sub after Called Sub Exits

    Hi Mehmetcik,

    Thank you for your help here. Your different approach has worked on the stopping the Sub if the Specific named sheet isn't found on the opened file (Which is great! one bug down). However i'm still having the issue where once it calls the Import_Data sub, if you select an excel file, it will open the file but wont copy the sheet over. Originally i got it to work all at the same time but now it just opens but doesn't copy unless i run it a second time.... am I missing a line of code?

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Issue Converting Sub to Function to End Main Sub after Called Sub Exits

    Could your error be here?

    Please Login or Register  to view this content.
    As I see it you are using the split function to get the file name.

    UBound tells you how many entries you have in your array, however the numbering often starts with 0 and ens in Ubound()-1

    Please Login or Register  to view this content.
    Put a breakpoint at that point then select view Local Window
    Select vfilename and check your array numbering.

  5. #5
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: Issue Converting Sub to Function to End Main Sub after Called Sub Exits

    morangeman, the code ran fine with the test spreadsheets I created (basically a macro spreadsheet with a 'Welcome' sheet and a dummy workbook with 'Raw Data' sheet). If anything, perhaps you need to consider defining a workbook in the SheetExists function.

    E.g.
    Please Login or Register  to view this content.
    EDIT: also, the main Import_Data function should be defined to True when the copy and paste is successful.

    Please Login or Register  to view this content.
    Last edited by quekbc; 08-20-2017 at 07:26 PM.

  6. #6
    Registered User
    Join Date
    12-11-2013
    Location
    auckland
    MS-Off Ver
    Excel 2010
    Posts
    56

    Re: Issue Converting Sub to Function to End Main Sub after Called Sub Exits

    Hi All,

    Thanks for all of your updates. I have managed to replicate the circumstances when it works and when it doesn't. If I click 'Developer', 'Macros', Then select the macro and click 'Run' it will work and pull the data first time and close the book and continue the macro or end it if the sheet doesnt exist. However if I use the keyboard shortcut e.g. Ctrl + Shift + 'G' it wont run the full macro instead it will just open the workbook and leave it sitting there..... Thoughts?

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Issue Converting Sub to Function to End Main Sub after Called Sub Exits

    Hi,

    This is a bug in Excel where if you include Shift in the shortcut key combination, it will stop at a workbooks.open call. See this article for an explanation and solution- http://www.jkp-ads.com/Articles/WorkbookOpenBug.asp
    Don
    Please remember to mark your thread 'Solved' when appropriate.

+ 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. Save program and back to main menu issue ?
    By Rodger Colmar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-25-2017, 02:35 PM
  2. [SOLVED] Exit Sub Procedure when called on Sub Procedure Exits
    By onmyway in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-29-2015, 08:17 AM
  3. Replies: 0
    Last Post: 02-27-2015, 06:40 AM
  4. Code exits Function at a point where it shouldn't be possible to exit
    By 6StringJazzer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-22-2014, 03:39 AM
  5. Macro exits prematurely from function after calling other function
    By LouisPhilippe in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-22-2013, 03:22 AM
  6. to exit main procedure from procedure called by yhe main
    By kmlprtsngh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-27-2010, 02:46 PM
  7. Replies: 3
    Last Post: 01-27-2010, 02:28 PM

Tags for this Thread

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