Closed Thread
Results 1 to 5 of 5

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

  1. #1
    uriel78
    Guest

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

    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



  2. #2
    Tom Ogilvy
    Guest

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

    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

    --
    Regards,
    Tom Ogilvy

    "uriel78" <[email protected]> wrote in message
    news:[email protected]...
    > 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
    >
    >




  3. #3
    uriel78
    Guest

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

    Thank you very much, it does exactly what i was looking for!!


    "Tom Ogilvy" <[email protected]> ha scritto nel messaggio
    news:%[email protected]...
    > 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
    >
    > --
    > Regards,
    > Tom Ogilvy




  4. #4
    uriel78
    Guest

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

    :-(
    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...:-(



    "Tom Ogilvy" <[email protected]> ha scritto nel messaggio
    news:%[email protected]...
    > 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
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "uriel78" <[email protected]> wrote in message
    > news:[email protected]...
    > > 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
    > >
    > >

    >
    >




  5. #5
    uriel78
    Guest

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

    :-) evrything gets ok this morning, maybe last night my brain was too
    hot....:-)



    "uriel78" <[email protected]> ha scritto nel messaggio
    news:[email protected]...
    > :-(
    > 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...:-(




Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1