Thanks Thanks:  0
Likes Likes:  0
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Macro to import file

  1. #1
    New Member
    Join Date
    May 2002
    Posts
    14
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi,

    I am trying to create a macro to import a data file into an excel sheet. I was able to record a macro,but i was only able to import the same file each time.

    Can anyone help me create a macro to open a file popup menu such that i can choose the file to be imported.

    I can send you my recorded macro code if you need.
    One more thing, this is my first encounter with VBasic.
    Please help me........
    Thank you.

  2. #2
    Board Regular
    Join Date
    Apr 2002
    Posts
    85
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    try this:

    sub import ()
    dim fileToOpen as String
    dim PasteOnThisWB as string

    PasteOnThisWB = Activeworkbook.name

    fileToOpen = Application.GetOpenFilename("Microsoft Excel Files (*.xls), *.xls")
    If fileToOpen <> False Then
    Workbooks.Open fileToOpen
    Else
    Exit Sub
    End If

    cells.select
    selection.copy

    workbooks(PasteOnThisWB).select
    Range("A1).select
    Selection.Paste

    Workbooks.Close fileToOpen Savechanges := False
    end Sub

  3. #3
    New Member
    Join Date
    May 2002
    Posts
    14
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    This helped me a bit, But i need to import a data file not open new file,your code opens an excel file.

    Can you help me with the Import thing.

    Thanks
    Raju

  4. #4
    New Member
    Join Date
    May 2002
    Posts
    14
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    ThIs isn't working.there is an error at the IF statement....

  5. #5
    Board Regular
    Join Date
    Apr 2002
    Posts
    85
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    What does error say? I just used that exact code in a macro

  6. #6
    New Member
    Join Date
    May 2002
    Posts
    14
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Run-time error '13':
    Type Mismatch
    I changed the parameters to "Allfiles *.* instead of "Microsoft Excel Files*.xls"


  7. #7
    Board Regular
    Join Date
    Mar 2002
    Location
    England, UK.
    Posts
    526
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    what data file, a text file?

    If so, is the data in the text file seperated by a comma, space or something or is it all 'stuck' together?

    RET79

    [ This Message was edited by: RET79 on 2002-05-02 13:25 ]

  8. #8
    Board Regular
    Join Date
    Apr 2002
    Posts
    85
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Sorry!

    Dim fileToOpen as variant

    that should fix runtime

  9. #9
    New Member
    Join Date
    May 2002
    Posts
    14
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Its a text file Seperated by a commas

  10. #10
    New Member
    Join Date
    May 2002
    Posts
    14
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thanks
    the Variant thing helped but was not able file in a table form.
    I think it has something to do with the comma delimiter. and how can i make the file to be open in the next sheet.

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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