+ Reply to Thread
Results 1 to 7 of 7

Putting strings in the 'next' position in an array without indexing manually + fix video

  1. #1
    Registered User
    Join Date
    01-04-2004
    Location
    Netherlands
    Posts
    54

    Putting strings in the 'next' position in an array without indexing manually + fix video

    Hello, I hope I described it well!

    I'm writing a lot of text files to the harddrive with my spreadsheet.
    Currently I do so by using VBA to fill cells in a sheet in subsequent rows.
    Then another bit of VBA writes this as a file somewhere on the harddrive.

    For neatness and possibly speed, I would prefer to bypass writing the rows to a sheet, and instead write directly from a VBA array to a text file.
    That means I have to find a neat way to store each next string into the 'next' position in the array.
    I may often insert lines halfway through a 500 line 'text file' so that makes manual indexing a pain, if I insert a line at line 4, I have to increment
    496 entries +1.

    I have attached an example of how I do it now, how I can do it manually indexing the array, and then with what I want... But can't figure out..

    Anyone have a neat solution for my (luxury) problem?
    Attached Files Attached Files
    Last edited by niels007; 02-23-2021 at 10:41 AM. Reason: Solved:

  2. #2
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Putting strings in the 'next' position in an array without indexing manually

    If the big problem is adding rows which should be inserted between existing rows, you could always use a linked list for printing in the desired order.

    If you'd never need more than 1,000 lines, you'd create an array with 1,000 rows and however many columns you need as well as creating another array with 1,001 entries (indices 0 to 1000). Initialize that 2nd array as

    Please Login or Register  to view this content.
    This is an array of following line indices. When you add, say, row 567 which you want between rows 331 and 332, you'd need

    array2(331) = 567
    array2(567) = 332

    which could be handled by

    Please Login or Register  to view this content.
    Fill out the 1st array however you want, just handle inserting lines with the lnklstinsert procedure above. Then print using the 2nd array. You should know how many lines you want to print, call that numlines. Since the 2nd array's 1st index is 0, that'd point to the index of the 1st line, meaning you could insert rows which should appear at the top of the text file. Handle printing using

    Please Login or Register  to view this content.

  3. #3
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Putting strings in the 'next' position in an array without indexing manually

    Not sure if this is what you're looking for, but this has one line of code that will create an array as large as there are lines on the worksheet. Insert a new line anywhere in the first column and rerun the sub.
    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    09-30-2018
    Location
    Vlaams Brabant Belgium
    MS-Off Ver
    365
    Posts
    456

    Re: Putting strings in the 'next' position in an array without indexing manually

    hi,

    i would use a sheet to write and edit
    then read the sheet to array and write it to file

    why you would hard code the tekst into your vba code is a mistery to me

    here is what i would do
    Please Login or Register  to view this content.
    Please be as complete as possible in your asking so it may save use all the time to rework the solution because you didn't give all the requirements. If you have a layout in mind please work it out first so we can adapt our solution to it. Thanks.
    If you have been helped, maybe you could click the *

  5. #5
    Registered User
    Join Date
    01-04-2004
    Location
    Netherlands
    Posts
    54

    Re: Putting strings in the 'next' position in an array without indexing manually

    Hello, thanks for the suggestions. As is so often the case, the fix turned out to be simpler than I imagined!

    I didn't know that you can use a : to do multiple operations on one line of VBA code. So now I get the effect by doing this:

    Please Login or Register  to view this content.
    Obviously there isn't just text in my real project, it is a mix of text with VBA calculated numbers.
    I am most curious about an eventual speedup achieved by doing this versus my old method. When I'm a bit further along I'll update this post with some results on that front.

    Thanks again for your time,
    Niels

  6. #6
    Registered User
    Join Date
    01-04-2004
    Location
    Netherlands
    Posts
    54

    Re: Putting strings in the 'next' position in an array without indexing manually

    Here is a video where you can see how this 'straight from array to file' is 4x quicker than the 'array to sheet to file' I used before.

    Always nice to get big speed improvements, even though it doesn't matter for actual time savings, it makes one feel clever (not necesarily rightfully so :P)

    https://youtu.be/bDvCyCHCzaE

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

    Re: Putting strings in the 'next' position in an array without indexing manually

    Use of ArrayList
    Please Login or Register  to view this content.
    Last edited by jindon; 02-23-2021 at 10:43 AM. Reason: Forgot to declare variable.

+ 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. Compare all characters from two strings and get position of mismatchs
    By MarieEve in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-20-2021, 05:43 PM
  2. [SOLVED] Sheets(1) question regarding indexing and position
    By terriertrip in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-11-2020, 07:30 PM
  3. [SOLVED] Array of lookups returning multiple strings if last two strings are empty
    By chriskay in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-14-2019, 07:35 AM
  4. Match different long strings with an array of short, summarizing strings
    By FKemps in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-24-2015, 12:55 AM
  5. Help searching an array of text strings for common strings
    By ABComp in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-23-2012, 11:19 PM
  6. Replies: 9
    Last Post: 12-21-2012, 04:18 AM
  7. Strings - Find position of the nth occurrence
    By efernandes67 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-06-2009, 05:46 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