VBA query pasting data in with new columns rather than just pasting values into existing columns?

SendHalp3

New Member
Joined
Oct 2, 2021
Messages
11
Office Version
  1. 2016
Platform
  1. Windows
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!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,223,880
Messages
6,175,157
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