+ Reply to Thread
Results 1 to 5 of 5

Conditional formatting

  1. #1
    Ian
    Guest

    Conditional formatting

    I'm having trouble setting conditional formats with VBA.

    I currently have:

    With
    Worksheets(OPG).Range("D8").FormatConditions.Add(xlCellValue, xlNotBetween,
    "=VALUE(LEFT(C8,4))", "=VALUE(RIGHT(C8,4))")
    With .Font
    .ColorIndex = 3
    End With
    End With

    This gives Subscript out of range on the first line. C8 contains 2.94-3.06
    and is a range of accepable values.

    Ian
    ---



  2. #2
    Norman Jones
    Guest

    Re: Conditional formatting

    Hi Ian,

    Try something like:

    Sub Tester()
    Dim rng As Range
    Dim var1 As Variant, var2 As Variant
    Dim sh As Worksheet

    Set sh = Worksheets(OPG)

    Set rng = sh.Range("C8")

    var1 = CDbl(Left(rng.Value, 4))
    var2 = CDbl(Right(rng.Value, 4))

    With rng(1, 2).FormatConditions
    .Delete
    .Add _
    Type:=xlCellValue, _
    Operator:=xlBetween, _
    Formula1:=var1, _
    Formula2:=var2
    .Item(1).Font.ColorIndex = 46
    End With

    End Sub

    ---
    Regards,
    Norman


    "Ian" <[email protected]> wrote in message
    news:[email protected]...
    > I'm having trouble setting conditional formats with VBA.
    >
    > I currently have:
    >
    > With
    > Worksheets(OPG).Range("D8").FormatConditions.Add(xlCellValue,
    > xlNotBetween, "=VALUE(LEFT(C8,4))", "=VALUE(RIGHT(C8,4))")
    > With .Font
    > .ColorIndex = 3
    > End With
    > End With
    >
    > This gives Subscript out of range on the first line. C8 contains 2.94-3.06
    > and is a range of accepable values.
    >
    > Ian
    > ---
    >
    >




  3. #3
    Norman Jones
    Guest

    Re: Conditional formatting

    Hi Ian,

    Additionaaly, if the worksheet name is OPG, then the line:

    > Set sh = Worksheets(OPG)


    should be:

    > Set sh = Worksheets("OPG")


    ---
    Regards,
    Norman



    "Norman Jones" <[email protected]> wrote in message
    news:[email protected]...
    > Hi Ian,
    >
    > Try something like:
    >
    > Sub Tester()
    > Dim rng As Range
    > Dim var1 As Variant, var2 As Variant
    > Dim sh As Worksheet
    >
    > Set sh = Worksheets(OPG)
    >
    > Set rng = sh.Range("C8")
    >
    > var1 = CDbl(Left(rng.Value, 4))
    > var2 = CDbl(Right(rng.Value, 4))
    >
    > With rng(1, 2).FormatConditions
    > .Delete
    > .Add _
    > Type:=xlCellValue, _
    > Operator:=xlBetween, _
    > Formula1:=var1, _
    > Formula2:=var2
    > .Item(1).Font.ColorIndex = 46
    > End With
    >
    > End Sub
    >
    > ---
    > Regards,
    > Norman
    >
    >
    > "Ian" <[email protected]> wrote in message
    > news:[email protected]...
    >> I'm having trouble setting conditional formats with VBA.
    >>
    >> I currently have:
    >>
    >> With
    >> Worksheets(OPG).Range("D8").FormatConditions.Add(xlCellValue,
    >> xlNotBetween, "=VALUE(LEFT(C8,4))", "=VALUE(RIGHT(C8,4))")
    >> With .Font
    >> .ColorIndex = 3
    >> End With
    >> End With
    >>
    >> This gives Subscript out of range on the first line. C8 contains
    >> 2.94-3.06 and is a range of accepable values.
    >>
    >> Ian
    >> ---
    >>
    >>

    >
    >




  4. #4
    Ian
    Guest

    Re: Conditional formatting

    Hi Norman

    I tried your code, but it still fails. I get the same error as before, but
    it appears on the "Set sh" line. The sheet name is definitely correct and I
    tried it with the original designation (Sheet6), but get a type mismatch
    error.

    At least your method narrows it down, but I'm still unable to figure out
    what's happening.

    --
    Ian
    --
    "Norman Jones" <[email protected]> wrote in message
    news:[email protected]...
    > Hi Ian,
    >
    > Try something like:
    >
    > Sub Tester()
    > Dim rng As Range
    > Dim var1 As Variant, var2 As Variant
    > Dim sh As Worksheet
    >
    > Set sh = Worksheets(OPG)
    >
    > Set rng = sh.Range("C8")
    >
    > var1 = CDbl(Left(rng.Value, 4))
    > var2 = CDbl(Right(rng.Value, 4))
    >
    > With rng(1, 2).FormatConditions
    > .Delete
    > .Add _
    > Type:=xlCellValue, _
    > Operator:=xlBetween, _
    > Formula1:=var1, _
    > Formula2:=var2
    > .Item(1).Font.ColorIndex = 46
    > End With
    >
    > End Sub
    >
    > ---
    > Regards,
    > Norman
    >
    >
    > "Ian" <[email protected]> wrote in message
    > news:[email protected]...
    >> I'm having trouble setting conditional formats with VBA.
    >>
    >> I currently have:
    >>
    >> With
    >> Worksheets(OPG).Range("D8").FormatConditions.Add(xlCellValue,
    >> xlNotBetween, "=VALUE(LEFT(C8,4))", "=VALUE(RIGHT(C8,4))")
    >> With .Font
    >> .ColorIndex = 3
    >> End With
    >> End With
    >>
    >> This gives Subscript out of range on the first line. C8 contains
    >> 2.94-3.06 and is a range of accepable values.
    >>
    >> Ian
    >> ---
    >>
    >>

    >
    >




  5. #5
    Ian
    Guest

    Re: Conditional formatting

    Brilliant, thanks Norman. This was alos the problem with my original line.
    All working well, now.

    --
    Ian
    --
    "Norman Jones" <[email protected]> wrote in message
    news:[email protected]...
    > Hi Ian,
    >
    > Additionaaly, if the worksheet name is OPG, then the line:
    >
    >> Set sh = Worksheets(OPG)

    >
    > should be:
    >
    >> Set sh = Worksheets("OPG")

    >
    > ---
    > Regards,
    > Norman
    >
    >
    >
    > "Norman Jones" <[email protected]> wrote in message
    > news:[email protected]...
    >> Hi Ian,
    >>
    >> Try something like:
    >>
    >> Sub Tester()
    >> Dim rng As Range
    >> Dim var1 As Variant, var2 As Variant
    >> Dim sh As Worksheet
    >>
    >> Set sh = Worksheets(OPG)
    >>
    >> Set rng = sh.Range("C8")
    >>
    >> var1 = CDbl(Left(rng.Value, 4))
    >> var2 = CDbl(Right(rng.Value, 4))
    >>
    >> With rng(1, 2).FormatConditions
    >> .Delete
    >> .Add _
    >> Type:=xlCellValue, _
    >> Operator:=xlBetween, _
    >> Formula1:=var1, _
    >> Formula2:=var2
    >> .Item(1).Font.ColorIndex = 46
    >> End With
    >>
    >> End Sub
    >>
    >> ---
    >> Regards,
    >> Norman
    >>
    >>
    >> "Ian" <[email protected]> wrote in message
    >> news:[email protected]...
    >>> I'm having trouble setting conditional formats with VBA.
    >>>
    >>> I currently have:
    >>>
    >>> With
    >>> Worksheets(OPG).Range("D8").FormatConditions.Add(xlCellValue,
    >>> xlNotBetween, "=VALUE(LEFT(C8,4))", "=VALUE(RIGHT(C8,4))")
    >>> With .Font
    >>> .ColorIndex = 3
    >>> End With
    >>> End With
    >>>
    >>> This gives Subscript out of range on the first line. C8 contains
    >>> 2.94-3.06 and is a range of accepable values.
    >>>
    >>> Ian
    >>> ---
    >>>
    >>>

    >>
    >>

    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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