Results 1 to 10 of 10

Thread: quickie - how to open multiple spreadsheets?[RESOLVED]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Resolved quickie - how to open multiple spreadsheets?[RESOLVED]

    I'm just looking for a line or two of code that opens all the .xls files in a directory (input as String by user).

    i.e. a working version of this:
    VB Code:
    1. Dim SourcePath As String
    2.     Dim fName As String
    3.    
    4.     SourcePath = InputBox("Enter Path to File(s)")
    5.    
    6.     ChDir SourcePath
    7.        
    8.     Workbooks.Open Filename:=*.*

    I assume it needs some sort of loop in the form of for all file in sourcepath, workbook.open filename:= file
    Last edited by pickarooney; Apr 21st, 2005 at 09:22 AM.

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: quickie - how to open multiple spreadsheets?

    VB Code:
    1. Option Explicit
    2.  
    3. Sub OpenAllFiles()
    4.     Dim vPath As String
    5.     Dim vFile As String
    6.     vPath = InputBox("Path?")
    7.     If Right(vPath, 1) <> "\" Then vPath = vPath & "\"
    8.     vFile = Dir(vPath & "*.xls")
    9.     While vFile <> ""
    10.         Workbooks.Open vPath & vFile
    11.         vFile = Dir
    12.     Wend
    13. End Sub
    This world is not my home. I'm just passing through.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: quickie - how to open multiple spreadsheets?

    Meur ras trisuglow

    What does the Option Explicit bit do?

  4. #4
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: quickie - how to open multiple spreadsheets?

    Throws up hands in horror!!

    Option Explicit is an instruction to VB to tell you if you haven't declared your variables. It also prevents you making a load of other basic errors.

    Everyone should always use Option Explicit. It might seem like hard work having to declare all your variables to begin with, but once you get used to it you'll find its well worth the effort.

    While I'm ranting, I don't want to see anybody using the Variant data type unless they have a very good excuse. Too many people use Variant because they can't be bothered to work out what they're really trying to do.
    This world is not my home. I'm just passing through.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: quickie - how to open multiple spreadsheets?

    Ah, it's for people who make mistakes...

  6. #6
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: quickie - how to open multiple spreadsheets?

    Like me

    PS missed the Cornish reference. A wodhes kewsel Kernewek? (sadly I don't).
    This world is not my home. I'm just passing through.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: quickie - how to open multiple spreadsheets?

    Na gewsav, ny gewsav Kernewek yn ta (or even at all). Just googled it for a laugh

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: quickie - how to open multiple spreadsheets?

    Sorry to reopen this, but I'm writing a new macro (in word) and need to be able to open all DOC and all RTF files in a directory. Is it possible to do that in one statement or do I need to make a second loop? I'm just asking now because I'm playing around with creating my own dialogs and currently have one which prompts the user to enter a path and pick either DOC, RTF or BOTH from a set of radio buttons, and BOTH currently returns the string *.doc, *.rtf, which I then use to open all the files of each type. If I'd be better opening each set of files in a separate loop, I think it's best if I change the three radio buttons to two tick boxes and just return a simple *.doc or *.rtf or both.

  9. #9
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: quickie - how to open multiple spreadsheets?

    Does this Word macro sample help you at all?
    VB Code:
    1. Sub OpenAll()
    2. '
    3. ' OpenAll Macro
    4. ' Macro created 21/04/2005 by
    5. '
    6.     Dim str As String
    7.     Dim vFiles As String
    8.     With Application.FileSearch
    9.         .FileName = "*.doc;*.txt"
    10.         .LookIn = "C:"
    11.         .Execute
    12.         For i = 1 To .FoundFiles.Count
    13.             str = str & .FoundFiles(i) & vbCrLf
    14.         Next i
    15.         MsgBox str
    16.     End With
    17. End Sub
    This world is not my home. I'm just passing through.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: quickie - how to open multiple spreadsheets?

    Yep, cheers again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width