Hi!
I have a macro/vba that imports a .txt file (i wanted to be able to assign the macro to create a button to import rather than copy/paste), I just wanted pastevalues starting with a specific cell, keep the layout/formatting because its in a nice grid. I had it working fine for a week then the following week it stopped pasting values, and is oadding in the data with new columns. Nothing changed with the import report.
I have another macro that clears the space prior to the import so its not that theres data there it doesn't want to paste over.
Here is the general vba:
Sub ImportReport()
Dim fileFilterPattern, fileToOpen
fileFilterPattern = "Text Files (*.txt;*.csv), *.txt; *.csv"
fileToOpen = Application.GetOpenFilename(fileFilterPattern)
If fileToOpen = False Then
'Input Canceled
MsgBox "no file selected"
Else
CSV_Open_Text3 CStr(fileToOpen)
End If
End Sub
'''
''' Open a CSV as text file to avoid formatting the data
'''
Sub CSV_Open_Text3(fname As String)
'
Dim QueryConnection As String
Dim QueryName As String
Dim ColCnt As Integer, I As Integer
Dim QT As QueryTable
Dim WS As Worksheet
Dim FormatArray() As Integer
Dim WB As Workbook
QueryConnection = "TEXT;" & fname
QueryName = "CSV_Import"
'Set WB = ThisWorkbook.Worksheets.Add
Set WB = ThisWorkbook
WB.Activate
Set WS = WB.Worksheets("Block PickUp")
Set QT = WS.QueryTables.Add(Connection:=QueryConnection, Destination:=Sheets("Report").Range("D10"))
With WS.QueryTables.Add(Connection:=QueryConnection, Destination:=Sheets("Report").Range("D10"))
.Name = QueryName
.FillAdjacentFormulas = False
.RefreshPeriod = 0
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.SaveData = True
.SavePassword = False
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
.TextFileCommaDelimiter = True
.TextFileConsecutiveDelimiter = False
.TextFileOtherDelimiter = ""
.TextFileParseType = xlDelimited
.TextFilePlatform = xlWindows
.TextFileTextQualifier = xlTextQualifierDoubleQuote
'.AdjustColumnWidth = True
'.FieldNames = True
'.TextFilePromptOnRefresh = False
'.TextFileSemicolonDelimiter = False
'.TextFileSpaceDelimiter = False
'.TextFileStartRow = 1
'.TextFileTabDelimiter = False
'.PreserveFormatting = True
'.RefreshOnFileOpen = False
.Refresh BackgroundQuery:=False
End With
Application.DisplayAlerts = True
Application.CellDragAndDrop = True
MsgBox "Import of Report complete"
End Sub
thank you for your help!
I have a macro/vba that imports a .txt file (i wanted to be able to assign the macro to create a button to import rather than copy/paste), I just wanted pastevalues starting with a specific cell, keep the layout/formatting because its in a nice grid. I had it working fine for a week then the following week it stopped pasting values, and is oadding in the data with new columns. Nothing changed with the import report.
I have another macro that clears the space prior to the import so its not that theres data there it doesn't want to paste over.
Here is the general vba:
Sub ImportReport()
Dim fileFilterPattern, fileToOpen
fileFilterPattern = "Text Files (*.txt;*.csv), *.txt; *.csv"
fileToOpen = Application.GetOpenFilename(fileFilterPattern)
If fileToOpen = False Then
'Input Canceled
MsgBox "no file selected"
Else
CSV_Open_Text3 CStr(fileToOpen)
End If
End Sub
'''
''' Open a CSV as text file to avoid formatting the data
'''
Sub CSV_Open_Text3(fname As String)
'
Dim QueryConnection As String
Dim QueryName As String
Dim ColCnt As Integer, I As Integer
Dim QT As QueryTable
Dim WS As Worksheet
Dim FormatArray() As Integer
Dim WB As Workbook
QueryConnection = "TEXT;" & fname
QueryName = "CSV_Import"
'Set WB = ThisWorkbook.Worksheets.Add
Set WB = ThisWorkbook
WB.Activate
Set WS = WB.Worksheets("Block PickUp")
Set QT = WS.QueryTables.Add(Connection:=QueryConnection, Destination:=Sheets("Report").Range("D10"))
With WS.QueryTables.Add(Connection:=QueryConnection, Destination:=Sheets("Report").Range("D10"))
.Name = QueryName
.FillAdjacentFormulas = False
.RefreshPeriod = 0
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.SaveData = True
.SavePassword = False
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
.TextFileCommaDelimiter = True
.TextFileConsecutiveDelimiter = False
.TextFileOtherDelimiter = ""
.TextFileParseType = xlDelimited
.TextFilePlatform = xlWindows
.TextFileTextQualifier = xlTextQualifierDoubleQuote
'.AdjustColumnWidth = True
'.FieldNames = True
'.TextFilePromptOnRefresh = False
'.TextFileSemicolonDelimiter = False
'.TextFileSpaceDelimiter = False
'.TextFileStartRow = 1
'.TextFileTabDelimiter = False
'.PreserveFormatting = True
'.RefreshOnFileOpen = False
.Refresh BackgroundQuery:=False
End With
Application.DisplayAlerts = True
Application.CellDragAndDrop = True
MsgBox "Import of Report complete"
End Sub
thank you for your help!