Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I'm trying to import data from a file in excel using :
Data -> From Text -> And select my file.

The macro recorded for this is :
VB
With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Users\Desktop\Project1.csv",Destination:=Range("$A$1"))
        .Name = Project1"
        .FieldNames = True ..........       

This always gets the data from the same path and file name, how do I change the macro such that the macro opens up the file dialogue and let the user select the required file ?
Posted
Updated 5-Jul-12 22:02pm
v2
Comments
ZurdoDev 5-Jul-12 11:08am    
Depends on the version of VB you are doing. Put a OpenFileDialog on your form and then use that instead.
perilbrain 5-Jul-12 11:41am    
He/She is talking about VBA.....
ZurdoDev 5-Jul-12 12:04pm    
You can still do forms in VBA.

 
Share this answer
 
VB
Sub getFile()
    'Declare a variable as a FileDialog object.
    Dim fd As FileDialog

    'Create a FileDialog object as a File Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    'Declare a variable to contain the path
    'of each selected item. Even though the path is a String,
    'the variable must be a Variant because For Each...Next
    'routines only work with Variants and Objects.
    Dim vrtSelectedItem As Variant
    Dim File_Name

    'Use a With...End With block to reference the FileDialog object.
    With fd

        'Allow the selection of multiple files.
        .AllowMultiSelect = False

        'Use the Show method to display the file picker dialog and return the user's action.
        'If the user presses the action button...
        If .Show = -1 Then
            File_Name = .SelectedItems(1)
            MsgBox File_Name
         
           
        'If the user presses Cancel...
        Else
        End If
    End With

    'Set the object variable to Nothing.
    Set fd = Nothing
End Sub


This can be used this way............in VBA
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & File_Name,Destination:=Range("$A$1"))
.Name = Project1"
.FieldNames = True ..........
 
Share this answer
 
v2
Comments
Sumal.V 6-Jul-12 4:13am    
Thank you very much. But I have another problem. In my previous method, where I specify the path name, I get an Import Wizard, where in I can choose the type of file, file origin, select the delimiters.
Because I select a .csv file I must specify the delimiters like comma.
The above method imports the data and prints them without organising them into columns. But now since it only selects the file name, I have no option to select the delimiters..
Sumal.V 6-Jul-12 4:27am    
Sorry I made some mistake while copying the values. It works. Thank u very much :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900