+ Reply to Thread
Results 1 to 8 of 8

VBA If statement containing a word & extracting characters

  1. #1
    Registered User
    Join Date
    12-17-2019
    Location
    Leeds, UK
    MS-Off Ver
    365
    Posts
    61

    Question VBA If statement containing a word & extracting characters

    Cell A2 in my active sheet has the value "Class Data, Day 1 - Monday 6th, June 2022"

    I need a VBA If statement which determines whether cell A2 (in
    Please Login or Register  to view this content.
    format) contains the word "Day", and if it does, Then my String variable "Day" becomes "Monday 6th, June 2022" (so preferably everything after the "- ")

    I have absolutely no idea how to write this in code. My best guess would've been along the lines of:
    Please Login or Register  to view this content.
    Thanks so much for any help you can offer!

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA If statement containing a word & extracting characters

    I was going to reply in your other thread about this point with an observation ...

    Potentially every cell with a date will contain 'day' (Monday, Tuesday... etc) so you could possibly search for '(Space)day' ... but you don't give enough information to say for certain.

    Another possibility, if the layout is consistent and cells you are interested in contains a separator as you have shown (" - ")
    Please Login or Register  to view this content.
    An aside ...
    ... Then my String variable "Day" becomes ...
    Don't name variables using VBA Function names ...
    Last edited by cytop; 06-24-2022 at 06:16 AM.

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

    Re: VBA If statement containing a word & extracting characters

    Something like this
    Use of "Day" as a variable name is not recommended since vba has Day function.
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    12-17-2019
    Location
    Leeds, UK
    MS-Off Ver
    365
    Posts
    61

    Re: VBA If statement containing a word & extracting characters

    Quote Originally Posted by cytop View Post
    I was going to reply in your other thread about this point with an observation ...

    Potentially every cell with a date will contain 'day' (Monday, Tuesday... etc) so you could possibly search for '(Space)day' ... but you don't give enough information to say for certain.

    Another possibility, if the layout is consistent and cells you are interested in contains a separator as you have shown (" - ")
    Please Login or Register  to view this content.
    An aside ...


    Don't name variables using VBA Function names ...

    Yes, sorry I should've attached a sample of the sheet. See attached.
    RE: naming variables using VBA Function names. Good tip thanks, I'll change it to "Survey_Date"
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA If statement containing a word & extracting characters

    The workbook adds nothing as there is no code to provide context for what you want. The code in posts #2 & #3 will do as you want - possibly with a change or 2. You just need to fit it in somewhere

  6. #6
    Registered User
    Join Date
    12-17-2019
    Location
    Leeds, UK
    MS-Off Ver
    365
    Posts
    61

    Re: VBA If statement containing a word & extracting characters

    Quote Originally Posted by jindon View Post
    Something like this
    Use of "Day" as a variable name is not recommended since vba has Day function.
    Please Login or Register  to view this content.
    Wasn't working with the $ but without, this works great. Thanks for your help!

  7. #7
    Registered User
    Join Date
    12-17-2019
    Location
    Leeds, UK
    MS-Off Ver
    365
    Posts
    61

    Re: VBA If statement containing a word & extracting characters

    Quote Originally Posted by cytop View Post
    The workbook adds nothing as there is no code to provide context for what you want. The code in posts #2 & #3 will do as you want - possibly with a change or 2. You just need to fit it in somewhere
    Yep, got it all working. Thanks for your help and advice

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

    Re: VBA If statement containing a word & extracting characters

    Quote Originally Posted by aurelien_21 View Post
    Wasn't working with the $ but without, this works great. Thanks for your help!
    Then your excel is something wrong.

+ 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: 05-05-2022, 04:16 PM
  2. Extracting characters from a URL
    By naga in forum Excel General
    Replies: 2
    Last Post: 01-03-2017, 01:51 PM
  3. Extracting data from excel to word. Different versions of MS Word giving issues.
    By aritradey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2014, 09:46 AM
  4. Extracting characters
    By siobeh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-27-2014, 10:28 PM
  5. Replies: 0
    Last Post: 06-15-2011, 02:22 PM
  6. extracting characters in a URL
    By claws in forum Excel General
    Replies: 6
    Last Post: 04-05-2010, 08:39 AM
  7. Extracting Characters
    By TaGY2K in forum Excel General
    Replies: 1
    Last Post: 11-09-2009, 06:06 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