References in Worksheet_Change

bosstec

New Member
Joined
Jun 23, 2011
Messages
12
I get this code to work now, but i need it to run for several cells.
I my example it only work for cell Q8, but it also need to be for:
Q9-Q16 + Q19-Q27 + Q30-Q38 + Q41-Q49 + Q52-Q60 + Q63-Q71 + Q74-Q82
How can i make references in my code for that?
Also the blue marked references should raise according to the above extra references.
I know, this is basic vba coding, but i'm new with this.
Thanks in advance.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim formula As String
If Not Application.Intersect(Target, Range("P8")) Is Nothing Then
If Target.Value = "Vagtudkald" Then
Range("Q8").Value = ""
Range("Q8").Select
With Selection.Interior
.Color = RGB(255, 255, 0)
End With
With Selection.Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Time24"
End With
Else
Range("Q8").FormulaLocal = "=IF(OR(P8="""";R7="""");"""";R7)"
Range("Q8").Select
With Selection.Interior
.Color = RGB(217, 217, 217)
End With
With Selection.Validation
.Delete
End With
End If
End If
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Untested code, but should be close:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    Dim formula As String
    If Target.Count = 1 Then
        If Not Application.Intersect(Target, "MyCells") Is Nothing Then
            With Target.Offset(, 1)
                If Target.Value = "Vagtudkald" Then
                    .Interior.Color = RGB(255, 255, 0)
                    .Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Time24"
                Else
                    .FormulaLocal = "=IF(OR(" & Target.Address(0, 0) & "="""";" & Target.Offset(-1, 2).Address(0, 0) & "="""");"""";" & Target.Offset(-1, 2).Address(0, 0) & ")"
                    .Interior.Color = RGB(217, 217, 217)
                    .Validation.Delete
                End If
            End With
        End If
    End If
    Application.EnableEvents = True
End Sub

MyCells is a range containing the cells to which the event applies. Make that range first before testing the code. Please inspect it closely.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,799
Members
452,943
Latest member
Newbie4296

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