ElseIF question

Jammydan

Board Regular
Joined
Feb 15, 2010
Messages
141
Hi, I have the following code which works fine...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
Dim SelectDG As Range
Dim UserDG As Range
Set SelectDG = Range("F10:F100")
Set UserDG = Range("O2")
Application.ScreenUpdating = False
'**** PLEASE SELECT RANGES ****
    For Each Cell In SelectDG
        If Cell.Value = "Please Select" Then
        Cell.Interior.ColorIndex = 36
        ElseIf Cell.Value = "Yes" Then
            With Cell
            .Interior.ColorIndex = 43
            End With
        ElseIf Cell.Value = "No" Then
            Cell.Interior.ColorIndex = 44
        ElseIf Cell.Value = "N/A" Then
            Cell.Interior.ColorIndex = 2
        End If
    Next
End Sub

However as soon as I add
Code:
.Offset(0, 1) = UserDG
as below it seems to loop for about 2 minutes.

Code:
ElseIf Cell.Value = "Yes" Then
            With Cell
            .Interior.ColorIndex = 43
            .Offset(0, 1) = UserDG
          End With

Can anybody help please?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try this instead:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim i As Long
Application.ScreenUpdating = False

For i = 10 To 100
    Select Case UCase(Range("F" & i))
        Case "PLEASE SELECT"
            Range("F" & i).Interior.ColorIndex = 36
        Case "YES"
            With Range("F" & i)
                .Interior.ColorIndex = 43
                .Offset(0, 1) = Range("O2")
            End With
        Case "NO"
            Range("F" & i).Interior.ColorIndex = 44
        Case "N/A"
            Range("F" & i).Interior.ColorIndex = 2
        End Select
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for replying, however I get the ame problem, the screen flickers for about 20 seconds. Any ideas what maybe causing this?
 
Upvote 0
Yes, it's because you're triggering a worksheet change inside the procedure which detects worksheet changes, and your procedure is repeatedly triggering itself.

Change this:-
Code:
.Offset(0, 1) = UserDG
to this:-
Code:
application.enableevents=false
.Offset(0, 1) = UserDG
application.enableevents=true
This will disable the worksheet change event from triggering as a result of that one statement.
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

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