+ Reply to Thread
Results 1 to 7 of 7

Visual Basic Help

  1. #1
    John
    Guest

    Visual Basic Help

    I am writing some code in a macro, that when a checkbox is checked, the macro
    is run and will look at a cell to see if the word "TRUE" is in it. If it is
    true, I want to turn the cell red. If it is not true, I want the cell to be
    white. I am getting an else without if error. Below is the code, could
    someone help me?

    Sub us()
    '
    ' us Macro
    ' Macro recorded 12/22/2005 by
    '

    '
    If Range("M42") = "True" Then
    ActiveWindow.SmallScroll Down:=4
    Range("D42,D44,D46").Select
    Range("D46").Activate
    ActiveWindow.SmallScroll Down:=3
    Range("D42,D44,D46,D48,D50").Select
    Range("D50").Activate
    With Selection.Interior
    .ColorIndex = 3
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    Else
    ActiveWindow.SmallScroll Down:=4
    Range("D42,D44,D46").Select
    Range("D46").Activate
    ActiveWindow.SmallScroll Down:=3
    Range("D42,D44,D46,D48,D50").Select
    Range("D50").Activate
    With Selection.Interior
    .ColorIndex = 3
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    End If
    End Sub


  2. #2
    Linc
    Guest

    Re: Visual Basic Help

    Don't you need an End With after your With blocks?


  3. #3
    PaulD
    Guest

    Re: Visual Basic Help

    You need to close your With sections with End With. Note inserted lines
    below
    Paul D

    "John" <[email protected]> wrote in message
    news:[email protected]...
    : I am writing some code in a macro, that when a checkbox is checked, the
    macro
    : is run and will look at a cell to see if the word "TRUE" is in it. If it
    is
    : true, I want to turn the cell red. If it is not true, I want the cell to
    be
    : white. I am getting an else without if error. Below is the code, could
    : someone help me?
    :
    : Sub us()
    : '
    : ' us Macro
    : ' Macro recorded 12/22/2005 by
    : '
    :
    : '
    : If Range("M42") = "True" Then
    : ActiveWindow.SmallScroll Down:=4
    : Range("D42,D44,D46").Select
    : Range("D46").Activate
    : ActiveWindow.SmallScroll Down:=3
    : Range("D42,D44,D46,D48,D50").Select
    : Range("D50").Activate
    : With Selection.Interior
    : .ColorIndex = 3
    : .Pattern = xlSolid
    : .PatternColorIndex = xlAutomatic
    End With '*****<--------
    : Else
    : ActiveWindow.SmallScroll Down:=4
    : Range("D42,D44,D46").Select
    : Range("D46").Activate
    : ActiveWindow.SmallScroll Down:=3
    : Range("D42,D44,D46,D48,D50").Select
    : Range("D50").Activate
    : With Selection.Interior
    : .ColorIndex = 3
    : .Pattern = xlSolid
    : .PatternColorIndex = xlAutomatic
    End With '*****<-----------
    : End If
    : End Sub
    :



  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello John,

    You are missing the End With statements. I highlighted them in red.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  5. #5
    Linc
    Guest

    Re: Visual Basic Help

    Don't you need 'End With' after your With blocks?

    Put 'End With' before 'Else' and before 'End If.'


  6. #6
    L. Howard Kittle
    Guest

    Re: Visual Basic Help

    I think this does the same as your recorded macro, a bit shorter. You
    usually don't have to select cells to do stuff to them.

    Sub us()
    If Range("M42") = "True" Then
    Range("D42,D44,D46,D48,D50").Interior.ColorIndex = 3
    Else
    Range("D42,D44,D46,D48,D50").Interior.ColorIndex = xlNone
    End If
    End Sub

    HTH
    Regards,
    Howard

    "John" <[email protected]> wrote in message
    news:[email protected]...
    >I am writing some code in a macro, that when a checkbox is checked, the
    >macro
    > is run and will look at a cell to see if the word "TRUE" is in it. If it
    > is
    > true, I want to turn the cell red. If it is not true, I want the cell to
    > be
    > white. I am getting an else without if error. Below is the code, could
    > someone help me?
    >
    > Sub us()
    > '
    > ' us Macro
    > ' Macro recorded 12/22/2005 by
    > '
    >
    > '
    > If Range("M42") = "True" Then
    > ActiveWindow.SmallScroll Down:=4
    > Range("D42,D44,D46").Select
    > Range("D46").Activate
    > ActiveWindow.SmallScroll Down:=3
    > Range("D42,D44,D46,D48,D50").Select
    > Range("D50").Activate
    > With Selection.Interior
    > .ColorIndex = 3
    > .Pattern = xlSolid
    > .PatternColorIndex = xlAutomatic
    > Else
    > ActiveWindow.SmallScroll Down:=4
    > Range("D42,D44,D46").Select
    > Range("D46").Activate
    > ActiveWindow.SmallScroll Down:=3
    > Range("D42,D44,D46,D48,D50").Select
    > Range("D50").Activate
    > With Selection.Interior
    > .ColorIndex = 3
    > .Pattern = xlSolid
    > .PatternColorIndex = xlAutomatic
    > End If
    > End Sub
    >




  7. #7
    Registered User
    Join Date
    07-17-2005
    Posts
    22
    Just a thought - depending on whether ticking the checkbox triggers a change to the cell (ie whether it is true or false) - you could also just use conditional formatting instead of code.

    ----

    Rob

+ 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