VBA Runtime Error when Selecting Dropdown List

Tigron

New Member
Joined
Jun 2, 2023
Messages
21
Office Version
  1. 2021
Platform
  1. MacOS
Hello community,

first of all I'd like to note, that I am a complete noob when it comes to VBA. Unlike other programming languages such as HTML, CSS or even Python, VBA doesn't really "speak with me", which makes finding a solution - or just even playing around with the code - pretty difficult. I feel "a tiny bit" overwhelmed.

I am working on a trading journal in Excel (I trade cryptocurrencies) and found a solution to an issue I had. I wanted to be able to put long notes on my individual trades in a very small cell and, when reviewing my trades in retrospect, be able to "hover over" that cell and have a pop-up appear with the complete text. I found a great solution here: Link

The problem with this solution is that, whenever I select a cell that contains a dropdown list, I get a VBA runtime error. Please view screen shots for the exact message.

The code I am using looks like this:
VBA Code:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Const SHAPENAME As String = "MessageShape"
    Dim ws As Worksheet
    Dim oshp As Shape
    Dim icount As Integer
    Dim iX As Integer
    Dim iY As Integer
    Dim iHeight As Integer
    Dim iWidth As Integer
    
    Set ws = ActiveSheet
    
    'Only run if one cell is selected
    If Target.Count > 1 Then Exit Sub
    
    'Delete any existing shapes. We run though all shapes backwards
    For icount = ws.Shapes.Count To 1 Step -1
        Set oshp = ws.Shapes(icount)
        If (oshp.Name = SHAPENAME) Then
            oshp.Delete
        End If
    Next icount
    
    'Lets grab the position of the selected cell, we'll use this to place the message box just to the right of it.
    'We add 5 to the position to give it a pleasant offset
    iX = Target.Cells(1, 2).Left + 5
    iY = Target.Top + 5
    iHeight = 200 'Adjust the box height by changing this
    iWidth = 450 'Adjust the box widthby changing this
    
    'Show the message box - Amend this if you want to change the position
    Select Case Target.Column
            Case 21, 32, 37 'Add column numbers here
            Set oshp = ws.Shapes.AddTextbox(msoTextOrientationHorizontal, iX, iY, iWidth, iHeight)
            oshp.TextFrame2.TextRange.Characters.Text = Target.Value
            oshp.Name = SHAPENAME
    End Select

End Sub

Can someone explain what is going wrong here and how to solve this issue. You would make me a immensely happy camper. :)
One last thing: I would also like to change the background color and the font color of the pop-up, but I have no idea as to where I have to paste the code.

Thanks in advance for any help you can provide!
 

Attachments

  • Debug_Error.png
    Debug_Error.png
    51.8 KB · Views: 14
  • 2023-06-04_22-27-17.jpg
    2023-06-04_22-27-17.jpg
    254.7 KB · Views: 15

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
See if changing this line works.

Original
VBA Code:
For icount = ws.Shapes.Count To 1 Step -1

New
VBA Code:
For icount = ws.Shapes.Count -1 To 1 Step -1
 
Upvote 1
So, if you don't want the code to fire when you are clicking on a cell that has a drop down, let's say that cell is 'A1', you could write code like below.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Range("A1"), Target) Is Nothing Then
    Const SHAPENAME As String = "MessageShape"
    Dim ws As Worksheet
    Dim oshp As Shape
    Dim icount As Integer
    Dim iX As Integer
    Dim iY As Integer
    Dim iHeight As Integer
    Dim iWidth As Integer

    Set ws = ActiveSheet

    'Only run if one cell is selected
    If Target.Count > 1 Then Exit Sub

    'Delete any existing shapes. We run though all shapes backwards
    For icount = ws.Shapes.Count To 1 Step -1
        Set oshp = ws.Shapes(icount)
        If (oshp.Name = SHAPENAME) Then
            oshp.Delete
        End If
    Next icount

    'Lets grab the position of the selected cell, we'll use this to place the message box just to the right of it.
    'We add 5 to the position to give it a pleasant offset
    iX = Target.Cells(1, 2).Left + 5
    iY = Target.Top + 5
    iHeight = 200 'Adjust the box height by changing this
    iWidth = 450 'Adjust the box widthby changing this

    'Show the message box - Amend this if you want to change the position
    Select Case Target.Column
            Case 21, 32, 37 'Add column numbers here
            Set oshp = ws.Shapes.AddTextbox(msoTextOrientationHorizontal, iX, iY, iWidth, iHeight)
            oshp.TextFrame2.TextRange.Characters.Text = Target.Value
            oshp.Name = SHAPENAME
    End Select
End If
End Sub
 
Upvote 1
After adding data to a test sheet, I think I'm getting what you want now.

Let me know if this works.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Const SHAPENAME As String = "MessageShape"
Dim ws As Worksheet
Dim oshp As Shape
Dim icount As Integer
Dim iX As Integer
Dim iY As Integer
Dim iHeight As Integer
Dim iWidth As Integer

Set ws = ActiveSheet

For icount = ws.Shapes.Count To 1 Step -1
    Set oshp = ws.Shapes(icount)
    If (oshp.Name = SHAPENAME) Then
        oshp.Delete
    End If
Next icount

iX = Target.Cells(1, 2).Left + 5
iY = Target.Top + 5
iHeight = 200
iWidth = 450

If Not IsEmpty(Target.Value) Then
    Select Case Target.Column
            Case 21, 32, 37
            With ws.Shapes.AddTextbox(msoTextOrientationHorizontal, iX, iY, iWidth, iHeight)
                .TextFrame2.TextRange.Characters.Text = Target.Value
                .Name = SHAPENAME
                .BackgroundStyle = msoBackgroundStylePreset10
            End With
    End Select
End If
End Sub
 
Upvote 1
Maybe that would be best. I can't replicate the error that you are getting. I added dropdowns and regular text and the code seems to be working as expected.
 
Upvote 1
Lol. Guess you're right. Must be because you are a new member. I don't have the option to send you a message either.

You can post the file on dropbox or onedrive or something like that and post the link here.
 
Upvote 1
Where are you clicking that it is giving you the error? I am on the 'Trading Journal' sheet and I don't see any dropdowns.
 
Upvote 1
I have no idea what might be going on on your end.

I opened the file and everything is working as expected. I can select different values from the dropdowns and when I click in columns 21, 32, and 37, the code fires and shows the shape with the text.

This is a different error than the images you posted in the original post, correct?

If so, what error are you getting now?
 
Upvote 1
Maybe it is. I've never used a Mac so, I am not sure what could be causing this.

The only thing I can think is that somehow Mac is using 0 based indexing whereas it's 1 based on PC.

Back to that first answer I gave changing

Excel Formula:
For icount = ws.Shapes.Count To 1 Step -1

to

Excel Formula:
For icount = ws.Shapes.Count -1 To 0 Step -1

But outside of that, I am kind of stumped.
 
Upvote 1
Actually, looking at it, the code I just posted is slightly different than the first time I posted it.

Give that last one a go. 🤞
 
Upvote 1
Sorry, buddy. Don't know what to tell you. Maybe someone else on here can help out.

I'll keep digging and keep you posted, but it doesn't make sense to me why it's doing this.
 
Upvote 1

Forum statistics

Threads
1,215,126
Messages
6,123,200
Members
449,090
Latest member
bes000

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