1

So I work with CSV files and I need to open excel go to Data -> From text and format certain columns certain ways. I recorded a macro that does that but it always opens the file I used when recording the macro. How do I modify the macro so it opens the dialog box and let me choose a file each time? I found this piece of code on the internet but I don't know how to integrate it with my recorded macro in VBA.

Dim MyFile As String
MyFile = Application.GetOpenFilename()

Now how and where and what do I replace in the below macro (the code for which is created using the "record macro" button in excel)?

Sub random_name()
'
' random_name Macro
'

'
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;MyFile = Application.GetOpenFilename()" _
        , Destination:=Range("$A$1"))
        .Name = "filename"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 1252
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

Thanks!

Community
  • 1
  • 1

2 Answers2

1

Hey I have modified your code:

Sub random_name()
'
' random_name Macro
'

'
Dim connectioString As String

connectioString = "TEXT;" & ListFile


    With ActiveSheet.QueryTables.Add(Connection:= _
        connectioString _
        , Destination:=Range("$A$1"))
        .Name = "filename"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 1252
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

beneth this paste this function:

Function ListFile()
' ----- Creating a dialog object -----------------------------------
    Dim oDiag As FileDialog
    Dim vrtSelectedItem As Variant
    Dim i As Integer
    Set oDiag = Application.FileDialog(msoFileDialogFilePicker)
    i = 0
    With oDiag
' ----- Going thru all of the files --------------------------------
        .AllowMultiSelect = False
        If .Show = -1 Then

           ListFile = .SelectedItems(1)

        End If
    End With

    Set oDiag = Nothing

End Function

I am not at this point a 100% sure if filedialog required a reference tell me if it works :)

Tackgnol
  • 483
  • 1
  • 9
  • 15
  • Thanks! Works like a charm. Could you just explain line 15 ".Name = "filename"" "filename" was put by me there to censor my actual used filename. When I first recorded the macro it used to be ".Name = "" So in reality this "filename" that is there right now relates to absolutely nothing yet it causes no problems - why is that? could it cause any problems in some situations? Thanks! – Tony Georgiev Jan 26 '17 at 07:44
  • Hi as far as I see in the documentation [link](https://msdn.microsoft.com/en-us/library/office/ff820914.aspx) The name can be anything you want it to be :) so you can replace that particular piece of code with anything. If you go in Excel Data -> Connections you will see it is under your filename. You can however use this name to refer to it in VBA: ActiveSheet.QueryTables("filename"). – Tackgnol Jan 26 '17 at 10:54
0

Try this:

Path = Application.GetOpenFilename()
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & Path, Destination:=Range("$A$1"))
Vinnie
  • 553
  • 1
  • 6
  • 10