Import and format txt file into open workbook.

djdbg1

New Member
Joined
Aug 23, 2017
Messages
17
Office Version
  1. 365
Platform
  1. Windows
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.

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
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Why are you inserting a new query using VBA, Powerquery was intended to give you "Build once, refresh many" solutions.
Using the From File, From Folder button it allows you to (e.g.) automatically import the most recent txt file from a folder on every refresh.
 
Upvote 0
Why are you inserting a new query using VBA, Powerquery was intended to give you "Build once, refresh many" solutions.
Using the From File, From Folder button it allows you to (e.g.) automatically import the most recent txt file from a folder on every refresh.
I'm not overly familiar with Power Query, i just use it to import txt docs to excel so i can ensure columns formatted correctly.
But.. i create a new folder each day (as it is a daily task), and a fresh extract (txt doc) goes into that folder each day along with newly created workbook (copied from master which has macros saved in it), which is fed by the fresh extract
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top