+ Reply to Thread
Results 1 to 17 of 17

string manipulation, move letter back in the string

  1. #1
    Registered User
    Join Date
    12-17-2015
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    21

    string manipulation, move letter back in the string

    I am having a bit of trouble coming up with a working vba code to move letters back in a string with some constraints and I would appreciate some help

    String KEY has fixes size of letters ie "ABCDEFGHIJ"
    There is another string LOCK that contains letters that are imovable in the string KEY. ie "AC"
    Finally I have the Letter string that contains the letter that is moving ie "J"

    What I am doing is that I am moving the Letter up the key string and once it reaches the end of it it goes back to the begining of the KEY string ie to position 1.

    It could be done with a simple code Result=right(KEY,len(key)-1) & LETTER

    The problem is that the LOCK string contains letters that are immovable in the KEY string, which means that the letters A will always occupy the 1st spot in the Key string and C the third.

    so for ie when trying to move J to the beginning of the string the result instead of being "JABCDEFGHI" should be "AJCDEFGHI"

    if the LOCK string was "AB" then the result would be "ABJCDEFGHI"

    if LOCK was "BC" then the solution is JBCADEFGH"

    Any help would be appreciated with some code to achieve this.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: string manipulation, move letter back in the string

    A
    B
    C
    D
    1
    Input
    ABCDEFGHIJ
    2
    Fix
    Move
    Result
    3
    AC J AJCBDEFGHI C3: =hj(C$1, A3, B3)
    4
    AB J ABJCDEFGHI
    5
    ACDE I AICDEBFGHJ


    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    12-17-2015
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    21

    Re: string manipulation, move letter back in the string

    Its brilliant.
    Thank you Shg.

    Excellent code because I will learn from it.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: string manipulation, move letter back in the string

    You're welcome.

  5. #5
    Registered User
    Join Date
    12-17-2015
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    21

    Re: string manipulation, move letter back in the string

    Shg,

    Is it possible to modify the code so that it will work regardless of where Smov is in the sInp string?

    It seems that it only works if Smov is in a higher position than the Sfix letters.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: string manipulation, move letter back in the string

    For example, ...?

  7. #7
    Registered User
    Join Date
    12-17-2015
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    21

    Re: string manipulation, move letter back in the string

    To make it work regardeless of where the Letter J is?

    If the letter J is not already at the last place in the string then if I try to move it by one it will move N spots according to the sFix string.

    Suppose Sinp is AJCDEFGHIB

    with Sfix as AC then moving J one up would make HJ report ADCJEFGHI (Because C is locked in the string)

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: string manipulation, move letter back in the string

    So it's supposed to move sMov one position to the left, not to the front of the string?

  9. #9
    Registered User
    Join Date
    12-17-2015
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    21

    Re: string manipulation, move letter back in the string

    sMov will always move to the right. Its just that once it reaches the last position of sInp it needs to move to position 1 in sInp based on sFix.
    Last edited by hammerjoe; 04-19-2016 at 05:57 PM.

  10. #10
    Registered User
    Join Date
    12-17-2015
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    21

    Re: string manipulation, move letter back in the string

    Shg,

    I have been playing with your code and it seems to work but at some point it breaks.
    For ie
    Sinp="CEFAHGIDB"
    sFix="AEDCIF"
    sMov="B"

    Running thru the code hj gets as result "CBFEHGAID"

    This is wrong. The correct result should be "CEFABHIDG"

    Do you know why the code breaks this way and can you provide a fix?
    Much appreciated.

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: string manipulation, move letter back in the string

    Quote Originally Posted by hammerjoe View Post
    Suppose Sinp is AJCDEFGHIB

    with Sfix as AC then moving J one up would make HJ report ADCJEFGHI (Because C is locked in the string)
    Where's the B go?

    Sinp="CEFAHGIDB"
    sFix="AEDCIF"
    sMov="B"

    This is wrong. The correct result should be "CEFABHIDG"
    sFix must have its characters in the order in which they appear in sInp.

    A
    B
    C
    D
    1
    Fix
    Move
    ABCDEFGHIJ
    2
    AC B ADCBEFGHIJ C2: =ShiftChrRt(C1, A2, B2)
    3
    AC B ADCEBFGHIJ
    4
    AC B ADCEFBGHIJ
    5
    AC B ADCEFGBHIJ
    6
    AC B ADCEFGHBIJ
    7
    AC B ADCEFGHIBJ
    8
    AC B ADCEFGHIJB
    9
    AC B ABCDEFGHIJ
    10
    11
    Fix
    Move
    AJCDEFGHIB
    12
    AC J ADCJEFGHIB C12: =ShiftChrRt(C11, A12, B12)
    13
    14
    Fix
    Move
    CEFAHGIDB
    15
    CEFAID B CEFABHIDG C15: =ShiftChrRt(C14, A15, B15)


    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    12-17-2015
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    21

    Re: string manipulation, move letter back in the string

    One again you come thru with nice and simple code to do the task.

    There is just one wrinkle in this shirt sFix needing to be in order they appear in sInp.

    Isnt there a way around that?
    I am thinking if the sFix could simply be sorted to the right order at the beginning of the function?
    The order the letters are in sFix doesnt really matter for sInp. It merely tells that the letter in sInp cant move.

    What do you think?

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: string manipulation, move letter back in the string

    Why don't you have a go and post back if you get stuck?

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: string manipulation, move letter back in the string

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    12-17-2015
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    21

    Re: string manipulation, move letter back in the string

    SHG I really appreciate your help and I want to say a big thank you.

    I am frustrated with my code and I was wondering if you could spare a few minutes to take a look at my sheet and perhaps guide me?

    On paper it seems a simple thing but right now my code is stuck in a loop and doesnt find any solutions.

    Please excuse the mess in the code but I have been trying different things and have not cleaned it up.

    TEMP5 is the main SUB that runs the whole thing.
    What this code is supposed to do is to parse all the lines in the F column until it reaches an empty cell and then try to replace the letters with digits that will solve the formulas.

    Can you take a look and see where I am going wrong with it?
    Thanks
    Attached Files Attached Files

  16. #16
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: string manipulation, move letter back in the string

    I have no idea what all that means.

  17. #17
    Registered User
    Join Date
    12-17-2015
    Location
    Canada
    MS-Off Ver
    2010
    Posts
    21

    Re: string manipulation, move letter back in the string

    Quote Originally Posted by shg View Post
    I have no idea what all that means.
    What do you need help with?

+ 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] String Manipulation
    By tbmart2 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-17-2014, 07:34 PM
  2. Replies: 12
    Last Post: 03-20-2013, 05:46 PM
  3. String manipulation
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-16-2009, 01:53 PM
  4. VBA String Manipulation
    By dzurawell in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-02-2009, 12:06 PM
  5. String Manipulation?
    By Sean Anderson in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-26-2007, 09:18 PM
  6. Importing Long String - String Manipulation (INVRPT) (EDI EANCOM 96a)
    By Brian in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-09-2006, 11:45 AM
  7. Importing Long String - String Manipulation (EDI EANCOM 96a)
    By Brian in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-09-2006, 08:30 AM

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