Import data from notepad

Ryanmm50

New Member
Joined
Jun 26, 2017
Messages
13
I need to import data from notepad but I need the source notepad file to be selected by the open file prompt. I tried using the macro recorder but I can't get it to adjust to the variable. And does anyone know the character [10] and [13] means? The commented out part of the code is the original code I thought would work using the variable datafile. It doesn't import any data, just the headers.
Thanks in Advance!


Code:
Sub GetData()
'7/26/17 New Macro
'Gets Data from the POS and formats it into usable data


 Dim dataFile As String
 Dim dataSheet As Worksheet
 Dim rawDat As String
  
'    Set dataSheet = Sheets.Add
'    dataSheet.Name = ("Raw Data")
    'dataFile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    
'If dataFile = "False" Then
'    Exit Sub
'End If


'
    Range("A1").Select
    ActiveWorkbook.Queries.Add Name:="July test (4)", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Table.FromColumns({Lines.FromBinary(File.Contents(""C:\Users\user\Desktop\July test.txt""), null, null, 1252)})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Source"
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""July test (4)""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [July test (4)]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "July_test__4"
        .Refresh BackgroundQuery:=False
    End With
    Range("A2").Select










''First try by recording macro and what i thought to change
'Range("A1").Select
'    ActiveWorkbook.Queries.Add Name:="Raw Data", Formula:= _
'        "let" & Chr(13) & "" & Chr(10) & "    Source = Table.FromColumns({Lines.FromBinary(File.Contents(datafile), null, null, 1252)})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Source"
'    ActiveWorkbook.Worksheets.Add
'    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
'        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=datafile" _
'        , Destination:=Range("$A$1")).QueryTable
'        .CommandType = xlCmdSql
'        .CommandText = Array("SELECT * FROM " & dataFile)
'        .RowNumbers = False
'        .FillAdjacentFormulas = False
'        .PreserveFormatting = True
'        .RefreshOnFileOpen = False
'        .BackgroundQuery = True
'        .RefreshStyle = xlInsertDeleteCells
'        .SavePassword = False
'        .SaveData = True
'        .AdjustColumnWidth = True
'        .RefreshPeriod = 0
'        .PreserveColumnInfo = True
'        '.ListObject.DisplayName = "Raw Data List"
'        '.Refresh BackgroundQuery:=False
'    End With
'    Range("A2").Select
'
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Re: Inport data from notepad

Try this.
Code:
Sub GetData()
'7/26/17 New Macro
'Gets Data from the POS and formats it into usable data


 Dim dataFile As String
 Dim dataSheet As Worksheet
 Dim rawDat As String
  
'    Set dataSheet = Sheets.Add
'    dataSheet.Name = ("Raw Data")
    'dataFile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    
'If dataFile = "False" Then
'    Exit Sub
'End If


'
    Range("A1").Select
    ActiveWorkbook.Queries.Add Name:="July test (4)", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Table.FromColumns({Lines.FromBinary(File.Contents(""" & datafile & """), null, null, 1252)})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Source"
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""July test (4)""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [July test (4)]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "July_test__4"
        .Refresh BackgroundQuery:=False
    End With
    Range("A2").Select
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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