+ Reply to Thread
Results 1 to 16 of 16

Copying a number from a string

  1. #1
    Registered User
    Join Date
    02-19-2019
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    MS Office 2010
    Posts
    24

    Copying a number from a string

    Hi all,

    I am trying (in VBA) to copy a value from a string before a specific word (Share), however there are some difficulties.

    1. The string can vary somewhat, for example 2 scenario's:

    John(10)(File A, 12341234 Share)
    Frank B Share (99)(Document, John, 312 Share, Saved)

    2. The value is not always the same digits. In the above cases, 12341234 and 312, but any length (up to .. say 20 digits) is possible

    3. The word Share might actually appear in the front of the string (although unlikely), both separately or as part of another word / name (e.g. Frank B Share / Frank B Shared)

    4. Most unlikely, but not inevitable, the word Share might not appear at all. In this case, it should skip this command all together and the sequential commands (I have piece of code included "sht.range("A1").value = LFoundValue"). In this case I will manually insert a value in cell A1 (after the macro has run). I would need to include a textbox at the end of my complete code though, to inform that cell A1 has been kept empty and manual work needs to be done. Please note that the next time running this macro, it should check if there is already a number in sht.range("A1"). If there is, nothing needs to be done (because I manually added the number).

    Now I thought of finding the position of the comma "," and find the position of the spaces after the comma, but there might actually be more than 1. The same goes for the word "Share". This makes it more complicated.

    Hopefully one of you has experience with a similar challenge.

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

    Re: Copying a number from a string

    Can you just upload a small example file with possible variation of data and the result that you want?

    So that we can work on it.

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,617

    Re: Copying a number from a string

    Probably the most appropriate approach would be by using regular expressions, but let me show more basic way - just with searching the string.

    Try such function:
    Please Login or Register  to view this content.
    To test how it works (application to your code (not shown) is on you :-))
    Write some sample texts in K1:K6, lets say
    John(10)(File A, 12341234 Share)*
    Frank B Share (99)(Document, John, 312 Share, Saved)
    Peter
    Share
    asia Share
    1234 Share mare lokus
    and run such macro:
    Please Login or Register  to view this content.
    Best Regards,

    Kaper

  4. #4
    Registered User
    Join Date
    02-19-2019
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    MS Office 2010
    Posts
    24

    Re: Copying a number from a string

    Hi Kaper,

    Thank you for the quick reply!

    This might sound strange, but I never learned to work with functions. I only write code in lengthy sub(), thus I wonder if it is possible to incorporate it in a sub (because I am not yet familiar with functions).

    I just now realise my explanation was not detailed enough, please see below my old code. This relied on having exactly 2 commas in the string, which I found was not reliable, as other files had 1 or 3 (or more) commas.

    Please Login or Register  to view this content.
    Additionally, this does not incorporate my other scenario's as in my topic start post.

    Hope you can help with a sub() version.

    Sidenote, do you believe I should get familiar with functions?
    Last edited by Excel-VBA; 07-24-2019 at 10:18 AM.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,755

    Re: Copying a number from a string

    Administrative Note:

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  6. #6
    Registered User
    Join Date
    02-19-2019
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    MS Office 2010
    Posts
    24

    Re: Copying a number from a string

    Quote Originally Posted by jindon View Post
    Can you just upload a small example file with possible variation of data and the result that you want?

    So that we can work on it.
    Hi jindon,

    Sure thing. Please see attached file.

    Side note:

    1. The string is always at another tab in position A1 (In my real model sht3.range("A1")
    2. The value I would like to copy from the string is always copied to the same cell (in my real model sht.range("F33") (thus a different sheet than the sht3)
    3. The possible other lines I provide in the attached sheet, could and would thus appear in A1, but now are in A8-10 just to provide you with various string options

    Thanks
    Attached Files Attached Files

  7. #7
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,617

    Re: Copying a number from a string

    edited - removed answer until moderator's request is fulfilled

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

    Re: Copying a number from a string

    For the data in your attachment.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    02-19-2019
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    MS Office 2010
    Posts
    24

    Re: Copying a number from a string

    Quote Originally Posted by jindon View Post
    For the data in your attachment.
    Please Login or Register  to view this content.
    Hi jindon,

    That looks very impressive, but very hard to read (for me).

    Would it be rude to ask for a bit of explanation as to how to read it? That way I can learn how to work with the code.

    If it is too difficult, nevermind I will try playing around with it.

  10. #10
    Registered User
    Join Date
    02-19-2019
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    MS Office 2010
    Posts
    24

    Re: Copying a number from a string

    Quote Originally Posted by Kaper View Post
    edited - removed answer until moderator's request is fulfilled
    Apologies, I have changed my reply to include the code brackets.

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

    Re: Copying a number from a string

    Used Regular Expressions, so google with the key words like VBA Regexp meta character. etc for the details.

    It finds the pattern matching.
    Please Login or Register  to view this content.
    means
    ( open bracket
    some string other than a comma and followed by a comma appears none or many and (block of numbers) followed by none or many string followed by Share and none or many string other than a closing bracket followed by a closing bracket.

    When it finds the pattern to match within a string, you can retrieve the target part using submatches for backreference.

    Only a brief explanation so, find the web that explains in detail.

  12. #12
    Registered User
    Join Date
    02-19-2019
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    MS Office 2010
    Posts
    24

    Re: Copying a number from a string

    Quote Originally Posted by Kaper View Post
    edited - removed answer until moderator's request is fulfilled
    Hi Kaper,

    Would you be able - now that I fulfilled the moderator's request - to repost your solution?

    I would like to use it, as to learn from it, if you will.

    That would be much appreciated.

    Quote Originally Posted by jindon View Post
    Used Regular Expressions, so google with the key words like VBA Regexp meta character. etc for the details.

    It finds the pattern matching.
    Please Login or Register  to view this content.
    means
    ( open bracket
    some string other than a comma and followed by a comma appears none or many and (block of numbers) followed by none or many string followed by Share and none or many string other than a closing bracket followed by a closing bracket.

    When it finds the pattern to match within a string, you can retrieve the target part using submatches for backreference.

    Only a brief explanation so, find the web that explains in detail.
    Thank you jindon!

    I will google for VBA Regexp and hopefully be able to use the coding myself.

    Much appreciated

  13. #13
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,617

    Re: Copying a number from a string

    Sure,

    But as I wrote in post #3 I also think that regular expressions could be the best way to do it. Anyway, the text deleted as per moderator request (now fulfilled):

    Let's start with sidenote: yes

    As for other issues: Having nor code neither workbook the answers would be very general. the same is with thje one I already gave: You said "to copy a value from a string before a specific word". But is this string in a cell? or may be already in a variable? or in some dataset?

    Going back to sidequestion - having a function allows you treat all above cases the same way: as with buitlt-in functions.

    You can use:
    Please Login or Register  to view this content.
    but also
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    etc.

    You use InStr function the same way.


    As for not answered parts: note that I focused on first 3 and some if 4th point - up to "the word Share might not appear at all. " then you can do whatever you want with the result of function - for instance check if it is a number.

    And finally - yes, you can of course put all the code inside main procedure (sub).

    It could look like (just guessing, as you published no sample file. But now, before publishing, try the codes from this and previous post):

    Please Login or Register  to view this content.

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

    Re: Copying a number from a string

    Non Regular Expression solution.
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    02-19-2019
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    MS Office 2010
    Posts
    24

    Re: Copying a number from a string

    Kaper, jindon,

    I hope this is allowed (not considered spam), but I would like to thank you both and I have given both your posts reputation.

    For ease of use and readability I have incorporated Kaper's solution, which is working great!

    As for jindon, I have read this weekend about Regular Expression, which seems difficult (or just new to me) to write and read, but I will practice using it.

    Much appreciated!

  16. #16
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,617

    Re: Copying a number from a string

    You are welcome, and thanks for reputation.

    It would be lovely if you could select thread tools (above your first post in this thread) and mark the therad as SOLVED.


    PS. No, this is not spam. Nobody (i hope) treats kindness as spam :-)

+ 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. Replies: 2
    Last Post: 01-05-2018, 12:31 AM
  2. Copying all rows whose string column matches an input string.
    By yoshitsune in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-13-2015, 03:27 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. VBA Find all instances of a specific string and copying the number of count in cell
    By dilshandil in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-23-2013, 10:27 PM
  5. [SOLVED] Extract a mainly numeric string (ISSN number) from an arbitrary string
    By Buzzed Aldrin in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 07-04-2012, 01:49 PM
  6. Replies: 1
    Last Post: 01-25-2010, 12:48 PM
  7. [SOLVED] copying part of a number string
    By Bill R in forum Excel General
    Replies: 1
    Last Post: 02-12-2006, 06:40 PM

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