Consulting

Results 1 to 14 of 14

Thread: Code to pick a workbook and add a code to it

  1. #1

    Code to pick a workbook and add a code to it

    Is there a way to find all workbooks in a computer using excel, and then pick which one to add a code to it?

  2. #2
    and once picked, it runs the code to the selected workbook?

  3. #3
    and once picked, it runs the code to the selected workbook?

  4. #4
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Do you mean you want to add code to a workbook, or run code that manipulates a particular workbook?
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Quote Originally Posted by rory
    Do you mean you want to add code to a workbook, or run code that manipulates a particular workbook?
    I need a code that will let me choose a workbook, then when chosen, I can run a code I already have on it.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If you add the code to Workbook_Open in that workbook, it will run automatically when you open it.

    You can select a file using GetOpenFilename, look it up in Help to see how to use it.
    ____________________________________________
    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

  7. #7
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Well you can easily use GetOpenFilename to allow the user to choose a workbook.

    Then you can open it, create a reference to it and use that reference in the code.

    By the way your title and original post were rather confusing, I think anyway.

    I actually thought you wanted to select a workbook and then use code to create code in the selected workbook.

  8. #8
    Quote Originally Posted by Norie
    Well you can easily use GetOpenFilename to allow the user to choose a workbook.

    Then you can open it, create a reference to it and use that reference in the code.

    By the way your title and original post were rather confusing, I think anyway.

    I actually thought you wanted to select a workbook and then use code to create code in the selected workbook.
    Is there a way for the code to be run as soon as the user selects the workbook from GetOpenFilename?

  9. #9
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Not really, you would probably still need to open the workbook.

    Is that a problem?

    PS What is the code actually doing?

  10. #10
    Quote Originally Posted by Norie
    Not really, you would probably still need to open the workbook.

    Is that a problem?

    PS What is the code actually doing?
    Yeah that is a problem, I need to hypothetically think the user has no Excel VBA knowledge at all. The code just creates a toolbar that hides/unhides columns in the spreadsheet.

  11. #11
    Quote Originally Posted by Norie
    Well you can easily use GetOpenFilename to allow the user to choose a workbook.

    Then you can open it, create a reference to it and use that reference in the code.

    By the way your title and original post were rather confusing, I think anyway.

    I actually thought you wanted to select a workbook and then use code to create code in the selected workbook.
    Can I get instructions on doing this?

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sure, lookup up GetOpenFilename and Set in VBA help.
    ____________________________________________
    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

  13. #13
    I got this far:

    Quote Originally Posted by xld
    Sure, lookup up GetOpenFilename and Set in VBA help.
    Sub GetImportFilename()
        Dim Finfo As String
        Dim FilterIndex As Integer
        Dim Title As String
        Dim Filename As Variant
        
        '   Set up list of file filters
            Finfo = "Text Files (*.txt),*.txt," & _
                    "Lotus Files (*.prn),*.prn," & _
                    "Comma Separated Filed (*.csv),*.csv," & _
                    "ASCII FILES (*.asc),*.asc," & _
                    "All Files (*.*),*.*"
    
    '   Display *.* by default
        FilterIndex = 5
        
    '   Set the dialog box caption
        Title = "Select a File to Import"
        
    '   Get the filename
        Filename = Application.GetOpenFilename(Finfo, FilterIndex, File)
        
    '   Handel return info dialog box
        If Filename = False Then
            MsgBox "No file was selected."
        Else
            MsgBox "You selected " & Filename
        End If
        
    
        
    
    End Sub
    how do I make it open the file when I select it?

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub GetImportFilename()
    Dim Finfo As String
    Dim FilterIndex As Integer
    Dim Title As String
    Dim Filename As Variant
    Dim WB As Workbook

    ' Set up list of file filters
    Finfo = "Text Files (*.txt),*.txt," & _
    "Lotus Files (*.prn),*.prn," & _
    "Comma Separated Filed (*.csv),*.csv," & _
    "ASCII FILES (*.asc),*.asc," & _
    "All Files (*.*),*.*"

    ' Display *.* by default
    FilterIndex = 5

    ' Set the dialog box caption
    Title = "Select a File to Import"

    ' Get the filename
    Filename = Application.GetOpenFilename(Finfo, FilterIndex, file)

    ' Handel return info dialog box
    If Filename = False Then
    Exit Sub
    Else
    Set WB = Workbooks.Open(Filename)
    End If

    End Sub
    [/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

Posting Permissions

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