+ Reply to Thread
Results 1 to 4 of 4

Method not available

Hybrid View

  1. #1
    Registered User
    Join Date
    03-08-2013
    Location
    Louisiana
    MS-Off Ver
    Excel 2010
    Posts
    25

    Method not available

    Hello everyone!

    Im trying to really develop some skills in VB so I may have some follow up questions as to how a bit of code works.

    Right now I'm working on a project to open all workbooks in a folder (the folder will change on a daily basis and the amount of workbooks in each folder will vary too). Eventually I'll have it make certain changes in each workbook.

    But for right now I have

    Sub combine()
     
    
     
     Dim directory As String
     Dim foldername As String
     Dim fldrobj As FileSystemObject
     Dim fileobj As FileSystemObject
     
     
    
     
    
     foldername = Format(DateTime.Now, "yyyymmdd")
    directory = "C:\combiner\"
    Set fldrobj = FileSystemObject.GetFolder(directory & foldername)
    
    For Each fileobj In fldrobj.Files
    
    
    Application.Workbooks.Open (fileobj)
    
    
     Next
        
     
     
    End Sub
    One issue I have is that the ".Files" method in the For each Loop is not available. I see other similar Subs out there that utilize this method but for some reason I dont have it. Is there some other type of fileobject that will allow me to run this for each loop?

    I also have the microsoft striplet library imported

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

    Re: Method not available

    Hi vincenzo345,

    You were missing a call that created the 'File System Object'. I modified your code slightly to use 'Late Binding' (not important what 'Late Binding is or does) so that you do not need the library reference:


    Try:
    Sub combine()
     
      Dim directory As String
      Dim foldername As String
      Dim oFs As Object
      Dim fldrobj As Object
      Dim fileobj As Object
     
      'Create the File System Object
      Set oFs = CreateObject("Scripting.FileSystemObject")
    
     
    
      foldername = Format(DateTime.Now, "yyyymmdd")
      directory = "C:\combiner\"
      Set fldrobj = oFs.GetFolder(ThisWorkbook.Path)
    
      For Each fileobj In fldrobj.Files
    
        Debug.Print fileobj.Name    'Sends output to the Immediate Window (CTRL G) in the debugger
        Application.Workbooks.Open (fileobj)
    
      Next
        
     'Clear object pointers
     Set oFs = Nothing
     Set fldrobj = Nothing
     Set fileobj = Nothing
     
    End Sub
    For more information about 'Early Binding' and 'Late Binding' (only if you want to make your head spin) see: http://peltiertech.com/Excel/EarlyLateBinding.html
    I prefer 'Late Binding' which disagrees with the Author. I like 'Late Binding' because it avoids the need for Library references, even though it may require a little more work at times when referring to Constants that are automatically provided by the Library reference.

    The following items may help you in the future:
    Debugger Secrets:
    a. Press 'F8' to single step (goes into subroutines and functions).
    b. Press SHIFT 'F8' to single step OVER subroutines and functions.
    c. Press CTRL 'F8' to stop at the line where the cursor is.
    d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
    e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
    output to the IMMEDIATE WINDOW.
    f. Select View > Locals to see all variables while debugging.
    g. To automatically set a BREAKPOINT at a certain location put in the line:
    'Debug.Assert False'
    h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
    if i >= 20 and xTV20 > 99.56 then
    Debug.Assert False
    endif
    i. A variable value will be displayed by putting the cursor over the variable name.

    Lewis

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

    Re: Method not available

    If you are using early binding and have an appropriate reference for FileSystemObject try this.
    Sub combine()
    Dim FSO As FileSystemObject
    Dim fldrobj As Folder
    Dim fileobj As File
    Dim directory As String
    Dim foldername As String
    
        Set FSO = New FileSystemObject
    
        foldername = Format(DateTime.Now, "yyyymmdd")
        directory = "C:\combiner\"
        Set fldrobj = FSO.GetFolder(directory & foldername)
    
        For Each fileobj In fldrobj.Files
    
            Application.Workbooks.Open (fileobj)
    
        Next fileobj
    
    End Sub
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    03-08-2013
    Location
    Louisiana
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Method not available

    LTMetzger,

    Thanks for the information on early and late binding. As a habit I always use early binding. . . in fact I didn't know lat binding existed.

    Norie,

    Thanks for the solution. I realized what I was having issues with. The first thing was that I defined Fldrobj and Fileobj as the wrong variable type. The other issue I realized was that I was trying to use the Filesystemobject class instead of an instance of the class.


    Thanks again!

+ 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. Replies: 2
    Last Post: 09-25-2012, 12:53 AM
  2. Method for f(x)=y
    By swedattack in forum Excel General
    Replies: 16
    Last Post: 06-13-2012, 01:22 AM
  3. A More Efficient Method Than the Evaluate Method?
    By anthony.mcgovern in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-22-2011, 05:39 AM
  4. Returning the number of cells from one method to another method.
    By Mordred in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-08-2010, 11:28 AM
  5. Help with method
    By CarolineHedges in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-31-2006, 10:29 AM
  6. GetObject method not work after Call Shell Method
    By ben in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-21-2006, 11:50 AM
  7. Why QUIT method doesn't work after COPY method?
    By surotkin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-26-2005, 11:32 AM
  8. best method
    By Tim in forum Excel General
    Replies: 1
    Last Post: 10-17-2005, 06:05 AM

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