+ Reply to Thread
Results 1 to 15 of 15

Generating a string from other cells/CSV file using a for loop

  1. #1
    Registered User
    Join Date
    06-27-2019
    Location
    Minnesota, USA
    MS-Off Ver
    Office 2016
    Posts
    7

    Generating a string from other cells/CSV file using a for loop

    Hello,

    I've been assigned a project that is fairly simple. Basically, I have a sheet with lots of data for different aftermarket products and I need to combine data from different cells to create one long string, or "title". I have written a pseudocode for what I want to be done, but I am very new to excel programming.

    I have two different files: one excel workbook file and a CSV file. The workbook file contains lots of data, but I only need to use data from two columns. One of the columns has a short, default title of basically what the product is, and the other column has different brands and part numbers that the product replaces, with each brand/part number separated by an HTML break tag (for example, one of these cells may look like this: "Cub Cadet #######<br>John Deere #######<br>Toro ######" - note that the part numbers may contain letters and numbers). The CSV file contains a column of different brand names.

    The task is simple: I want to write a code that searches looks for each brand from the CSV file in the cell containing brand and part number information, and if found, take the brand name and part number and append it to the cell with the default title - all while the total number of characters is less than or equal to 128. So if the default title cell is "Lawn Mower Blade", I want the end result to be something like "Lawn Mower Blade Replaces John Deere ####### Cub Cadet ####### Toro ######".

    Here is the pseudocode for what I'd like to happen (or of course if there is a better/easier way of doing it, I'd much appreciate it!)


    For i = 1 To 36 (36 being the total number of brand names on the list) {

    Find "Brand i" in the 'Replacement Brand/Part Number' cell

    Use extraction formula (shown below), taking from starting position of "brand i" and ending at starting position of next "<br>"

    IF: length of output of extraction formula + length of everything before < 129, add to title cell

    ELSE: break loop }

    *Extraction formula: =" "&MID(O2,FIND("Brand i",O2),FIND("<br>",O2,FIND("Brand i",O2))-FIND("Brand i",O2)) -- This is the formula I use to extract the brand and part number following it; column O contains the cells with brand/part replacement info.


    The loop is applied to each row/product. I would really appreciate any help given. Again, I'm pretty new to excel programming any new or better code than mine would be much appreciated too - thank you in advance!

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Generating a string from other cells/CSV file using a for loop

    Attach sample source files (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    06-27-2019
    Location
    Minnesota, USA
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: Generating a string from other cells/CSV file using a for loop

    I've attached a sample workbook. I didn't attach the CSV, but it just has a list of brand names listed by priority (for example, John Deere is at the top because the first brand/number we want is John Deere).
    Attached Files Attached Files

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Generating a string from other cells/CSV file using a for loop

    Humour me, and attach en example of the CSV file too.

  5. #5
    Registered User
    Join Date
    06-27-2019
    Location
    Minnesota, USA
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: Generating a string from other cells/CSV file using a for loop

    Here's a sample of the CSV file
    Attached Files Attached Files

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

    Re: Generating a string from other cells/CSV file using a for loop

    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Generating a string from other cells/CSV file using a for loop

    Or you could use Power Query. Format your source (before) data as a table, then use:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    06-27-2019
    Location
    Minnesota, USA
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: Generating a string from other cells/CSV file using a for loop

    The first code has the title formatted correctly, but I need the brands listed in the order from the CSV file - I also need only the brands from the CSV, meaning if there is a brand in the replacement info cell that isn't on the CSV, it shouldn't be put in the title. It also doesn't look like it takes the 128 characters max into account. And I don't have Power Query to try the other solution.

  9. #9
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Generating a string from other cells/CSV file using a for loop

    Your profile says you're using Excel 2016 - Power Query is built in (Get & Transform Data)

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

    Re: Generating a string from other cells/CSV file using a for loop

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    06-27-2019
    Location
    Minnesota, USA
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: Generating a string from other cells/CSV file using a for loop

    Quote Originally Posted by jindon View Post
    Please Login or Register  to view this content.
    This is very close to how I need it. However, when I use this code, the title cell looks like this:


    Replacement Mower Belt Replaces Cub Cadet 132013-C1 464351-R1 Craftsman 501089 Ariens 07211700 07211900 Ariens 07211700 Ariens 07211900 Craftsman 501089 Cub Cadet 132013-C1 Cub Cadet 464351-R1


    The first half is perfect, but the titles always end with redundant brands and numbers - always after a double space. I need to get rid of this information. It should look like this (with the red underlined text deleted):

    Replacement Mower Belt Replaces Cub Cadet 132013-C1 464351-R1 Craftsman 501089 Ariens 07211700 07211900 Ariens 07211700 Ariens 07211900 Craftsman 501089 Cub Cadet 132013-C1 Cub Cadet 464351-R1

    Also does this code take the 128 characters into account?

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

    Re: Generating a string from other cells/CSV file using a for loop

    1) Don't quote full post unless you really need it to, forum doesn't like it.

    2) change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    3) What do you want to do when it exceeds 128 characters?

  13. #13
    Registered User
    Join Date
    06-27-2019
    Location
    Minnesota, USA
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: Generating a string from other cells/CSV file using a for loop

    Thanks so much! When the title exceeds 128 characters, I want to 'undo' the last brand/number that was put in.

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

    Re: Generating a string from other cells/CSV file using a for loop

    See if this works as you expected.
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    06-27-2019
    Location
    Minnesota, USA
    MS-Off Ver
    Office 2016
    Posts
    7

    Re: Generating a string from other cells/CSV file using a for loop

    Thanks you so much! This does just what I need. Thanks for all your help!

+ 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. [SOLVED] Loop trough a text file and find each instance of an string
    By colddeck84 in forum Excel Programming / VBA / Macros
    Replies: 34
    Last Post: 10-10-2017, 10:37 AM
  2. [SOLVED] Populate Cells below based on length of string by a loop?
    By Pablos690_1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-30-2015, 07:42 AM
  3. Generating random number string based on Alphanumeric String
    By ridemeve in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-15-2014, 04:56 PM
  4. [SOLVED] Generating Random String from Text in Cells
    By abbeycrombie in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 12-18-2013, 12:09 PM
  5. [SOLVED] Loop for search a string, split data, save as new file.
    By johnch in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-29-2012, 01:57 AM
  6. [SOLVED] Loop through cells and create string
    By pablowilks in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-26-2012, 12:02 PM
  7. Generating a list of reports as HTML in a .txt file depending on cells
    By john_london in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-24-2012, 06:01 AM

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