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!
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 :)
Try this:
Path = Application.GetOpenFilename()
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Path, Destination:=Range("$A$1"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.