Public Variable in a Class Module

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,964
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hi all

Here's the problem I'm facing.

I have a class module that handles the BeforeDropOrPaste event of a bunch of Labels on a userform. All is going well.

In the userform module, I have another event, a Listbox_MouseMove event. Users can move elements of a listbox into any one of the labels on the userform.

But I need to track whether shift or ctrl was pressed when moving. How should I pass on that information in a public variable? I've defined a public variable in the userform module:

Public lShift As Long

But for some reason I can't use it in the class module. If I write to a sheet and read back from it, it works perfectly.

I hope someone can help with it.

Thanks in advance,

Wigi
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hmm, no replies.

Should I include more details?
 
Upvote 0
Hmm, no replies.

Should I include more details (not sure which, but I could try)?

Well, I thought about the details I could still provide.

I can say that I put the line

Public lShift As Long

on top of the userform module. Below that, I have:

Code:
Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
                       ByVal X As Single, ByVal Y As Single)

    Dim MyDataObject As DataObject
    Dim lEffect As Long

    If Button = 1 Then

        'track whether Ctrl was pressed
        lShift = Shift     ' Range("P1").Value = Shift

        'drag
        Set MyDataObject = New DataObject

        'fill the data object
        MyDataObject.SetText Me.ListBox1.Value
        
        lEffect = MyDataObject.StartDrag
    End If
End Sub

In the class module that handles all the labels, I have:

Code:
Sub LabelGroup_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _
                                 ByVal Action As MSForms.fmAction, _
                                 ByVal Data As MSForms.DataObject, ByVal X As Single, _
                                 ByVal Y As Single, _
                                 ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)

    Dim i As Long

    Cancel = True
    With UserForm1.ListBox1

        'effect
        If .List(.ListIndex, 2) = "No" Then
            Effect = fmDropEffectNone
        ElseIf lShift <> 2 Then     'Range("P1").Value
            Effect = fmDropEffectMove
        Else
            Effect = fmDropEffectCopy
        End If

        'delete if Ctrl was not pressed
        If lShift <> 2 Then .RemoveItem (.ListIndex)    'Range("P1").Value

    End With
End Sub

When I try to use it, I get an error. With cell P1 (as in the comments) it works perfectly.

Anyone got a clue? I'm sure this should be possible but I just can't see what I'm doing wrong. Thanks.

Wigi
 
Upvote 0
Hi Wigi

What happens if you place the Public declaration in a separate standard (ie non-class) module? Do you still get the error? What error exactly is it?
 
Upvote 0
Hello Richard

Gosh, that works perfectly! I put the Public ... line on top of a standard module.

The solution seems easy but this is only the first time I used class modules. Thanks!

Wigi
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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