+ Reply to Thread
Results 1 to 34 of 34

Replace string with vba

  1. #1
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Replace string with vba

    I have strings which vary like:

    1. 12 X 14 SCALE PAPER 21#(5 X1M/CS) 9927

    2. 12 X 18 DRY WAX21LB (1M/BOX)


    3. 12 X 14 DRY WAX SCALE 18LB (1M/BDL/5 BDL/CS) J


    I have to do maths on these which will be like this:

    1. 12*14*21
    2. 12*18*21
    3. 12*14*18

    I am having a hard time using REPLACE/SUBSTITUTE to solve this, because the spaces vary between different cells.

    Suggestions?
    Last edited by diyVBA; 03-16-2020 at 01:41 PM.

  2. #2
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    There is no underline in the actual string.
    The string always starts with the number and the third number always has # or LB at the end of the number which I want to use.
    Last edited by diyVBA; 03-16-2020 at 01:24 PM.

  3. #3
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Replace string with vba

    assuming data in column "A"
    see next code
    Please Login or Register  to view this content.
    - Battle without fear gives no glory - Just try

  4. #4
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    This is great, but its kinda complicated for me to understand to modify for my need (I would want the fixes to be done in some other column and not column A itself, i.e. after the code works I want column lets say G to have 12*14*21 not in column A itself).

    Also for the below string:

    12 X 14 SCALE PAPER 21#(5 X1M/CS) 9927

    this is what it does:

    12. 14x21x9927
    Last edited by diyVBA; 03-16-2020 at 02:43 PM.

  5. #5
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    It also gives a subscript out of range error and stops after row 86

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Replace string with vba

    Please attach a short Excel sample

  7. #7
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    Excel sample.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Replace string with vba

    The file was really welcome ...!
    Ajust DstCol to your need
    Pay attention to row 926
    Please Login or Register  to view this content.
    Last edited by PCI; 03-16-2020 at 03:20 PM.

  9. #9
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    works just perfectly!!

  10. #10
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Replace string with vba

    Did you double check row 926 ???

  11. #11
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    yes I mean other than that, these are exceptions which will happen in the dataset (2-3 times)

  12. #12
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    Lets say the string is 15 X 18 DRY WAX SCALE 21LB (2000/BOX)

    It would be better if I had 15 in one column, 18 in another column and 21 in another column for me to then multiply it

    I have to multiply 15x18x21

  13. #13
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Replace string with vba

    yes
    Good news
    Enjoy Excel

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

    Re: Replace string with vba

    Quote Originally Posted by diyVBA View Post
    It would be better if I had 15 in one column, 18 in another column and 21 in another column for me to then multiply it

    I have to multiply 15x18x21
    Here's how I read your problem.
    Extract numbers:
    1) From the beginning
    2) preceded by an X
    3) followed by LB or #
    So, for example
    row 87
    12 X 18 SCALE PAPER (17 LBS/BDL)
    The code extracts 12, 18 & 17
    If any problem, post back with your desired result.
    Please Login or Register  to view this content.
    Last edited by jindon; 03-16-2020 at 11:45 PM.

  15. #15
    Registered User
    Join Date
    02-25-2020
    Location
    HaNoi,VietNam
    MS-Off Ver
    2019
    Posts
    10

    Re: Replace string with vba

    You refer to try
    I am learning VBA. Therefore, Code has many problems. Please ignore it
    PHP Code: 
    Function My_Product(ByVal Text As StringChoise As Boolean)
        
    Dim i As IntegerAs Long
        Dim strTemp 
    As StringStreText As String
    Text 
    Split(Text"(")(0)
    Text Replace(Text"#""")
    Text Replace(Text"X"" ")
    Text Replace(Text" ""*")
    Text Replace(Text"/""*")
    For 
    1 To Len(Text)
        
    Select Case Asc(Mid(Texti1))
            Case 
    40 To 5794
                strTemp 
    strTemp Mid(Texti1)
        
    End Select
    Next i
    For Each Str In Split(strTemp"*")
        If 
    Str <> "" Then
            
    If 3 Then Exit For
            If 
    Not IsNumeric(StrThen
                Str 
    Val(StrReverse(Str)):   Str StrReverse(Val(Str))
                If 
    Str 0 Then GoTo 1
            End 
    If
            
    eText IIf(eText ""StreText "*" Str)
            
    1
        End 
    If
    1:
    Next
    If 2 Then
        
    If Choise False Then My_Product eText Else My_Product Evaluate(eText)
    End If
    End Function 
    Attached Files Attached Files
    Last edited by NhatChiLan; 03-17-2020 at 12:14 AM.

  16. #16
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Replace string with vba

    You may give this a try to see if this covers all your strings and find the measurements correctly.
    In the attached click the button called "Find Measurements & Calculate" to run the code.

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


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  17. #17
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Replace string with vba

    It would be better if I had 15 in one column, 18 in another column and 21 in another column for me to then multiply it

    I have to multiply 15x18x21
    Do you want 3 columns with value or final result ?
    How will be the display for row 926 ??
    Last edited by PCI; 03-17-2020 at 02:27 AM.

  18. #18
    Valued Forum Contributor
    Join Date
    12-22-2015
    Location
    HK
    MS-Off Ver
    2010
    Posts
    532

    Re: Replace string with vba

    try
    Please Login or Register  to view this content.

  19. #19
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    Hi Guys,

    I have a string which also looks like this:

    12 X 12 SCALE PAPER (10 LBS/BOX))

    I need to use the numbers next to # LB and LBS, sorry this wasn't stated by me in the original requirement.
    12*12*10

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

    Re: Replace string with vba

    Quote Originally Posted by diyVBA View Post
    12 X 12 SCALE PAPER (10 LBS/BOX))

    I need to use the numbers next to # LB and LBS, sorry this wasn't stated by me in the original requirement.
    12*12*10
    Have you tried my code in
    https://www.excelforum.com/excel-pro...ml#post5296324
    ?

  21. #21
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,649

    Re: Replace string with vba

    It seems you are not testing all the proposed codes?

  22. #22
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    I have just come to work, I will test all proposed solution. Thanks.

  23. #23
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    Sorry, testing it now.
    Last edited by diyVBA; 03-17-2020 at 12:09 PM.

  24. #24
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    I just tested your code and seems to do what I wanted. This is perfect.

  25. #25
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    jindon,

    Suppose my column is now in F instead of A and I want the results to be pasted from Column Q, so Q, R and S.
    How do I modify your code?
    I am a beginner with VBA, so everything is complex for me.
    Last edited by jeffreybrown; 03-17-2020 at 12:48 PM. Reason: Please do not use full quotes!

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

    Re: Replace string with vba

    Try change to
    Please Login or Register  to view this content.

  27. #27
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    jindon,

    This is just perfect, thanks!!
    If I want to divide the product with 432, can I do this -- >

    =product(rc[-3]:rc[-1])/(432)
    Last edited by jeffreybrown; 03-17-2020 at 12:49 PM. Reason: Please do not use full quotes!

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

    Re: Replace string with vba

    You can just change that line to
    Please Login or Register  to view this content.
    Please do not quote full post, the forum doesn't like it...-

  29. #29
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    Ok, thanks! It works.

  30. #30
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    Quote Originally Posted by PCI View Post
    Do you want 3 columns with value or final result ?
    How will be the display for row 926 ??
    926 is an exception.

  31. #31
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Replace string with vba

    Hi diyVB,

    Please don't quote whole posts -- it's just clutter.*If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding.
    HTH
    Regards, Jeff

  32. #32
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    Noted, thanks!

  33. #33
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    @jindon

    I have the same requirement but for a different set of strings, I need to pick the numbers between 2 Xs and the string has HOT sometimes and sometimes it doesn't, but prime need is to pick the numbers before and after the Xs:

    Strings can be like:

    10.5X2X4.5 WIND.BAG PRT.2COL .WIND.1/2 FRONT/1/2 GOSSET (1M)
    11 X 3 X 16 PIZZA BAGS PRINTED (500/B0X)
    11X4X16 BROWN BREAD BAG PRINTED (1000/BOX)
    340113-4.5" X 1.88" X 16.625" SUB PLAIN BAG (1000/BOX)
    4 3/4 X 3 X 10 BAGS GP PRT. AROUCH V BOTTOM (1000/BOX)
    5 1/4 X 3 1/2 X 12 BAR-B-Q BAGS (500/B0X
    5.75X13.5X4.5 BROWN WAX BAG PRT.MEUNERIE URBAINE(1M/BOX0
    6X3/4X63/4 SAC SANDWICH CIRE (1M/BOX)
    8.75"X 2"X11" WHITE PIZZA BAG PRT. (1M)
    7.5" X 2" X 8" FOIL HAMBURGER BAG (1000/BOX)
    FOIL HAMBURGER BAGS 6X3/4X6.5 (1M/BOX)
    FOIL HOT DOG 7X1.5X5.5 (1000/BOX)
    FOIL HOT DOG BAG 7X1.5X5.5 (1000/BOX)
    MANCHON HOT DOG GENERIQUE 3.5X1,25X7,5(1000/BOX)
    POLYBAGS .002; 9x12


    Seperate the numbers betweens the Xs and put in a different columns and then use formaula =product(rc[-3]:rc[-1]/432)

    10.5X2X4.5 WIND.BAG PRT.2COL .WIND.1/2 FRONT/1/2 GOSSET (1M) ---> ((10.5*2)*(2*2)*(4.5*2))/432

    11 X 3 X 16 PIZZA BAGS PRINTED (500/B0X) ---> ((11*2)*(3*2)*(16*2))/432

    11X4X16 BROWN BREAD BAG PRINTED (1000/BOX) ---> ((11*2)*(4*2)*(16*2))/432

    340113-4.5" X 1.88" X 16.625" SUB PLAIN BAG (1000/BOX) ---> ((4.5*2)*(1.88*2)*(16.625*2))/432

    4 3/4 X 3 X 10 BAGS GP PRT. AROUCH V BOTTOM (1000/BOX) ---> ((4.75*2)*(3*2)*(10*2))/432

    5 1/4 X 3 1/2 X 12 BAR-B-Q BAGS (500/B0X ---> ((5.25*2)*(3.5*2)*(12*2))/432

    5.75X13.5X4.5 BROWN WAX BAG PRT.MEUNERIE URBAINE(1M/BOX0 ---> ((5.75*2)*(13.5*2)*(4.5*2))/432

    6X3/4X63/4 SAC SANDWICH CIRE (1M/BOX) ---> ((6*2)*(0.75*2)*(6.75*2))/432

    8.75"X 2"X11" WHITE PIZZA BAG PRT. (1M) ---> ((8.75*2)*(2*2)*(11*2))/432

    7.5" X 2" X 8" FOIL HAMBURGER BAG (1000/BOX) ---> ((7.5*2)*(2*2)*(8*2))/432

    FOIL HAMBURGER BAGS 6X3/4X6.5 (1M/BOX) ---> ((6*2)*(0.75*2)*(6.5*2))/432

    FOIL HOT DOG 7X1.5X5.5 (1000/BOX) ---> ((7*2)*(1.5*2)*(5.5*2))/432

    FOIL HOT DOG BAG 7X1.5X5.5 (1000/BOX) ---> ((7*2)*(1.5*2)*(5.5*2))/432

    MANCHON HOT DOG GENERIQUE 3.5X1,25X7,5(1000/BOX) ---> ((3.5*2)*(1.25*2)*(7.5*2))/432

    POLYBAGS .002; 9x12 ---> ((0.002*2)*(9*2)*(12*2))/432

    These are all the different types.

  34. #34
    Forum Contributor
    Join Date
    12-20-2019
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    116

    Re: Replace string with vba

    How do I store the values of detected pattern into variables?

+ 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] Replace Characters to Left of a String with Another String/Character
    By stevoDE in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-06-2019, 02:43 AM
  2. how do I replace a string within a string in an array
    By dschmitt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-14-2015, 02:30 AM
  3. [SOLVED] Need help with REPLACE function to replace HEX A0 with HEX 20 in string
    By flyboy54 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-26-2013, 12:52 PM
  4. [SOLVED] Replace string with another string from another worksheet (like decode)
    By sugee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2013, 02:05 AM
  5. [SOLVED] Find a string by VBA Regular Expression and replace a part of that string
    By taps in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-25-2013, 11:08 AM
  6. How to Assign Value to a String & Replace String With Assigned Value?
    By xiaokang1982 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2012, 06:01 PM
  7. Replies: 3
    Last Post: 08-10-2006, 07:20 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