macro to import file (help with a piece of code)

U

uriel78

I actually use a macro to import a txt file with a lot of columns

Below there is a part of the code that allows me to import the file...
I want to import only the first 6 columns (from A to g) and actually I
import all the columns and then erase the exceeding with

Columns("G:IV").Select
Selection.Delete Shift:=xlToLeft

How can I import them without doing the delete procedure...?
I think there is sthg to do with

.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)

But I don't understand what....



With ActiveSheet.QueryTables.Add(Connection:=fileToOpen _
, Destination:=Range("A1"))
.Name = "Push"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 3
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierNone
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
Columns("G:IV").Select
Selection.Delete Shift:=xlToLeft
 
T

Tom Ogilvy

first initialize an array to pass to TextFileColumnDataTypes:

dim v(0 to 255) as Long
for i = 0 to 255
if i <= 7
v(i) = 1
else
v(i) = 9
end if
Next

9 means to skip the column

then change you code:

.TextFileColumnDataTypes = v
 
U

uriel78

:-(
it seems to work 'til yesterday....

now it doesn't...

I just want to import only those 7 columns and I want values already on the
sheet to be replaced by the data imported without moving any rows or
columns...how can I do...I'm struggling...:-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top