0

I am trying to use msoFileDialogOpen in VBA (using Excel 2010) to import data from a file. The macro should make it delimited and separated by comma on import. Here is the macro:

Public Sub Function4_FileExplorer()
Dim file As String
'   Start File Explorer to select file containing data (simple GUI, much easier than coding vFileName)
On Error GoTo ErrorHandler
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
If .Show Then
    file = .SelectedItems(1)
    Workbooks.Open file ' there used to be Path.Open here but that doesn't work (since Path is a string)
End If
End With
ErrorHandler:
MsgBox "Error detected" & vbNewLine & "Error" & Err.Number & _
       Err.Description, vbCritical, "Error Handler: Error " & Err.Number

End Sub

This demonstrates what the kind of files I am trying to separate, 1 value per cell (just Excel files, not in Notepad):

http://blogs.technet.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-76-18-metablogapi/2055.HSG09091001_5F00_71EB0EE6.jpg

1
  • 1
    Have you tried to record a macro while importing the file to get the proper syntax? Developer Tab->Code->Record Macro->Data Tab-> Get External Data->From Text --->Fill in Import file wizard --- Developer Tab->Code->Stop Recording
    – TnTinMn
    Jul 15, 2015 at 14:15

1 Answer 1

0

This would be one way to go :

Public Sub Function4_FileExplorer()
Dim File As String, _
    Wb As Workbook, _
    Ws As Worksheet, _
    WsDest As Worksheet

'   Start File Explorer to select file containing data (simple GUI, much easier than coding vFileName)
On Error GoTo ErrorHandler
With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = False
    If .Show Then
        File = .SelectedItems(1)
        Set Wb = Workbooks.Open(File) ' there used to be Path.Open here but that doesn't work (since Path is a string)
    End If
End With

Set Ws = Wb.ActiveSheet
'Or
'Set Ws = Wb.Sheets("Sheet_Name_Here")

Ws.Copy After:=Wb.Sheets(Wb.Sheets.Count)
Set WsDest = Wb.ActiveSheet

    WsDest.Range("A1:A" & WsDest.Range("A" & WsDest.Rows.Count).End(xlUp).Row).TextToColumns _
            Destination:=WsDest.Range("AA1"), _
            DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, _
            Tab:=False, _
            Semicolon:=False, _
            Comma:=True, _
            Space:=False, _
            Other:=False, _
            FieldInfo:=Array(1, 1), _
            TrailingMinusNumbers:=True

WsDest.Range("AA1").Activate
GoTo SubCloser
Exit Sub

ErrorHandler:
MsgBox "Error detected" & vbNewLine & "Error" & Err.Number & _
       Err.Description, vbCritical, "Error Handler: Error " & Err.Number

SubCloser:
Set Wb = Nothing
Set Ws = Nothing
Set WsDest = Nothing

End Sub
4
  • thanks for the function! When I compile it, however, I get the error message "Compile error: Method or data member not found". The line in question:
    – user4974730
    Jul 15, 2015 at 14:55
  • Set WsDest = Wb.Worksheets.Add
    – user4974730
    Jul 15, 2015 at 14:55
  • My bad, it must be Set WsDest = Wb.Sheets.Add
    – R3uK
    Jul 15, 2015 at 15:05
  • then I get the error: "Compile error: method / data member not found" on line "Set WsDest.Wb.Sheets.Add".
    – user4974730
    Jul 15, 2015 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.