+ Reply to Thread
Results 1 to 9 of 9

EXCEL Text to Columns macro with a variable numberof Fields

Hybrid View

  1. #1
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,906

    EXCEL Text to Columns macro with a variable numberof Fields

    Hi Guys,
    I've go a question again about something that I can't seem to put together.

    The code below works fine for the 41 columns and does exactly what it needs to do
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
        Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), _
        Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1), Array(20, 1), _
        Array(21, 1), Array(22, 1), Array(23, 1), Array(24, 1), Array(25, 1), Array(26, 1), Array(27, 1), _
        Array(28, 1), Array(29, 1), Array(30, 1), Array(31, 1), Array(32, 1), Array(33, 1), Array(34, 1), _
        Array(35, 1), Array(36, 1), Array(37, 1), Array(38, 1), Array(39, 1), Array(40, 1), Array(41, 1)), _
        TrailingMinusNumbers:=True

    What I am trying to figure out is how can y use variable list of fields for the FiledInfo

    I have this routine in the macro that checks how many columns are in the first row of the importet text file

    Dim headArrRow  As Variant, hCol As Integer, fInfoArrlist As Variant
    headArrRow = Split(WS.Cells(1, 1), ",")
    fInfoArrlist = ""
    For hCol = LBound(headArrRow) To UBound(headArrRow)
        fInfoArrlist = fInfoArrlist & "Array(" & hCol & " ,1),"
    Next hCol
    fInfoArrlist = Trim(Left(fInfoArrlist, Len(fInfoArrlist) - 1))
    The contents of fInfoArrList will vary in relation to the what the text file contains

    [code]
    For 6 colums it looks like this fInfoArrList= "Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)"
    And more columns well more

    My idea is that I use this as a variable (see the red text below) but it doesn't work, no error nothing it just doesn't do anything the data remains in one column

    'Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=Array(fInfoArrlist), _
        TrailingMinusNumbers:=True
    The question is how can this be made to work?

    Hope my explanation makes sense
    Thanks for looking and any ideas or tips are welcome.

    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,726

    Re: EXCEL Text to Columns macro with a variable numberof Fields

    Instead of building a string, why not ReDim an array the size you want and then loop through it making each entry in the array represent a column … much as you have done with the string. That way, you end up with an array. Hopefully you can use that in the text to columns.

    Totally untested but an idea to play with.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: EXCEL Text to Columns macro with a variable numberof Fields

    Yes, you need an array whose elements are themselves arrays.
    Remember what the dormouse said
    Feed your head

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,906

    Re: EXCEL Text to Columns macro with a variable numberof Fields

    Thanks @TMS adn @rorya, the array, yes, that's tru a single array, I was confused by the Array(1,1) all I need then is one dimentional array .
    I'm off to be now and will look and test tomorrow, I was making it more difficult than it really is.
    Thanks again, will update tomorrow (if I get it done) and will also post the code snippet
    Good-night

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: EXCEL Text to Columns macro with a variable numberof Fields

    A 1D array of arrays to be specific.

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: EXCEL Text to Columns macro with a variable numberof Fields


    Hi,

    as FiedInfo parameter is optional and according to the initial post code as all columns are set to general so it is useless, can be removed !

    When this parameter is really necessary - for example to convert dates - you can use an array for the non general columns …

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,906

    Re: EXCEL Text to Columns macro with a variable numberof Fields

    @Marc L: I agree and this is the default setting, once I get that working I have a table that checks the ghearder name where it reads if it's a date or a general or specific text setting, thta's why I'm trying to figure it out
    Tried array one D and 2D but it doesn't work

    My last attempt as 2D array no go either
    Dim headArrRow  As Variant, hCol As Integer, fInfoArrlist() As Variant
    ReDim fInfoArrlist(1 To UBound(headArrRow), 2)
    For hCol = LBound(headArrRow) To UBound(headArrRow) - 1
        fInfoArrlist(hCol + 1, 1) = hCol + 1
        fInfoArrlist(hCol + 1, 2) = 1
    Next hCol
    '*
    WS.Activate
    Columns("A:A").Select
    
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=fInfoArrlist, _
        TrailingMinusNumbers:=True
    Thanks anyway, will find another way

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: EXCEL Text to Columns macro with a variable numberof Fields

    2D is wrong. You need a 1d array and each element of that is also a 1D array

  9. #9
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,906

    Re: EXCEL Text to Columns macro with a variable numberof Fields

    Hi guys, thanks and i got it working, now only the finetuning but thanks

    Dim headArrRow  As Variant, hCol As Integer, fInfoArrlist()
    headArrRow = Split(WS.Cells(1, 1), ",")
    ReDim fInfoArrlist(1 To UBound(headArrRow), 1)
    For hCol = 1 To UBound(headArrRow)
        fInfoArrlist(hCol, 1) = 1
    Next hCol
    '*
    WS.Activate
    Columns("A:A").Select
    
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=fInfoArrlist, _
        TrailingMinusNumbers:=True

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro to Export Variable columns from One Excel Work Sheet to another
    By naresh73 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2013, 11:01 AM
  2. Replies: 1
    Last Post: 10-27-2012, 02:54 AM
  3. [SOLVED] How to add counter to monitor numberof edits and copy this data in diff cells??
    By CHETANHADIMANI in forum Excel - New Users/Basics
    Replies: 27
    Last Post: 07-20-2012, 02:25 PM
  4. Scan through row 1 fields for specified text and record the column in a variable
    By wfogel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-28-2010, 04:49 PM
  5. Import text fields into columns from txt file
    By Pshawn in forum Excel General
    Replies: 3
    Last Post: 01-28-2010, 12:30 PM
  6. Import Text file Fields as Columns in Excel
    By vijay2482 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2009, 11:05 AM
  7. [SOLVED] Excel SHOULD NOT AUTO-CHANGE formated text fields to DATE FIELDS!
    By PSSSD in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-08-2006, 04:35 PM
  8. [SOLVED] how to compare text fields in 2 different columns in excel
    By ljt1 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-31-2006, 07:25 PM

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