Avatar billede elidulwich Nybegynder
06. december 2013 - 10:30 Der er 7 kommentarer og
1 løsning

Adding a word to the end of cells that are of certain colours.

I have 4000 cells that are either Red, Blue, Green or Pink:

cell.Interior.Color = "52377"
cell.Interior.Color = "255"
cell.Interior.Color = "16776960"
cell.Interior.Color = "13408767"

These coloured cells also contain numbers and words.  I would like all the cells to have a word added to the end of the cell.  It should not replace the existing data in the cell.

For example:  Cell C3 is colour "16776960" and contains the text "Commodity Fund".  I would like the cell to have the word "(Blue)" added to it.  So Cell C3 will contain "Commodity Fund (Blue)"

" (Blue)" for colour  16776960
" (Green)" for colour  52377
" (Pink)" for the colour  13408767
" (Red)" for the colour  255

If possibile I'd like to highlight the range of cells that are coloured and then run the vba code you give me.

I hope I explained this well, if not please do ask me more specific questions.  I am using Excel 2010 on Windows 7.
Avatar billede elidulwich Nybegynder
06. december 2013 - 13:54 #1
So this is what I've come up with for changing the coloured cells to have the words in them.  The problem is that it repalces the data in the cell, when I need it to be added at the end of the cell's contents:




Sub Colours_Selected()
'
' Colours_Selected Macro
'
' Keyboard Shortcut: Ctrl+g
'
Dim cell As Range
  Sheets("Pricing1").Select
    Range("AM15:BI86").Select
    Range("AM86").Activate
'
For Each cell In Selection
    If cell.Interior.Color = "255" Then
        cell.Value = "(Red)"
    ElseIf cell.Interior.Color = "13408767 " Then
        cell.Value = "(Pink)"
    ElseIf cell.Interior.Color = "52377" Then
        cell.Value = "Green"
    ElseIf cell.Interior.Color = "16776960" Then
    End If
Next cell
'
End Sub
Avatar billede kabbak Professor
06. december 2013 - 16:46 #2
added to it.
#1
  cell.Value = cell.Value  & "(Red)"
Avatar billede elidulwich Nybegynder
06. december 2013 - 18:36 #3
Thanks for your help so far. 

I realise now that I need the "(Green)", "(Blue)", "(Red)", "(Pink)" to be in size 8 Times New Roman font.  Because some cell contents are in wingdings font.  How can this be specified?
Avatar billede kabbak Professor
06. december 2013 - 22:09 #4
Sub Colours_Selected()
'
' Colours_Selected Macro
'
' Keyboard Shortcut: Ctrl+g
'
Dim CELL As Range
  Sheets("Pricing1").Select
    Range("AM15:BI86").Select
    Range("AM86").Activate
'
For Each CELL In Selection
    If CELL.Interior.Color = "255" Then
        CELL.Value = CELL.Value & "(Red)"
    ElseIf CELL.Interior.Color = "13408767 " Then
        CELL.Value = CELL.Value & "(Pink)"
    ElseIf CELL.Interior.Color = "52377" Then
        CELL.Value = CELL.Value & "Green"
    ElseIf CELL.Interior.Color = "16776960" Then
   
    End If
Call ChangeFont(CELL)
Next CELL
'
End Sub
Sub ChangeFont(CELL)
    With CELL.FONT
        .Name = "Times New Roman"
        .Size = 8
    End With
End Sub
Avatar billede elidulwich Nybegynder
09. december 2013 - 10:12 #5
The only remaining problem I have is that the text in the cell "CELL.Value" gets converted to Times New Roman font size 8.  I only want the new text (e.g.  & "(Red)")to be in Times New Roman font size 8.  Many of the cells have specific fonts such as wingdings which I don't want to interfere with them.
Avatar billede elidulwich Nybegynder
09. december 2013 - 10:13 #6
Latest version:

Sub Colours_Selected()
'
' Colours_Selected Macro
'
' Keyboard Shortcut: Ctrl+g
'
Dim CELL As Range
  Sheets("ISDA Colour Only").Select
    Range("G11:DJ11").Select
    Range("G11").Activate
'
For Each CELL In Selection
    If CELL.Interior.Color = "255" Then
        CELL.Value = CELL.Value & " (Red)"
    ElseIf CELL.Interior.Color = "13408767" Then
        CELL.Value = CELL.Value & " (Pink)"
    ElseIf CELL.Interior.Color = "52377" Then
        CELL.Value = CELL.Value & " (Green)"
    ElseIf CELL.Interior.Color = "16776960" Then
        CELL.Value = CELL.Value & " (Blue)"
   
    End If
Call ChangeFont(CELL)
Next CELL
'
End Sub
Sub ChangeFont(CELL)
    With CELL.Font
        .Name = "Times New Roman"
        .Size = 8
    End With
End Sub
Avatar billede kabbak Professor
09. december 2013 - 12:47 #7
Sub Colours_Selected()
'
' Colours_Selected Macro
'
' Keyboard Shortcut: Ctrl+g
'
Dim CELL As Range
  Sheets("ISDA Colour Only").Select
    Range("G11:DJ11").Select
    Range("G11").Activate
'
For Each CELL In Selection
    If CELL.Interior.Color = "255" Then
        CELL.Value = CELL.Value & " (Red)"
        Call ChangeFont(CELL, 5)

    ElseIf CELL.Interior.Color = "13408767" Then
        CELL.Value = CELL.Value & " (Pink)"
          Call ChangeFont(CELL, 6)
         
    ElseIf CELL.Interior.Color = "52377" Then
        CELL.Value = CELL.Value & " (Green)"
          Call ChangeFont(CELL, 7)
         
    ElseIf CELL.Interior.Color = "16776960" Then
        CELL.Value = CELL.Value & " (Blue)"
    Call ChangeFont(CELL, 6)
    End If

Next CELL
'
End Sub




Sub ChangeFont(CELL, LENGTOFWORD)
    With CELL.Characters(Start:=Len(CELL) - LENGTOFWORD, Length:=LENGTOFWORD).Font
        .Name = "Times New Roman"
        .Size = 8
    End With
End Sub
Avatar billede elidulwich Nybegynder
09. december 2013 - 14:14 #8
Thank you very much.  This works very well.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Excel kurser for alle niveauer og behov – find det kursus, der passer til dig

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester