ChristineJ
Well-known Member
- Joined
- May 18, 2009
- Messages
- 731
- Office Version
-
- 365
- Platform
-
- Windows
I have been successfully using the following code in a workbook so that when a cell in B6:B16 is selected, the contents in three cells to the right in columns E, F, and L in the same row appears in a pop-up box. The cells in B6:B16 each contain an input message under Data Validation, but the "Show input message when cell is selected" is not checked. It works perfectly.
I want to duplicate that and ALSO use the content in columns H, K, and J when a cell in D6:D16 is selected.
I have been trying to insert this code that was suggested below into what I had, but I am clearly not doing it correctly because I get a compile error: Else without If. Any suggestions?
Here is my attempt at combining the two codes above:
Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim strTitle As String
Dim strMsg As String
Dim lDVType As Long
Dim sTemp As Shape
Application.EnableEvents = False
On Error Resume Next
Set sTemp = ActiveSheet.Shapes("txtInputMsg")
'* If the box does not exist, add it
If Err.Number <> 0 Then
Set sTemp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, 10, 10, 72, 72)
sTemp.Name = "txtInputMsg"
End If
On Error GoTo 0
sTemp.TextFrame.Characters.Text = ""
sTemp.Visible = False
With Target
If 1 = .Cells.Count And Not Application.Intersect(.Cells, Range("B6:B16")) Is Nothing Then
lDVType = 99
On Error Resume Next
lDVType = .Validation.Type
On Error GoTo 0
If lDVType <> 99 And .Value <> "" Then
Rem put text in box
strTitle = CStr(Range("E" & Target.Row)) & vbCr
strMsg = CStr(Range("F" & Target.Row)) & IIf(CStr(Range("L" & Target.Row)) = "", vbNullString, vbCr) & CStr(Range("L" & Target.Row))
sTemp.TextFrame.Characters.Text = strTitle & strMsg
Rem size and format text box
sTemp.TextFrame.AutoSize = True
sTemp.TextFrame.Characters.Font.Bold = False
sTemp.TextFrame.Characters(1, Len(strTitle)).Font.Bold = False
Rem position text box
sTemp.Left = .Offset(0, 1).Left
'I changed the 0 to 1
sTemp.Top = .Top - sTemp.Height
Rem show text box
sTemp.Visible = msoTrue
End If
End If
End With
Application.EnableEvents = True
End Sub
I want to duplicate that and ALSO use the content in columns H, K, and J when a cell in D6:D16 is selected.
I have been trying to insert this code that was suggested below into what I had, but I am clearly not doing it correctly because I get a compile error: Else without If. Any suggestions?
Code:
With Target
If 1 = .Cells.Count And Not Application.Intersect(.Cells, Range("B6:B16")) Is Nothing Then
'*
'* Code for B6:B16
ElseIf 1 = .Cells.Count And Not Application.Intersect(.Cells, Range("D6:D16")) Is Nothing Then
'*
'* Code for D6:D16
End If
End With
Here is my attempt at combining the two codes above:
Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim strTitle As String
Dim strMsg As String
Dim lDVType As Long
Dim sTemp As Shape
Application.EnableEvents = False
On Error Resume Next
Set sTemp = ActiveSheet.Shapes("txtInputMsg")
'* If the box does not exist, add it
If Err.Number <> 0 Then
Set sTemp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, 10, 10, 72, 72)
sTemp.Name = "txtInputMsg"
End If
On Error GoTo 0
sTemp.TextFrame.Characters.Text = ""
sTemp.Visible = False
With Target
If 1 = .Cells.Count And Not Application.Intersect(.Cells, Range("B6:B16")) Is Nothing Then
lDVType = 99
On Error Resume Next
lDVType = .Validation.Type
On Error GoTo 0
If lDVType <> 99 And .Value <> "" Then
Rem put text in box
strTitle = CStr(Range("E" & Target.Row)) & vbCr
strMsg = CStr(Range("F" & Target.Row)) & IIf(CStr(Range("L" & Target.Row)) = "", vbNullString, vbCr) & CStr(Range("L" & Target.Row))
sTemp.TextFrame.Characters.Text = strTitle & strMsg
Rem size and format text box
sTemp.TextFrame.AutoSize = True
sTemp.TextFrame.Characters.Font.Bold = False
sTemp.TextFrame.Characters(1, Len(strTitle)).Font.Bold = False
Rem position text box
sTemp.Left = .Offset(0, 1).Left
'I changed the 0 to 1
sTemp.Top = .Top - sTemp.Height
Rem show text box
sTemp.Visible = msoTrue
End If
End If
ElseIf 1 = .Cells.Count And Not Application.Intersect(.Cells, Range("D6:D16")) Is Nothing Then
lDVType = 99
On Error Resume Next
lDVType = .Validation.Type
On Error GoTo 0
If lDVType <> 99 And .Value <> "" Then
Rem put text in box
strTitle = CStr(Range("E" & Target.Row)) & vbCr
strMsg = CStr(Range("F" & Target.Row)) & IIf(CStr(Range("L" & Target.Row)) = "", vbNullString, vbCr) & CStr(Range("L" & Target.Row))
sTemp.TextFrame.Characters.Text = strTitle & strMsg
Rem size and format text box
sTemp.TextFrame.AutoSize = True
sTemp.TextFrame.Characters.Font.Bold = False
sTemp.TextFrame.Characters(1, Len(strTitle)).Font.Bold = False
Rem position text box
sTemp.Left = .Offset(0, 1).Left
'I changed the 0 to 1
sTemp.Top = .Top - sTemp.Height
Rem show text box
sTemp.Visible = msoTrue
End If
End If
End With
Application.EnableEvents = True
End Sub