+ Reply to Thread
Results 1 to 9 of 9

Convert two dimensional row to column data to a hard-coded array

  1. #1
    Registered User
    Join Date
    10-15-2012
    Location
    Cary
    MS-Off Ver
    Excel 2003
    Posts
    18

    Convert two dimensional row to column data to a hard-coded array

    Hello Forum,

    I'm struggling to design a reasonable way that will turn two dimensional data into a hard-coded two dimensional array. The data is a long list that looks like the following (each comma here is a new column and each carriage return is a new row):

    33.03349 , [M+H]+ , CH3OH , Methanol, Acetonitrile / solvent
    42.03383, [M+H]+, CH3CN, ACN, Acetonitrile / solvent
    .....

    The idea is to take the data in the sheet, convert it in some reasonable way (concatenation maybe...) and then enter the data into the array similar to the following:

    pcon(0,0) = 33.03349
    pcon(0,1) = [M+H]+
    pcon(0,2) = CH3OH
    pcon(0,3) = Methanol
    pcon(0,4) = Acetonitrile / solvent
    pcon(1,0) = 42.03383
    pcon(1,1) = [M+H]+
    pcon(1,2) = CH3CN
    pcon(1,3) = ACN
    pcon(1,4) = Acetonitrile / solvent
    ....

    Is there an intelligent way to iterate this or loop through it?

    I started down this road:

    Please Login or Register  to view this content.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Convert two dimensional row to column data to a hard-coded array

    This is a large table of row-data? 5 cells per row make each set and you want to fill that into an array in VBA memory?

    If the data starts in a known position and you have a way to check how many rows of data there are, you can fill the array all at once with the values in the found range of cells. For example, were the data to start in A1 and go down "x" rows and is 5 columns:

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    10-15-2012
    Location
    Cary
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Convert two dimensional row to column data to a hard-coded array

    Thanks for the response. That is an elegant way to move the data within a sheet to an array - thanks.

    I was hoping to "hard-code" the data such that all the data is contained within the code and not generated by a sheet range reference. My objective is to have a template file containing all blank sheets and a back-end reference library.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Convert two dimensional row to column data to a hard-coded array

    Here's an idea
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-15-2012
    Location
    Cary
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Convert two dimensional row to column data to a hard-coded array

    This looks like a good way to separate it out. I attempted to paste one section that had 750 records and I was notified with the "Too many line continuations error message" which seems to happen ~ 30 records. I used the following line to concatenate the first and then the rest of excel data rows:

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    =

    I then entered a segment of this data into VBA:

    Please Login or Register  to view this content.
    Perhaps the solution is to create a hidden sheet with the information and use the first recommended array?

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Convert two dimensional row to column data to a hard-coded array

    Quote Originally Posted by evancharles View Post
    Perhaps the solution is to create a hidden sheet with the information and use the first recommended array?

    That's exactly how I would do it. Not only is the code extremely simple this way, so is editing the values in your hidden sheet data table. If you could get the monster code above working, maintaining/editing would be a migraine.

    You can use VBA to hide the sheet with the xlVeryHidden attribute and then no one could see the sheet at all, nor unhide it, without VBA.

  7. #7
    Registered User
    Join Date
    10-15-2012
    Location
    Cary
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Convert two dimensional row to column data to a hard-coded array

    Thank you for all of your recommendations. I agree that sifting through the code to apply an update would be unnecessarily complicated. Thumbs up on both code recommendations as they are directly applicable.

    The hidden sheet with a dynamicly referenced array method will be the road to take here; errors combined with complex modifications makes the hard-coded approach a poor choice.

    Cheers.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Convert two dimensional row to column data to a hard-coded array

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Thanks.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Convert two dimensional row to column data to a hard-coded array

    I was notified with the "Too many line continuations error message" which seems to happen ~ 30 records.
    You can separate and continue like this.
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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