Consulting

Results 1 to 10 of 10

Thread: Solved: Opening a file via macro

  1. #1
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location

    Solved: Opening a file via macro

    Hello.
    This is my first post on the forum. Thanks for the help ahead of time. I have about 20 files all named similarly with ##- myfile.xls . I am trying to get my macro to open a file where the user can enter simply say 70 or 91 which will then open either 70- myfile.xls or 91- myfile.xls. I tried as seen below to just open it with the - myfile.xls a constant and the ## as a user entered variable. This is not working

    ideas?

    Here where I am code wise currently:

    ****************
    [VBA]
    'Inputbox for tab to generate
    Dim DivVariable As String
    DivVariable = InputBox("2 Div Number", "Enter Div Number", "")
    Dim FileVariable As String
    FileVariable = "F:\mydir\"DivVariable"- myfile.xls"
    '
    'Open All Files
    ChDir "F:\mydir\"
    Workbooks.Open Filename:= _
    FileVariable, UpdateLinks:=0

    Windows(DivVariable"- myfile.xls").Activate[/VBA]
    *************************************************

  2. #2
    Try this:

    [VBA]'Inputbox for tab to generate
    Dim DivVariable As String
    Dim FilePath As String
    Dim FileName As String
    DivVariable = InputBox("2 Div Number", "Enter Div Number", "")
    FilePath = "F:\mydir\"
    FileName = DivVariable & "- myfile.xls"
    '
    'Open All Files
    Workbooks.Open FileName:=FilePath & FileName, UpdateLinks:=0
    Windows(FileName).Activate[/VBA]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why not just use filedialog and let them browse for it

    [vba]

    Dim nCount As Long

    ' Open the file dialog
    With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = True
    .Show

    ' Display paths of each file selected
    For nCount = 1 To .SelectedItems.Count
    Workbooks.Open .SelectedItems(nCount)
    Exit For
    Next lngCount
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    You should always check if the workbook is open before opening a file via VBA. You can do so with a simple function...

    [vba]function WbOpen(wbName as string) as Boolean
    'Originally found by Jake Marx
    on error resume next
    wbopen = len(workbooks(wbname).name)
    end function[/vba]

    HTH

  5. #5
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Thank you for all the great suggestions. I will try them out and see which best fits the need of the project.

  6. #6
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Does the code to check if it's already open go anywhere specific? Can that be right in the middle of my 'sub'. Sorry if that's a n00b question. I've only been working with VBA for a few days.

    Thanks

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Put it before the opne code, there is no point opening it if it already open.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Are functions like that to be within a Sub? It says that I must end the sub before having that code?

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes indeed, if it isn't within a sub it couldn't be invoked/called.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I generally try to set books as variables when using them via code. To set a workbook to a variable dependent on whether it is open or not, what I use (just one way)...
    [vba]dim wb as workbook
    '...
    if wbopen("name.xls") = true then set wb = workbooks("name.xls")
    if wb is nothing then set wb = workbooks.open("C:\path\name.xls")
    '...
    msgbox wb.name 'whatever here... you can manipulate it with this variable[/vba]

    HTH

Posting Permissions

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