+ Reply to Thread
Results 1 to 31 of 31

Adding lines ABOVE based on number of commas used.

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Adding lines ABOVE based on number of commas used.

    Hi there,

    Not sure if this is possible, but I am looking for a way to have Excel automatically copy and paste ABOVE a record line based on the number of ","(comma). Please see the attached file.

    In the "orig" worksheet, you see 3 columns of data. The number of comma is found ONLY in the "Notes" section.

    What I would like to do is to use some kind of macro to generate the data in the "desired output" worksheet. The only difference is that new lines are added ABOVE each patient record based on the number of commas used in the "Notes" column. For example, for patient "23", there are 3 commas found in the "Notes" column, so I need to add 3 lines ABOVE the original "23". If there is no comma, then nothing needs to be done.

    I'm not sure how the syntax goes in VBA, but can this be done?

    Anyone able to assist?

    Thanks
    Attached Files Attached Files

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding lines ABOVE based on number of commas used.

    Hello LikeSeeker,

    The macro below has been added to a button on the "Orig" worksheet in row 1. This add a new row for each comma found plus one. One is added to the comma count since there are n + 1 items separated by the commas.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    Hi Leigh,

    Thanks very for taking the time coding it.

    It seems the entire code revolves around the "RegExp", and I am always intrigued by it

    Please Login or Register  to view this content.
    Is it a free object that we can use in VBA library?

    and what does the
    Please Login or Register  to view this content.
    do? It goes back one row?

    What does
    Please Login or Register  to view this content.
    do? Specifically, what are the indexes 1 and 3 referring to?
    Last edited by Lifeseeker; 06-18-2012 at 02:59 PM. Reason: specifying questions

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding lines ABOVE based on number of commas used.

    Hello LifeSeeker,

    The Regular Expressions object library was added to VBScript in 2000. This powerful pattern matching script is used by several other languages to parse web data. Languages like Java, PHP, and ASP to name a few. The down sides are the lack of explicit feature documentation and the strange syntax.

    The backslash "\" is used to stop Regular Expressions from interpreting a character in a string that has special meaning in Regular Expressions. In this case, that character is a comma. The Global property is set to true to match all patterns in the text. By default this is set to False, which then only searches for the first match in the text. The search pattern is now set to look for commas in the text.

    The Regular Expression method Test checks if the text contains any matches. This is useful for 2 reasons. First is flow control. If there is pattern then deal with it and if not continue on. Second, is the Execute method will throw an error if there are no matches.

    The Execute method will return a collection of all the matches found in the text along with any Submatches. I won't go into detail about these objects because they depend on how the expressions are written. The Matches index starts at zero. The object has a Count property to which tells us how many matches were found.

    The Step function in a For loop is used to control the incrementing or decrementing of the control index value. The default is to increment by a value of +1. This can be changed to any value you want to use, positive or negative. The control index will start with the initial value and be increased or decreased by the Step value.

  5. #5
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    Hi Leith,

    What does the word "data" in the
    Please Login or Register  to view this content.
    refers to?

    And what are the indexes in the bracket referring to? Are they rows and columns?

    Sorry I still seem to have a bit of trouble understanding the use of "Step", especially with a "-" sign. If it is negative one, then it is counting backwards?

    Thanks

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding lines ABOVE based on number of commas used.

    Hello LifeSeeker,

    The variable Data is Variant that holds the values of the cells in the row. When you only need to work the value of cell and not any of the formatting, it is faster to copy the cells values into an Variant as a 2-D array (Rows and Columns). So the indices refer to the rows and columns in the array called Data.

    You are correct about the Step function. It is counting down by one.

  7. #7
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    Thanks Leigh for explaining. I need to familiarize myself with the Regular Expression coz it can be a very powerful scripting language.

    Can you also explain perhaps with greater detail the following codeline?

    Please Login or Register  to view this content.
    How does Offset come into play? 1 is row and 0 is column? It is looking for the intersection of two ranges? I have seen that quite alot in problems that other people post, but never quite understand it fully.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding lines ABOVE based on number of commas used.

    Hello Likeseeker,

    The object variable Rng is first assigned to the contiguous cells on the worksheet starting with "A1". Since row 1 is header row, it can be excluded from the remaining range.

    Lets say the range is A1:G10. Offsetting the range by 1 row down produces A2:G11. Now, row 11 has no data and needs to removed. To do that Intersect is used. This returns a Range that is the result of where 2 or more Ranges have cells in common. In this case, the Range result is A2:G10.

  9. #9
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    Thanks.

    I am trying to understand the code step by step:

    Please Login or Register  to view this content.

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding lines ABOVE based on number of commas used.

    Hello Lifeseeker,

    Here is the loop with comments added.
    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    In the line
    Please Login or Register  to view this content.
    1 is the row and 3 is the column? If so, they are not looping variables?

    and in
    Please Login or Register  to view this content.
    You said the number of rows equals the comma count plus 1, but why is there a "-1" at the end?

    Though the explanation is very clear, what is the difference between:

    Please Login or Register  to view this content.

    Thanks

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding lines ABOVE based on number of commas used.

    Hello Lifeseeker,

    The variant array Data is filled with the cell values from a single row in the range (variable Rng). So, the element indices don't change. Data(1, 3) refers to the third column of the current row in the loop.

    The total number of rows, the original plus the new rows, does equal the number of commas plus one. The line below calculates the number of new lines to be inserted.

    Let's say [n]Rng[/b] is cells "A2:C10". The size of the array will be 9 by 3. If I want to know which row Rng(1, 3) refers to on the worksheet then some conversion is needed. Rng.Rows(1) returns the row index for Rng which is 1. Rng.Rows(1).Row returns the row index of the Parent of Rng which is the worksheet which is 2.

  13. #13
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    Hi,

    In
    Please Login or Register  to view this content.
    n is the current row, and we are adding Commacount then minus 1. Why are we subtracting 1? We add rows based on the # of commas used, right?


    Sorry I still seem to struggle with this one:

    Let's say [n]Rng[/b] is cells "A2:C10". The size of the array will be 9 by 3. If I want to know which row Rng(1, 3) refers to on the worksheet then some conversion is needed. Rng.Rows(1) returns the row index for Rng which is 1. Rng.Rows(1).Row returns the row index of the Parent of Rng which is the worksheet which is 2.

    What is the parent of Rng? the original worksheet?

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding lines ABOVE based on number of commas used.

    Hello Lifeseeker,

    Let's say the Worksheet row (n) = 2 , and the comma count (c) is 2 (new lines are to be added).

    Adding 2 rows above the current worksheet row 2 will push all the rows below row 2 down 2 rows. What was originally Row 2 is now row 4. To calculate the rows on the worksheet we use the formula:
    Please Login or Register  to view this content.
    The worksheet the range is on is the parent of that range.

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

    Re: Adding lines ABOVE based on number of commas used.

    This should do exactly the same as your desired result.
    Please Login or Register  to view this content.

  16. #16
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    Hi,

    In
    Please Login or Register  to view this content.
    shouldn't it be 2+ 2 - 1 = 3 or?

    I think this is where the confusion was.

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

    Re: Adding lines ABOVE based on number of commas used.

    Quote Originally Posted by Lifeseeker View Post
    Hi,

    In
    Please Login or Register  to view this content.
    shouldn't it be 2+ 2 - 1 = 3 or?

    I think this is where the confusion was.
    See my post.
    http://www.excelforum.com/excel-prog...=1#post2831669

  18. #18
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Adding lines ABOVE based on number of commas used.

    Hello Lifeseeker,

    Yes, you are correct. It should be 3, not 4. Two new lines will added at rows 2 and 3. The original row 2 has now been moved to row 4.

  19. #19
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    hi Jindon,

    I appreciate your help on this as well. I would like to understand Leigh's solution first then come back to yours.

    Thanks

  20. #20
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    Hi Leigh,

    In
    Please Login or Register  to view this content.
    The # of rows equals the comma count plus, I don't see any problem with this statement.

    However, the # of rows equals the comma count plus, but then there is a - 1. Why are we subtracting 1 then insert the row? I still don't seem to see it clearly...

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

    Re: Adding lines ABOVE based on number of commas used.

    Lifeseeker,

    No problem.

    Just tell you that most of you learned about Regular Expressions in this thread is Correct, but not all.

    Just let me know when you are ready.

  22. #22
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    Hi Jindon,

    My questions can be found in the following code:

    Please Login or Register  to view this content.
    Thanks

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

    Re: Adding lines ABOVE based on number of commas used.

    Which part?

  24. #24
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    my questions are found in the code, comment section.

    2 as in the .Resize(,2).value and .Resize(x+1,2).value.

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

    Re: Adding lines ABOVE based on number of commas used.

    1) Meta-characters in regular expressions are

    ^  $  ?  *  +  .  |  {  }  \  [  ]  (  )

    each one of them has a special meaning.
    And if you need to use those character as its original string in pattern, you will need to add an escape character that is back slash "\" in front of them.
    e.g
    \( \+ etc.
    Obviously comma is not, so you don't need "\".

    .Pattern ",(?!$)"
    (?! means exclude the character that is in front of it, that followed by the character in the bracket.
    $ means end of the line.
    So it means, all comma excdept at the end.

    So it only matches first 2 comma from "1,2,3,"

    Does this help?
    Last edited by jindon; 06-22-2012 at 11:00 AM.

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

    Re: Adding lines ABOVE based on number of commas used.

    Quote Originally Posted by Lifeseeker View Post
    my questions are found in the code, comment section.

    2 as in the .Resize(,2).value and .Resize(x+1,2).value.
    Ahhh

    That part is coying Col.A:B value to Col.A:B of newly added row(s).

  27. #27
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    Hi Jindon,

    Thanks for explaining the Regular Expression part. However, my questions are below:

    if you don't mind explaining this part of the code for me..

    Please Login or Register  to view this content.

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

    Re: Adding lines ABOVE based on number of commas used.

    When it needs to add new row(s), it stores Col.A-B value of origin to variable temp.

    And output it to Col.A-B of new rows added.

    If you step through the code, you will find what it does.

    While you are in VBE:

    Click on somewhere on the code and hit F8.

    As you hit F8, the code will execute line by line.
    If you shrink the VBE window, you can see what is going on the sheet.

    And more, if you open Local Window (goto View - LocalWindow).
    You can also see all the variables in the LocalWindow.
    Last edited by jindon; 06-22-2012 at 11:13 AM.

  29. #29
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    Hi Jindon,

    Very helpful tips. I stepped through the code and I believe I just have the following question:

    In the code:

    Please Login or Register  to view this content.
    . Why are we using the resize() as part of insert?

    Thanks

    Please Login or Register  to view this content.

  30. #30
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Adding lines ABOVE based on number of commas used.

    you still there?

    bump

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

    Re: Adding lines ABOVE based on number of commas used.

    Resize property has 2 parameter.

    Resize(RowSize, ColumnSize)
    Default value of the parameter has 1.
    So when one of the parameter is missing, it gives you 1 to the missing parameter.

    Resize(5) = Resize(5, 1)
    Resize(, 5) = Resize(1, 5)

    Then your problem

    For example.

    If i = 3 and x = 4,

    Rows(3).Resze(4 + 1, 1)
    means 5 rows from row 3
    Because row(3) is inculded, it needs to add 1 to x (number of comma)

    that is equivqlent to
    Rows("3:7")

    Does this help?

+ Reply to 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