Consulting

Results 1 to 5 of 5

Thread: Solved: opening filedialog in excel 2000

  1. #1

    Solved: opening filedialog in excel 2000

    Hi,

    I am trying to open a 'open file' dialog window using vba, but the following code (found on this site) gives me a 'runtime error 438:'

    [vba]Sub mmm()

    With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = False
    .Show
    If .SelectedItems.Count > 0 Then
    MsgBox .SelectedItems(1)
    End If
    End With

    End Sub[/vba]
    I looked in the Object Browser and could not see that application > filedialog listed; is this not available in Excel2000?

    What I want to do is:
    1. Open a filedialogue box so that the user can select a .xls workbook.
    2. Import the 1st worksheet of that workbook as a worksheet called "raw_data"

    Chris

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Filedialog came in in Excel 2002 I believe so you need to use GetOpenFilename

    [vba]

    Sub OpenFiles()
    Dim mpFilename As Variant
    mpFilename = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
    If mpFilename <> False Then
    MsgBox "Open " & mpFilename
    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

  3. #3
    Ah ok,
    I wish the IT department would rollout a newer office suite!

    Thankyou for the alternate script.

    Chris

  4. #4
    Nice, this code now works, I call the script from a button on sheet 2.
    It copies the 1st sheet of the opened file and places it before the sheet named "-----3".

    My next question is, how do I make the imported sheet overwrite a sheet if one of the same name exists?

    i.e. i want to be able to import the sheet if any changes are made, but dont want endless copies such as data, data(1), data(2) etc.

    [vba]Sub mpImport_data()
    Dim mpFilename As Variant
    Dim mpWorkbookData As Workbook
    Dim mpWorkbookCurrent As Workbook
    mpFilename = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
    Set mpWorkbookCurrent = ActiveWorkbook
    Set mpWorkbookData = Workbooks.Open(mpFilename)
    mpWorkbookData.Sheets(1).Copy mpWorkbookCurrent.Worksheets("-----3")
    mpWorkbookData.Close
    Sheets(2).Select
    End Sub[/vba]
    Chris

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just delete it by default before importing

    [vba]

    Application.DisplayAlerts = False
    On Error Resume Next
    Worksheets("data").Delete
    On Error Goto 0
    Application.DisplayAlerts = True
    [/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
  •