+ Reply to Thread
Results 1 to 9 of 9

Vba to change cell color if text has only 1 word (SAVE MY JOB PLS)

  1. #1
    Forum Contributor
    Join Date
    01-19-2021
    Location
    Brazil
    MS-Off Ver
    Office 365
    Posts
    263

    Exclamation Vba to change cell color if text has only 1 word (SAVE MY JOB PLS)

    Hello,

    I'm a excel newbie and been studying and learning for the past 2 months, so forgive me if what I ask happens to be stupid

    I was trying to create a code that changes the color of a cell if the cell value contains only one word


    For example: I have a column with names and surnames, and I want to highlight entries that do not contain a surname (that means it's a single word value) with a RGB color

    I'm not a real coder, and all my success attempts in other situations was adapting/merging codes I find in the internet, but I can't find anything related to this. Anyway, my only attempt that didn't result error (but didn't do anything at all) was the following:

    Please Login or Register  to view this content.
    I came across somecodes that use the split function to result a single value as an error, then a ISERROR would recognize the cells marked as an error and change the color of it, so I tried to do something similar

    If anyone could help me out I'd be extremely thankful!

    Pedro.
    Last edited by therealdees; 02-26-2021 at 03:15 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Vba to change cell font if text has only 1 word

    Administrative Note:

    Welcome to the forum.

    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 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.)
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    01-19-2021
    Location
    Brazil
    MS-Off Ver
    Office 365
    Posts
    263

    Re: Vba to change cell font if text has only 1 word

    Just corrected it! I'm sorry, had no idea

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Vba to change cell color if text has only 1 word (SAVE MY JOB PLS)

    No need for 2 loops.
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    01-19-2021
    Location
    Brazil
    MS-Off Ver
    Office 365
    Posts
    263

    Re: Vba to change cell color if text has only 1 word (SAVE MY JOB PLS)

    Quote Originally Posted by Norie View Post
    No need for 2 loops.
    Please Login or Register  to view this content.
    It works perfectly!!! Much love!!

    Can you explain how the UBound works?

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Vba to change cell color if text has only 1 word (SAVE MY JOB PLS)

    UBound determines the size of an array, if there is only one word in the cell then when you use Split with a space as a delimiter you will get an array of size 0, if there is more than 1 word in the cell then the size will be greater than 0.

  7. #7
    Forum Contributor
    Join Date
    01-19-2021
    Location
    Brazil
    MS-Off Ver
    Office 365
    Posts
    263

    Re: Vba to change cell color if text has only 1 word (SAVE MY JOB PLS)

    Quote Originally Posted by Norie View Post
    UBound determines the size of an array, if there is only one word in the cell then when you use Split with a space as a delimiter you will get an array of size 0, if there is more than 1 word in the cell then the size will be greater than 0.
    Perfect! Thanks for the help Norie

  8. #8
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Vba to change cell color if text has only 1 word (SAVE MY JOB PLS)

    Just pointing out that you do not have to use the Split function to do what you want... InStr works as well... simply test for a space character and, if there is none, then you have a single word name...
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    01-19-2021
    Location
    Brazil
    MS-Off Ver
    Office 365
    Posts
    263

    Re: Vba to change cell color if text has only 1 word (SAVE MY JOB PLS)

    Quote Originally Posted by Rick Rothstein View Post
    Just pointing out that you do not have to use the Split function to do what you want... InStr works as well... simply test for a space character and, if there is none, then you have a single word name...
    Please Login or Register  to view this content.
    Thanks for the observation

+ 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. Change Font size based on text value of other cell
    By food4feet in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-13-2018, 11:57 AM
  2. Change Font Color of cell if another cell contains specific text
    By Cubajz in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-11-2017, 05:19 AM
  3. [SOLVED] Change Font Color in column (Text and Number same cell)
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-18-2017, 02:31 PM
  4. [SOLVED] Change Autoshape Text font to bold based on other cell value
    By DarkEgon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-09-2016, 01:40 AM
  5. How to find text in cell and change font color if found
    By niraya in forum Excel - New Users/Basics
    Replies: 19
    Last Post: 05-14-2012, 01:23 PM
  6. Replies: 1
    Last Post: 04-03-2012, 03:01 PM
  7. [SOLVED] How to get font of particulat word in text inside a cell?
    By Laguna in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-09-2005, 01:05 AM

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