Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

Thread: conditional formating if Teddy I

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    conditional formating if Teddy I

    I am using code to highlight a cell if the vlook returns a value of Teddy I. Here is the code I tested

    [vba]With Range("B2", Range("B2").End(xlDown).Offset(-1, 0))
    With .FormatConditions.Add Type:=xlExpression, _
    Formula1:="=if(VLOOKUP(RC[1], _
    '[BusinessReportingReference.xls]Products'!C1:C9,9,FALSE)=Teddy I)"
    .Font.Bold = True
    .Font.ColorIndex = 3
    .Interior.ColorIndex = 8
    End With
    End With[/vba]

    However it keeps giving me an error on the with .formatconditions line

    Edited 13-Mar-07 by geekgirlau. Reason: insert line breaks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    With Range("B2", Range("B2").End(xlDown).Offset(-1, 0))
    With .FormatConditions.Add(Type:=xlExpression, _
    Formula1:="=VLOOKUP(RC[1],'[BusinessReportingReference.xls]Products'!R1C3:R9C12,9,FALSE)=""Teddy I""")
    .Font.Bold = True
    .Font.ColorIndex = 3
    .Interior.ColorIndex = 8
    End With
    End With
    [/vba]

  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    XLD,

    it is still giving me a compile error

  4. #4
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    don't worry now it worked

  5. #5
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I am no longer getting a compile error but I am getting an invalid procedure call or argument on the formatconditions line

  6. #6
    VBAX Contributor moa's Avatar
    Joined
    Nov 2006
    Posts
    177
    Location
    Youre missing a bracket by the looks:
    [vba].FormatConditions.Add(Type:=xlExpression, Formula1:="=VLOOKUP(RC[1],'[BusinessReportingReference.xls] _
    Products'!R1C3:R9C12,9,FALSE)=""Teddy I"")")[/vba]
    Glen

  7. #7
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Glen,

    your code is giving me a compile error. This is what I have:

    [VBA]With Range("B2", Range("B2").End(xlDown).Offset(-1, 0))
    With .FormatConditions.Add(Type:=xlExpression, _
    Formula1:="=VLOOKUP(RC[1],'[BusinessReportingReference.xls]Products'!R1C3:R9C12,9,FALSE)=""Teddy I""")
    .Font.Bold = True
    .Font.ColorIndex = 3
    .Interior.ColorIndex = 8
    End With
    End With[/VBA]

    and it is giving me an invalid procedure call or argument on the formatconditions line

  8. #8
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    bump

  9. #9
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    As far as I know, you cannot have a reference to another sheet or workbook in conditional formatting.

  10. #10
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Have you tried entering the conditional formatting without the code? I tried without the code, and it kept giving me errors in the formula about the R1C1 notation. After I adjusted that to reflect "A1" notation it gave me an error stating:

    "You may not use references to other worksheets or workbooks for Conditional Formatting criteria."

    Is the condition based on the current worksheet in the current workbook or is it elsewhere? Maybe this is your problem.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  11. #11
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Sorry geekgirlau, I took too long to reply without refreshing




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  12. #12
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Quite okay Joseph - I'm sure I was channelling you at the time anyway

  13. #13
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    He he he... stop playing with my name and it'll work fine
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  14. #14
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I tried it and thought it worked. Maybe it was a fluke. Is there another way of doing it? Not through conditional formating, maybe?

  15. #15
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    [VBA]Sub formatting_teddy()
    Dim cell As Range
    Dim lookup As Range
    Dim firstaddress As String
    'every cell in B2:BX - X
    For Each cell In Worksheets(1).Range("B2", Range("B2").End(xlDown).Offset(-1, 0))
    'This is the list with descriptions
    With Worksheets(2).Range("C1:C9")
    'Lookup number of description
    Set lookup = .Find(cell, LookIn:=xlValues)
    'In sheets(2) or for you the businessreports
    '1 in column C - Teddy I in column D
    '2 in column C - Another description in D
    '...
    '9
    'If 1 we color the cell
    If Not lookup Is Nothing And lookup = 1 Then
    firstaddress = lookup.Address
    Do
    cell.Interior.ColorIndex = 8
    Set lookup = .FindNext(lookup)
    Loop While Not lookup Is Nothing And lookup.Address <> firstaddress
    End If
    End With
    Next cell
    End Sub[/VBA]

  16. #16
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by geekgirlau
    Quite okay Joseph - I'm sure I was channelling you at the time anyway
    Yes I saw into the future of your post and decided to write the same thing (since your 15 hours 'ahead' of me)




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  17. #17
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Charlize,

    I am sorry but I don't understand your code. How do I use it? I can't figure out where it attaches to the other spreadsheet to check if it finds Teddy I? Please help I am confused?

  18. #18
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Quote Originally Posted by Djblois
    Charlize,

    I am sorry but I don't understand your code. How do I use it? I can't figure out where it attaches to the other spreadsheet to check if it finds Teddy I? Please help I am confused?
    I thought that you needed to check for an offset value (needs to be added) of a value that you searched for. So you look for 1 in sheet2. If found, check on Teddy I (offset column) and color if true.
    [vba]If Not lookup Is Nothing Then
    firstaddress = lookup.Address
    Do
    If lookup.Offset(,9) = "Teddy I" then
    cell.Interior.ColorIndex = 8
    End If
    Set lookup = .FindNext(lookup)
    Loop While Not lookup Is Nothing And lookup.Address <> firstaddress
    End If
    [/vba]Charlize

  19. #19
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Charlize,

    here is an example of what I am trying to do:

    In the spreadsheet that I am testing:
    345257
    487585
    120565
    In the spreadsheet that I am looking up in
    345257 Teddy I
    487585
    120565 Teddy I
    This is what the original would look like:
    345257
    487585
    120565

    I tried to do it with a V-Look up and it didn't work.

  20. #20
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    [vba]Sub formatting_teddy()
    Dim cell As Range
    Dim lookup As Range
    Dim firstaddress As String
    'every cell in B2:BX - X / X is not in the range to be formatted
    For Each cell In Worksheets(1).Range("B2", Range("B2").End(xlDown).Offset(-1, 0))
    'This is the list with descriptions
    With Worksheets(2).Range("C1:C9")
    'Lookup number of description
    Set lookup = .Find(cell, LookIn:=xlValues)
    'In sheets(2) or for you the businessreports
    'looking for value of column b
    If Not lookup Is Nothing Then
    firstaddress = lookup.Address
    Do
    'if value found check for offset value
    If lookup.Offset(, 1) = "Teddy I" Then
    'if true then column b is bold
    cell.Font.Bold = True
    End If
    'next search
    Set lookup = .FindNext(lookup)
    Loop While Not lookup Is Nothing And lookup.Address <> firstaddress
    End If
    End With
    Next cell
    End Sub[/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •