I am trying to use a MsgBox upon selection of a cell. I need different message box's to appear depending on which cell is selected, so I'm trying to use and IF statement. Something is going wrong and I don't know what it is. Here is the code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.Calculate
Dim ws1 As Worksheet, ws2 As Worksheet
Dim rngPH1, rngPH2, rngPH3, rngPH4, rngPH5, rngPH6, rngPH7, rngPH8 As Range
Set ws1 = Worksheets("Budget Hours")
Set ws2 = Worksheets("Schedule")
Set rngPH1 = ws2.Range("E7:E27")
Set rngPH2 = ws2.Range("E43:E63")
Set rngPH3 = ws2.Range("E79:E99")
Set rngPH4 = ws2.Range("E115:E135")
Set rngPH5 = ws2.Range("E151:E171")
Set rngPH6 = ws2.Range("E187:E207")
Set rngPH7 = ws2.Range("E222:E242")
Set rngPH8 = ws2.Range("E259:E279")
If Target.Address = rngPH1 Then
Dim rng1 As Range, rng2 As Range, msg1, i1 As Long
Set rng1 = ws1.Range("E6:E10")
Set rng2 = rng1.Offset(0, Target.Row - 6)
msg1 = rng2.EntireColumn.Cells(3).Value & vbNewLine
For i1 = 1 To rng1.Cells.Count
msg1 = msg1 & vbNewLine & rng1.Cells(i).Value & " - " & rng2.Cells(i).Value & " Hours"
Next i1
MsgBox msg1, , ws1.Range("E5").Value
ElseIf Target.Address = rngPH2 Then
Dim rng3 As Range, rng4 As Range, msg2, i2 As Long
Set rng3 = ws1.Range("E15:E19")
Set rng4 = rng3.Offset(0, Target.Row - 42)
msg2 = rng4.EntireColumn.Cells(3).Value & vbNewLine
For i2 = 1 To rng3.Cells.Count
msg2 = msg2 & vbNewLine & rng3.Cells(i).Value & " - " & rng4.Cells(i).Value & " Hours"
Next i2
MsgBox msg2, , ws1.Range("E14").Value
End If
End Sub
I get a "Type mismatch" error at the first IF statement. Why is that? What can I do to get around this?
Target.Address = rngPH1.Address? But normally you useIntersectto do this.rngPH1orrngPH2. I know the code inside theIFstatement works because I ran it with slightly different parameters before. Do you know what could be happening such that it isn't working? Thanks in advance.