Need macro to import text file from same folder workbook is located

Liberteric

New Member
Joined
Sep 29, 2014
Messages
28
I need to import the text file DPLOTUS.csv from whatever folder the workbook that contains this macro is located.
The macro below works great for me but when I give it to my fiance, the folder names will be different. What won't be different is the workbook AND the text file will be located in the same folder.



Sub Import()
'
' import Macro
'
'
Sheets("TB").Select
Columns("A:D").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A9").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\cbeaulieu\Desktop\DPLOTUS.csv", Destination:=Range("$B$9"))
.Name = "DPLOTUS"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 9, 1, 9, 9, 9, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try replacing...

Code:
"TEXT;C:\Users\cbeaulieu\Desktop\DPLOTUS.csv"

with

Code:
"TEXT;" & ThisWorkbook.Path & "\DPLOTUS.csv"

Hope this helps!
 
Upvote 0
Domenic answered it while i was playing with your code :)... anyway here is a bit of code to try as well:

Code:
Sub Import()
'
' import Macro

Dim SourceBook As Workbook
Dim TB As Worksheet
Dim wbPath As String

' Turn off screen updating.
    Application.ScreenUpdating = False

' Set object variables for the active book and active cell.
    Set SourceBook = ActiveWorkbook
    Set TB = SourceBook.Worksheets("TB")
    wbPath = SourceBook.Path & "\"
    
    
TB.Range("A:D").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

    With TB.QueryTables.Add(Connection:="TEXT;" & wbPath & "DPLOTUS.csv", Destination:=TB.Range("$B$9"))
            .Name = "DPLOTUS"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 9, 1, 9, 9, 9, 9)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
    
    End With

' Turn off screen updating.
     Application.ScreenUpdating = False
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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