I have a workbook that i copy each day, import a fresh txt file (created daily), format some columns to ensure text as they are numeric identifiers and excel always compacts them. I then proceed to run various macros against this data to conditional format and populate cells with formulas. This is a daily task using fresh extract, hence why using macros to streamline and ensure regularity.
I am just about complete, but i still manually import the file, use powerdata to select columns that i need to change to text and then load.
I have recorded macro of me importing the text file, but when i run the macro, I get the following error "[Expression.Error] The value isn't a single-character string."
I have tried a few times and always same error. But when i do steps manually then there is never an issue. Script is as follows, ( i had to obscure the column data due to sensitivity but hopefully still makes sense.
I am just about complete, but i still manually import the file, use powerdata to select columns that i need to change to text and then load.
I have recorded macro of me importing the text file, but when i run the macro, I get the following error "[Expression.Error] The value isn't a single-character string."
I have tried a few times and always same error. But when i do steps manually then there is never an issue. Script is as follows, ( i had to obscure the column data due to sensitivity but hopefully still makes sense.
VBA Code:
Sub Import_Suspect_Extract_1()
'
' Import_Sheet1 Macro
'
'
ActiveWorkbook.Queries.Add Name:="Sheet1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""[B]file location[/B]\Sheet1.txt""),[Delimiter="" "", Columns=146, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{" & _
"{""SuspectIdPK"", type text}, {""Rec 1"", type text}, {""Rec 2"", type text}, [B]'Lots of other columns in here'[/B] {""Email "", 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$;Location=Sheet1;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Sheet1]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Sheet1"
.Refresh BackgroundQuery:=True
End With
Application.CommandBars("Queries and Connections").Visible = False
MsgBox ("Suspect Extract Successfully Imported")
End Sub