Adding to code for pop-ups

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
761
Office Version
  1. 365
Platform
  1. Windows
This code below from my worksheet creates a pop-up box when certain cells in columns Q, S, and U have a value and are selected. The contents of the pop-up include the text values in other cells in the same row. (Blue text below.)

I'd like to ADD similar functionality for creating the pop-ups in certain cells in columns BI, BK, BM, and BO. However, rather than pulling the text values in the pop-ups from cells in the same row, I would like any cell in a column to have the same contents in the pop-up.

For example, when any cell in the range in BI is clicked on, its pop-up would have:
This is test 1.
This is test 2.
This is test 3.
ANSWER: 55

When any cell in the range in BK is selected, the pop-up would have:
This is test 4.
This is test 5.
This is test 6.
ANSWER: 77

Same for the other two columns. The text sentences would vary quite a bit more than my example above.
Can this code be modified? Thanks.


Rich (BB code):
    Dim strTitle As String
    Dim strMsg As String
    Dim lDVType As Long
    Dim sTemp As Shape
    On Error Resume Next
    Set sTemp = ActiveSheet.Shapes("txtInputMsg")
    '*
    'Code will only work if A13=1 (pop-ups) A34=1 so pop-up does not appear before grading occurs (next line)
    If Range("B6").Value <> 1 Then Exit Sub
    If Range("A34").Value <> 1 Then Exit Sub
    
    '* Add the box if does not exist
    If Err.Number <> 0 Then
        Set sTemp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, 10, 10, 72, 72)
        sTemp.Name = "txtInputMsg"
    End If
    '*
    '* Exit if not in range
    
    With Target
        ' test whether there is more than one cell in the selection, that the cell is in
        ' column 'Q', 'S', or 'U' (Each of these is a named range), that there is something in the cell
        ' and that it has a validation rule applied.
        lDVType = 99
        lDVType = Target.Validation.Type
        If 1 < .Cells.Count Or _
                Application.Intersect(.Cells, Union(Range("ColumnQ"), Range("ColumnS"), Range("ColumnU"))) Is Nothing Or _
                .Value = "" Or lDVType = 99 Then
        
            sTemp.TextFrame.Characters.Text = ""
            sTemp.Visible = msoFalse
        Else
            Application.EnableEvents = False
            .Validation.ShowInput = False
            Select Case .Column
                'Each of these is the column number
                Case 17    '* Column Q
                    strTitle = IIf(CStr(Range("AO" & .Row)) = "", "", (CStr(Range("AO" & .Row)) & vbCr)) & _
                        IIf(CStr(Range("AP" & .Row)) = "", "", (CStr(Range("AP" & .Row)) & vbCr)) & _
                        IIf(CStr(Range("AQ" & .Row)) = "", "", (CStr(Range("AQ" & .Row)) & vbCr))
                    strMsg = IIf(CStr(Range("AG" & .Row)) = "", "ANSWER:   Leave blank", "ANSWER:   " & _
                        CStr(Range("AG" & .Row)))
                Case 19    '* Column S
                    strTitle = IIf(CStr(Range("AR" & .Row)) = "", "", (CStr(Range("AR" & .Row)) & vbCr)) & _
                        IIf(CStr(Range("AS" & .Row)) = "", "", (CStr(Range("AS" & .Row)) & vbCr)) & _
                        IIf(CStr(Range("AT" & .Row)) = "", "", (CStr(Range("AT" & .Row)) & vbCr))
                    strMsg = IIf(CStr(Range("AI" & .Row)) = "", "ANSWER:   Leave blank", "ANSWER:   " & _
                        CStr(Format(Range("AI" & .Row), "#,###")))
    
                Case 21    '* Column U
                    strTitle = IIf(CStr(Range("AU" & .Row)) = "", "", (CStr(Range("AU" & .Row)) & vbCr)) & _
                        IIf(CStr(Range("AV" & .Row)) = "", "", (CStr(Range("AV" & .Row)) & vbCr)) & _
                        IIf(CStr(Range("AW" & .Row)) = "", "", (CStr(Range("AW" & .Row)) & vbCr))
                    strMsg = IIf(CStr(Range("AK" & .Row)) = "", "ANSWER:   Leave blank", "ANSWER:   " & _
                    CStr(Format(Range("AK" & .Row), "#,###")))
    
            End Select
            strMsg = IIf(Range("B1") = 3, "", strMsg)
            strTitle = IIf(Range("B1") = 2, "", strTitle)
            '*
            '* Remove last vbCR from strTitle if strMsg = ""
            strTitle = IIf(strMsg = "", LEFT(strTitle, Len(strTitle) - 1), strTitle)
            sTemp.TextFrame.Characters.Text = strTitle & strMsg
            sTemp.TextFrame.AutoSize = True
            sTemp.TextFrame.Characters.Font.Bold = False
            sTemp.TextFrame.Characters(1, Len(strTitle)).Font.Bold = False
            sTemp.LEFT = .Offset(0, -5).LEFT
            sTemp.Top = .Top - sTemp.Height
            sTemp.Visible = msoTrue
        
            On Error GoTo 0
            Application.EnableEvents = True
        End If
    End With
 
One more question - this occurs in the spreadsheet you sent me as well.

The way to cause the pop-up to disappear is to click on a cell that is not in one of the named ranges affected by the code.

However, many of those cells are merged in my spreadsheet. When I click on a merged cell, the pop-up does not disappear (it did in my original code), and it would be better if it did.
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
OK, not too difficult but a few small pittfalls. When we check to see if Target is single cell, we can add an "or Target is merged range". Rather than reversed checking and exiting the sub prematurely, I now skip the rest if it is multi cell, non-merged.

Then sneakily, if it is a merged (or multicell) range, you cannot ask its value: x = Range("B4:G8").value will fail. (You can set the value of a multi cell range though).
So in order to overcome this I want to get the value of the first cell of the multicell range, and this looks weird, but it is:
Range("B4:G8").Range("A1").value
ie the A1 cell (top left) of the range in question.

So now the total code is as follows:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim strTitle As String
    Dim strMsg As String
    Dim lDVType As Long
    Dim sTemp As Shape
    Dim bFlag As Boolean








    '*
    'Code will only work if B6=1 (pop-ups) A34=1 so pop-up does not appear before grading occurs (next line)
    If Range("B6").Value <> 1 Then Exit Sub
 '   safe to exit sub here as we haven't changed anything yet




    bFlag = False




    On Error Resume Next
    Set sTemp = ActiveSheet.Shapes("txtInputMsg")
    On Error GoTo 0








    With Target
        ' test whether there is more than one cell in the selection, that the cell is in
        ' column 'Q', 'S', or 'U' (Each of these is a named range), that there is something in the cell
        ' and that it has a validation rule applied.


        ' Check that we are looking at single cell selection. Account for merged cells.
        If .Cells.Count = 1 Or .MergeCells = True Then
        
            lDVType = 99
        On Error Resume Next
            If Target.Validation.Type Then lDVType = Target.Validation.Type
        On Error GoTo 0
'            .Range(1, 1) = 3
            
            If Application.Intersect(.Cells, Union(Range("ColumnQ"), Range("ColumnS"), Range("ColumnU"), _
                    Range("ColumnBI"), Range("ColumnBM"), Range("ColumnBK"), Range("ColumnBO"))) Is Nothing Or _
                    .Range("A1").Value = "" Or lDVType = 99 Then
                    'Note the .Range("A1").Value above is full: Target.Range("A1").Value
                    'This refers to the top left cell of Target, when the Target is a
                    'merged cell. (If target is a non-merged multi cell range then the
                    'sub is not run)
                If Not sTemp Is Nothing Then    'if txtbox does exist
                    sTemp.TextFrame.Characters.Text = ""
                    sTemp.Visible = msoFalse
                End If
    
    
    
    
                Exit Sub
            Else
                Application.EnableEvents = False
                .Validation.ShowInput = False
                '*
                Select Case .Column
                    'Each of these is the column number
                    Case Range("ColumnQ").Column    '* Column Q
                        If Range("B6").Value = 1 Then
                            bFlag = True
                            strTitle = IIf(CStr(Range("AO" & .Row)) = "", " ", (CStr(Range("AO" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AP" & .Row)) = "", "", (CStr(Range("AP" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AQ" & .Row)) = "", "", (CStr(Range("AQ" & .Row)) & vbCr))
                            strMsg = IIf(CStr(Range("AG" & .Row)) = "", "ANSWER:   Leave blank", "ANSWER:   " & _
                                CStr(Range("AG" & .Row)))
                        End If
                    Case Range("ColumnS").Column    '* Column S
                        If Range("B6").Value = 1 Then
                            bFlag = True
                            strTitle = IIf(CStr(Range("AR" & .Row)) = "", " ", (CStr(Range("AR" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AS" & .Row)) = "", "", (CStr(Range("AS" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AT" & .Row)) = "", "", (CStr(Range("AT" & .Row)) & vbCr))
                            strMsg = IIf(CStr(Range("AI" & .Row)) = "", "ANSWER:   Leave blank", "ANSWER:   " & _
                                CStr(Format(Range("AI" & .Row), "#,###")))
                        End If
    
    
    
    
                    Case Range("ColumnU").Column    '* Column U
                        If Range("B6").Value = 1 Then
                            bFlag = True
                            strTitle = IIf(CStr(Range("AU" & .Row)) = "", " ", (CStr(Range("AU" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AV" & .Row)) = "", "", (CStr(Range("AV" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AW" & .Row)) = "", "", (CStr(Range("AW" & .Row)) & vbCr))
                            strMsg = IIf(CStr(Range("AK" & .Row)) = "", "ANSWER:   Leave blank", "ANSWER:   " & _
                            CStr(Format(Range("AK" & .Row), "#,###")))
                        End If
                    
                    Case Range("ColumnBI").Column
                        If Range("B7").Value = 1 Then
                            bFlag = True
                            strTitle = "ColumnBI"
                            strMsg = "This is test 1." & vbCr & "This is test 2." & vbCr & "This is test 3." & vbCr & "ANSWER: 55"
                        End If
                    Case Range("ColumnBK").Column
                        If Range("B8").Value = 1 Then
                            bFlag = True
                            strTitle = "ColumnBK"
                            strMsg = "This is test 1." & vbCr & "This is test 2." & vbCr & "This is test 3." & vbCr & "ANSWER: 55"
                        End If
                    Case Range("ColumnBM").Column
                        If Range("B9").Value = 1 Then
                            bFlag = True
                            strTitle = "ColumnBM"
                            strMsg = "This is test 1." & vbCr & "This is test 2." & vbCr & "This is test 3." & vbCr & "ANSWER: 55"
                        End If
                    Case Range("ColumnBO").Column
                        If Range("B10").Value = 1 Then
                            bFlag = True
                            strTitle = "ColumnBO"
                            strMsg = "This is test 1." & vbCr & "This is test 2." & vbCr & "This is test 3." & vbCr & "ANSWER: 55"
                        End If
    
    
    
    
                End Select
                If bFlag Then
                    strMsg = IIf(Range("B1") = 3, "", strMsg)
                    strTitle = IIf(Range("B1") = 2, "", strTitle)
                    '*
                    '* Remove last vbCR from strTitle if strMsg = ""
                    strTitle = IIf(strMsg = "", Left(strTitle, Len(strTitle) - 1), strTitle)
                    '* Add the box if does not exist
                    If sTemp Is Nothing Then
                        Set sTemp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, 10, 10, 72, 72)
                        sTemp.Name = "txtInputMsg"
                    End If
                    sTemp.TextFrame.Characters.Text = strTitle & strMsg
                    sTemp.TextFrame.AutoSize = True
                    sTemp.TextFrame.Characters.Font.Bold = False
                    sTemp.TextFrame.Characters(1, Len(strTitle)).Font.Bold = False
                    sTemp.Left = .Offset(0, -5).Left
                    sTemp.Top = .Top - sTemp.Height
                    sTemp.Visible = msoTrue
                End If
    
    
                Application.EnableEvents = True
            End If
        End If
    End With
End Sub

I have also replaced the spreadsheet in DropBox, see link in posts above.
 
Upvote 0
Brilliant! It works perfectly just as you suggested. How do you know all this??

I have other code on my worksheet. It all works except this little snippet of code that follows your code. (It is so any cell that contains "Exit" causes a macro to run.)

Code:
       If Target.Count > 1 Or Intersect(Target, Range("T1:KH1100")) Is Nothing Then Exit Sub
   If Target.Value = "Exit" And Range("C2").Value = "Split" Then
        Call FullScreenShowAll_2
    ElseIf Target.Value = "Exit" And Range("C2").Value = "Full" Then
        Call ShowAll
        Range("C2").Value = "Split"
    End If

If I put this snippet of code before your code, it works, but then yours does not. Only the code that appears first seems to run.

THANK YOU ! :)
 
Upvote 0
Read what your code is doing. It says if this or that is true then quit. So it will then never process any other code which might follow.
So what you want to do is say is if this isn't true then do this.

I mentioned before that using 'Exit sub' is bad form, because you can leave things undone. It has bitten you here again.

So better is something like this.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim strTitle As String
    Dim strMsg As String
    Dim lDVType As Long
    Dim sTemp As Shape
    Dim bFlag As Boolean


    '*
    'Code will only work if B6=1 (pop-ups) A34=1 so pop-up does not appear before grading occurs (next line)
    If Range("B6").Value <> 1 Then Exit Sub
 '   safe to exit sub here as we haven't changed anything yet




    bFlag = False


    On Error Resume Next
    Set sTemp = ActiveSheet.Shapes("txtInputMsg")
    On Error GoTo 0




    With Target
        ' Check that we are looking at single cell selection. Account for merged cells.
        If .Cells.Count = 1 Or .MergeCells = True Then
        
            If Not Intersect(.Cells, Range("T1:KH1100")) Is Nothing Then
                If .Value = "Exit" And Range("C2").Value = "Split" Then
                    MsgBox "Call FullScreenShowAll_2"
                ElseIf Target.Value = "Exit" And Range("C2").Value = "Full" Then
                    MsgBox "Call ShowAll"
                    Range("C2").Value = "Split"
                End If
            End If
            
        
        ' test whether there is more than one cell in the selection, that the cell is in
        ' column 'Q', 'S', or 'U' (Each of these is a named range), that there is something in the cell
        ' and that it has a validation rule applied.
            lDVType = 99
        On Error Resume Next
            If Target.Validation.Type Then lDVType = Target.Validation.Type
        On Error GoTo 0
            
            If Application.Intersect(.Cells, Union(Range("ColumnQ"), Range("ColumnS"), Range("ColumnU"), _
                    Range("ColumnBI"), Range("ColumnBM"), Range("ColumnBK"), Range("ColumnBO"))) Is Nothing Or _
                    .Range("A1").Value = "" Or lDVType = 99 Then
                    'Note the .Range("A1").Value above is full: Target.Range("A1").Value
                    'This refers to the top left cell of Target, when the Target is a
                    'merged cell. (If target is a non-merged multi cell range then the
                    'sub is not run)
                If Not sTemp Is Nothing Then    'if txtbox does exist
                    sTemp.TextFrame.Characters.Text = ""
                    sTemp.Visible = msoFalse
                End If
    
    
    
    
                Exit Sub
            Else
                Application.EnableEvents = False
                .Validation.ShowInput = False
                '*
                Select Case .Column
                    'Each of these is the column number
                    Case Range("ColumnQ").Column    '* Column Q
                        If Range("B6").Value = 1 Then
                            bFlag = True
                            strTitle = IIf(CStr(Range("AO" & .Row)) = "", " ", (CStr(Range("AO" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AP" & .Row)) = "", "", (CStr(Range("AP" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AQ" & .Row)) = "", "", (CStr(Range("AQ" & .Row)) & vbCr))
                            strMsg = IIf(CStr(Range("AG" & .Row)) = "", "ANSWER:   Leave blank", "ANSWER:   " & _
                                CStr(Range("AG" & .Row)))
                        End If
                    Case Range("ColumnS").Column    '* Column S
                        If Range("B6").Value = 1 Then
                            bFlag = True
                            strTitle = IIf(CStr(Range("AR" & .Row)) = "", " ", (CStr(Range("AR" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AS" & .Row)) = "", "", (CStr(Range("AS" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AT" & .Row)) = "", "", (CStr(Range("AT" & .Row)) & vbCr))
                            strMsg = IIf(CStr(Range("AI" & .Row)) = "", "ANSWER:   Leave blank", "ANSWER:   " & _
                                CStr(Format(Range("AI" & .Row), "#,###")))
                        End If
    
    
    
    
                    Case Range("ColumnU").Column    '* Column U
                        If Range("B6").Value = 1 Then
                            bFlag = True
                            strTitle = IIf(CStr(Range("AU" & .Row)) = "", " ", (CStr(Range("AU" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AV" & .Row)) = "", "", (CStr(Range("AV" & .Row)) & vbCr)) & _
                                IIf(CStr(Range("AW" & .Row)) = "", "", (CStr(Range("AW" & .Row)) & vbCr))
                            strMsg = IIf(CStr(Range("AK" & .Row)) = "", "ANSWER:   Leave blank", "ANSWER:   " & _
                            CStr(Format(Range("AK" & .Row), "#,###")))
                        End If
                    
                    Case Range("ColumnBI").Column
                        If Range("B7").Value = 1 Then
                            bFlag = True
                            strTitle = "ColumnBI"
                            strMsg = "This is test 1." & vbCr & "This is test 2." & vbCr & "This is test 3." & vbCr & "ANSWER: 55"
                        End If
                    Case Range("ColumnBK").Column
                        If Range("B8").Value = 1 Then
                            bFlag = True
                            strTitle = "ColumnBK"
                            strMsg = "This is test 1." & vbCr & "This is test 2." & vbCr & "This is test 3." & vbCr & "ANSWER: 55"
                        End If
                    Case Range("ColumnBM").Column
                        If Range("B9").Value = 1 Then
                            bFlag = True
                            strTitle = "ColumnBM"
                            strMsg = "This is test 1." & vbCr & "This is test 2." & vbCr & "This is test 3." & vbCr & "ANSWER: 55"
                        End If
                    Case Range("ColumnBO").Column
                        If Range("B10").Value = 1 Then
                            bFlag = True
                            strTitle = "ColumnBO"
                            strMsg = "This is test 1." & vbCr & "This is test 2." & vbCr & "This is test 3." & vbCr & "ANSWER: 55"
                        End If
    
    
    
    
                End Select
                If bFlag Then
                    strMsg = IIf(Range("B1") = 3, "", strMsg)
                    strTitle = IIf(Range("B1") = 2, "", strTitle)
                    '*
                    '* Remove last vbCR from strTitle if strMsg = ""
                    strTitle = IIf(strMsg = "", Left(strTitle, Len(strTitle) - 1), strTitle)
                    '* Add the box if does not exist
                    If sTemp Is Nothing Then
                        Set sTemp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, 10, 10, 72, 72)
                        sTemp.Name = "txtInputMsg"
                    End If
                    sTemp.TextFrame.Characters.Text = strTitle & strMsg
                    sTemp.TextFrame.AutoSize = True
                    sTemp.TextFrame.Characters.Font.Bold = False
                    sTemp.TextFrame.Characters(1, Len(strTitle)).Font.Bold = False
                    sTemp.Left = .Offset(0, -5).Left
                    sTemp.Top = .Top - sTemp.Height
                    sTemp.Visible = msoTrue
                End If
    
    
                Application.EnableEvents = True
            End If
        End If
    End With
End Sub
 
Upvote 0
How do I know all this stuff? Practice, read, investigate, read, try, play about, visit this forum often just to see how others solve problems, try to answer a few yourself.

You now have a decent piece of code, but try to understand each part exactly.

VBA has so much, I don't know half of it, but then again I am unlikely to need all of it.

But if I need to do something different, I will try to find which property or object can deliver this.
 
Upvote 0
OK, I now see how that snippet of code could fit in within the your code. Great learning experience for me - I do try to learn from the code, but I am just beginning. Again, thanks for all your help!
 
Upvote 0
Could I ask for help on one more thing. This relates to the code you posted in post #24 above, which solved the problem I had with the pop-up not disappearing when a merged cell was clicked on by adding Or .MergeCells = True. Below is the part I am having an issue with. Now when I select any merged cell, the debugger window comes up and highlights the line I have in blue below.

Rich (BB code):
    With Target
        ' Check that we are looking at single cell selection. Account for merged cells.
        If .Cells.Count = 1 Or .MergeCells = True Then
        
            If Not Intersect(.Cells, Range("T1:KH1100")) Is Nothing Then
If .Value = "Exit" And Range("C2").Value = "Split" Then
                    MsgBox "Call FullScreenShowAll_2"
                ElseIf Target.Value = "Exit" And Range("C2").Value = "Full" Then
                    MsgBox "Call ShowAll"
                    Range("C2").Value = "Split"
                End If
            End If
 
Upvote 0
Ah, we need to refer to the top left cell of target.That can be done by referring to Target.Cell(1,1):

<font face=Courier New>            <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> Intersect(.Cells, Range("T1:KH1100")) <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> .Cells(1, 1).Value = "Exit" And Range("C2").Value = "Split" <SPAN style="color:#00007F">Then</SPAN><br>                    MsgBox "Call FullScreenShowAll_2"<br>                <SPAN style="color:#00007F">ElseIf</SPAN> .Cells(1, 1).Value = "Exit" And Range("C2").Value = "Full" <SPAN style="color:#00007F">Then</SPAN><br>                    MsgBox "Call ShowAll"<br>                    Range("C2").Value = "Split"<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            </FONT>
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,845
Members
449,051
Latest member
excelquestion515

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