The goal is to import a text file using the path and file name contained in a cell that's a named range.
I have a macro that I use daily to import a text file. The macro asks for the specific file to be imported. I would like to import the files without user input. The files to import are always the newest file and the next to newest file to two different worksheets for comparison of changes.
I have adapted a macro to list all the files in a specific folder on a worksheet and sort them in descending date order. Thus the newest file will always be in cell C1/ range "cel_Admin1Path" and the secondoldest newest in cell C2/ range "cel_Admin2Path"
I've pared the code down to what I think is the minimum required to ease troubleshooting. My plan is to create a second macro for the second file. KISS. Once it's working ?
Deeply appreciate a review of the code and why it's not working.
TIA
Ron
?
Edit added by Admin:
Apologies, the cell value is C:\Users\me\Documents\AD\Raw_AD_Admin\AD_Admin_Accounts_09-23-2020-0553.txt
I have a macro that I use daily to import a text file. The macro asks for the specific file to be imported. I would like to import the files without user input. The files to import are always the newest file and the next to newest file to two different worksheets for comparison of changes.
I have adapted a macro to list all the files in a specific folder on a worksheet and sort them in descending date order. Thus the newest file will always be in cell C1/ range "cel_Admin1Path" and the second
I've pared the code down to what I think is the minimum required to ease troubleshooting. My plan is to create a second macro for the second file. KISS. Once it's working ?
Deeply appreciate a review of the code and why it's not working.
TIA
Ron
?
VBA Code:
Sub m_ImportAdminTextGivenLocation()
' 9-24-20
' Import the admin user account list
' Use path and file name from worksheet ws_FileList Range cel_Admin1Path
'--ensure the worksheet is clear and named properly
'
Application.DisplayAlerts = True
ws_3Admin.Name = "Admin Accounts"
ws_3Admin.Activate
Cells.Clear
ws_3Admin.Visible = True
'
Dim ThisWb As Workbook
Dim ThisWs As Worksheet
fileToOpen = Range("cel_Admin1Path")
'
Set ThisWb = ActiveWorkbook
Set ThisWs = ActiveSheet
'
'--Start import text file---------------------------------------------------------
With ThisWs.QueryTables.Add(Connection:="TEXT;" & fileToOpen, Destination:=Range("$A$1"))
.Name = fileName
.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 = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "^"
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
'.Refresh BackgroundQuery = False
'--End import text file--
'--Delete external query range name--
For Each qtbl In ThisWs.QueryTables
qtbl.Delete
Next
'--End Delete external query range name --
End With 'ThisWs
Cells(1, 1).Select
'-------------------------------------------------------------------
End Sub
Edit added by Admin:
Apologies, the cell value is C:\Users\me\Documents\AD\Raw_AD_Admin\AD_Admin_Accounts_09-23-2020-0553.txt
Last edited by a moderator: