VBA instead of a nested IF function or Vlookup

Argh_Work

New Member
Joined
Oct 17, 2023
Messages
7
Office Version
  1. 2013
Platform
  1. Windows
Hi All,

I'm trying to hand off a large worksheet that has a couple of really slow steps. To make it as easy as possible for my colleague I am trying to refine some of the steps.
I was hoping someone could help me with a VBA code for the below?

In the worksheet column F is blank. I use it to Group the information from column E into central teams.
For instance
Blue Green goes to Blue
Yellow Yellow goes to Yellow
Blue Purple goes to Blue

I can't create the VBA code on my own but I am able to generally customise from an example. I'd really appreciate it if someone could have a look at the scenario for me.

If necessary I am happy to include a table on a second worksheet like the 2nd image below.

Thanks so much for your help,
 

Attachments

  • How to add team to worksheet.PNG
    How to add team to worksheet.PNG
    86.7 KB · Views: 7
  • Table of groupings.PNG
    Table of groupings.PNG
    5.8 KB · Views: 7

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Please try the following on a copy of your workbook. Put the code in a standard module & run when the sheet is active.
VBA Code:
Option Explicit
Sub Into_Teams()
    Dim a, b, i As Long
    a = Range("E2", Cells(Rows.Count, "E").End(xlUp))
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    For i = 1 To UBound(a, 1)
        Select Case UBound(Split(a(i, 1), " "))
            Case 0
                b(i, 1) = a(i, 1)
            Case 1
                b(i, 1) = Left(a(i, 1), InStr(a(i, 1), " ") - 1)
            Case Else
                b(i, 1) = "Rainbow"
        End Select
    Next i
    Range("F2").Resize(UBound(b, 1)).Value = b
End Sub
 
Upvote 0
Another option:

VBA Code:
Sub ColorBeforeSpace()
Dim LR As Long

LR = Range("E" & Rows.Count).End(xlUp).Row

    Range("F2:F" & LR).FormulaR1C1 = "=LEFT(RC[-1],FIND("" "",RC[-1]))"
    Range("F2:F" & LR).Value = Range("F2:F" & LR).Value
End Sub
 
Upvote 0
@Coyotex3 your suggestion will return an error if there's only one colour in the cell (see second image in post #1) and also will not return "Rainbow" if more than 2 colours are in the cell.
 
Upvote 0
Thank you all for taking the time to respond @Coyotex3 as @kevin9999 stated is pulling an error as pictured below.
Is there a suggestion to avoid this? Thanks again everyone.
 

Attachments

  • Error.PNG
    Error.PNG
    56.3 KB · Views: 4
Upvote 0
Did you try the code in post #2? When I run it, I get this:
Book1
EF
1Sub centreTeam
2RedRed
3Blue OrangeBlue
4Yellow YellowYellow
5Yellow YellowYellow
6Blue PurpleBlue
7Purple DinosaurPurple
8Pink Purple OrangeRainbow
9Pink Purple OrangeRainbow
10RedRed
11RedRed
12RedRed
13Blue OrangeBlue
14Yellow YellowYellow
15Yellow YellowYellow
16Blue PurpleBlue
17Purple DinosaurPurple
18Pink Purple OrangeRainbow
19Pink Purple OrangeRainbow
20RedRed
21RedRed
Sheet1
 
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,069
Members
449,090
Latest member
fragment

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top