hi!
I'm working on a code that enables the user to choose a file and if that file happens to be a csv-file, make some modifications to it.
Also the chosen file should be added to the end of the workbook.
But as you can imagine my code does not work
I think the problem lies in the myfile-variable after the user chooses the file, but I'm not sure :D
Any help is much appreciated!
I'm working on a code that enables the user to choose a file and if that file happens to be a csv-file, make some modifications to it.
Also the chosen file should be added to the end of the workbook.
But as you can imagine my code does not work
Code:
Dim wb As Workbook
Dim myfile As Variant
Set wb = ThisWorkbook
myfile = Application.GetOpenFilename
If myfile <> False Then
Workbooks.Open Filename:=myfile
If myfile Like "*csv" Then
ActiveWorkbook.Queries.Add Name = myfile, Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(myfile),[Delimiter="";"", Columns=9, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", type" & _
" text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
'
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [myfile]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
'.ListObject.DisplayName = "output_10"
.Refresh BackgroundQuery:=False
End With
Rows("1:1").Select
ActiveSheet.ShowHeaders = False
Selection.Delete Shift:=xlUp
End If
End If
Sheets(Sheets.Count).Name = Format(Date, "mmyyyy") & Sheets(Sheets.Count).Range("B11")
I think the problem lies in the myfile-variable after the user chooses the file, but I'm not sure :D
Any help is much appreciated!