+ Reply to Thread
Results 1 to 18 of 18

unable to load & save a workbook from a VBA function

Hybrid View

  1. #1
    Registered User
    Join Date
    06-23-2013
    Location
    Elmer, New Jersey, YSA
    MS-Off Ver
    Excel 2013
    Posts
    9

    unable to load & save a workbook from a VBA function

    Here's what I have so far

    Option Explicit
    
    Function LRS(ByVal instring As String)
    
     ' where instring is the name of an xlsx workbook
     
     Dim result As Integer
      
     Dim rootPath As String
        rootPath = ThisWorkbook.Path
        rootPath = rootPath & "\"
        'MsgBox "root path " & rootPath
    
      
     Dim Filename As Variant
     Filename = rootPath & "\" & instring & ".xlsx"
     If instring <> "" Then
        result = LRS_do_one(Filename)
     End If
      
     End Function
    Function LRS_do_one(Filename)
      
     MsgBox "Doing " & Filename     ' gives popup w expected results
     
     Workbooks(Filename).Open       ' doesn't to anything
     Workbooks(Filename).Activate   ' doesn't to anything
     Workbooks(Filename).RefreshAll ' doesn't to anything
     Workbooks(Filename).Save       ' doesn't to anything
     Workbooks(Filename).Close      ' doesn't to anything
      
    End Function
    Last edited by arlu1201; 06-24-2013 at 11:00 AM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: unable to load & save a workbook from a VBA function

    Function LRS(ByVal instring As String)
     ' where instring is the name of an xlsx workbook
      Dim result As Integer, rootPath As String
      rootPath = ThisWorkbook.Path 
      Dim Filename As String
     Fname = rootPath & "\" & instring & ".xlsx"
     If instring <> "" Then
        Workbooks.Open Filename:=Fname
     end if
    End Function
    please check commands sintax on Help, don't create personal commands
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    06-23-2013
    Location
    Elmer, New Jersey, YSA
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: unable to load & save a workbook from a VBA function

    still doesn't work .... here's what I have now

    Option Explicit
    
    Function LRS(ByVal instring As String)
    
     ' where instring is the name of an xlsx workbook
     
     Dim result As Integer, Fname As String, Filename As String
      
     Dim rootPath As String
        rootPath = ThisWorkbook.Path
        rootPath = rootPath & "\"
        'MsgBox "root path " & rootPath
      
     Fname = rootPath & "\" & instring & ".xlsx"
     If instring <> "" Then
        MsgBox "Doing " & Fname
        Workbooks.Open Filename:=Fname
        'Workbooks.Activate Filename:=Fname
        'Workbooks.RefreshAll Filename:=Fname
        'Workbooks.Save Filename:=Fname
        Workbooks(Filename).Close
         
     End If
      
     End Function
    thanks
    Last edited by arlu1201; 06-24-2013 at 11:00 AM.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Where are you trying to use the function?

    If it's on a worksheet then it's not going to work.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    06-23-2013
    Location
    Elmer, New Jersey, YSA
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: unable to load & save a workbook from a VBA function

    what do I have to do to use this function in a worksheet in one workbook,
    to operate on another workbook ?

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

    Re: unable to load & save a workbook from a VBA function

    Can you just clarify exactly where you are trying to use the function?

    Are you trying to use it in a cell?

  7. #7
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: unable to load & save a workbook from a VBA function

    why do you use a function and not a sub ? try this
    sub LRS()
    Dim result As Integer, Fname As String, Fame As String, instring as string
    Dim rootPath As String
    rootPath = ThisWorkbook.Path
    instring = "yourfilename" ' <<<<<<<<<< change it
    Fname = rootPath & "\" & instring & ".xlsx"
    If instring <> "" Then
    MsgBox "Doing " & Fname
    Workbooks.Open Filename:=Fname
    'Workbooks.Activate Filename:=Fname
    'Workbooks.RefreshAll Filename:=Fname
    'Workbooks.Save Filename:=Fname
    'Workbooks(Filename).Close
    
    End sub
    Last edited by patel45; 06-23-2013 at 10:17 AM.

  8. #8
    Registered User
    Join Date
    06-23-2013
    Location
    Elmer, New Jersey, YSA
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: unable to load & save a workbook from a VBA function

    no joy ... here's what I have

    Option Explicit
    
    Sub LRS(ByVal instring As String)
    Dim result As Integer, Fname As String, Fame As String
    Dim rootPath As String
    rootPath = ThisWorkbook.Path
    'instring = "Funds.Fidelity"  ' <<<<<<<<<< change it
    Fname = rootPath & "\" & instring & ".xlsx"
    If instring <> "" Then
    MsgBox "Doing " & Fname                     ' looks ok
    Workbooks.Open Filename:=Fname              ' doesn't do anything
    'Workbooks.Activate Filename:=Fname
    'Workbooks.RefreshAll Filename:=Fname
    'Workbooks.Save Filename:=Fname
    'Workbooks(Filename).Close
    End If
    End Sub
    Last edited by arlu1201; 06-24-2013 at 11:00 AM.

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

    Re: unable to load & save a workbook from a VBA function

    Paul

    Are you going to reveal where you are trying to use this and how it isn't working?

    If you are trying to use it in cell on a worksheet it just isn't going to happen.

    PS Please use code tags when posting code, kind of a rule.

  10. #10
    Registered User
    Join Date
    06-23-2013
    Location
    Elmer, New Jersey, YSA
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: unable to load & save a workbook from a VBA function

    in cell H23 Funds.Fidelity
    in cell J23 =LRS(H23)

    in cell H24 Funds.Franklin
    in cell J24 =LRS(H24)

    H columns are either blank or the name of a workbook in the same folder

    does this answer your question ?
    thanks

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

    Re: unable to load & save a workbook from a VBA function

    You can't use a user defined function on a worksheet to do what you want.

  12. #12
    Registered User
    Join Date
    06-23-2013
    Location
    Elmer, New Jersey, YSA
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: unable to load & save a workbook from a VBA function

    ok ... sigh .
    then how do I do what I want to do ?

  13. #13
    Registered User
    Join Date
    06-23-2013
    Location
    Elmer, New Jersey, YSA
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: unable to load & save a workbook from a VBA function

    is there a way for me to load, save selected workbooks in a folder ?
    thanks

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Yes, but not with a UDF.

  15. #15
    Registered User
    Join Date
    06-23-2013
    Location
    Elmer, New Jersey, YSA
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: unable to load & save a workbook from a VBA function

    if not with a UDF, then what can I use ?

  16. #16
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: unable to load & save a workbook from a VBA function

    you can use a sub, as I showed you

  17. #17
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: unable to load & save a workbook from a VBA function

    Paul,

    Welcome to the forum.

    I have added code tags to all your posts. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. In order to put code tags, either type [CODE] before your code and [/CODE] at the end of it, OR you can highlight your code and click the # icon at the top of your post window.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  18. #18
    Registered User
    Join Date
    06-23-2013
    Location
    Elmer, New Jersey, YSA
    MS-Off Ver
    Excel 2013
    Posts
    9

    Re: unable to load & save a workbook from a VBA function

    ok ... now using a button to invoke LRS_List() ... making progress, but still doesn't work
    automatic calculations a load time never complete .... see line by line comments
    Option Explicit
    Sub LRS_List()
     
    Dim rootPath As String
    rootPath = ThisWorkbook.Path
    'MsgBox "Doing " & rootPath
    
    Dim Fname As String
    Dim file As String
    file = "Funds.Fidelity"
    Fname = rootPath & "\" & file & ".xlsx"
    LRS (Fname)
    
    End Sub
    Sub LRS(Fname)
    Dim res As Integer
    MsgBox "Doing " & Fname             ' looks ok !
    Workbooks.Open Filename:=Fname      ' loads it OK !
    'Application.CalculateFullRebuild   ' no effect
    DoEvents                             ' automatic calculations never complete  
    ActiveWorkbook.RefreshAll           ' WEB queries, seems to execute, status bar
    DoEvents
     'ActiveWorkbook.Save                'Saves it (commented out for debugging)
    ActiveWindow.Close                  'Closes it, prompts to save
    End Sub
    Last edited by Paul VanLeer; 06-24-2013 at 11:46 AM. Reason: add code tags

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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