How to store a variable in a class module in vba

kapela2017

New Member
Joined
Oct 16, 2022
Messages
34
Office Version
  1. 365
Platform
  1. Windows
Greetings colleagues Currently and started working with class modules
in Vba, Please elaborate this small example to try to explain as best as possible and
in the simplest way, you see I'm trying to store a variable in a
textbox, (Textbox1) and store it in a Class Module ( C_ct) to be able to
call it at any time and from another instance, in this example I call it
through CommandButton2 but when I delete the information from the textbox it is deleted
also the variable, that is, a blank msgbox is displayed, I have the impression that I need
something at the class module level and in the Initialize event of Userform1, thank you very much
for your contributions I will be attentive to your contributions
VBA Code:
[CODE=vba]

'in the textbox

Private Sub TextBox1_Change()
miClase.nuevoValor = TextBox1.Value
End Sub

'in the commandbutton1

Private Sub CommandButton1_Click()

Dim miClase As New C_ct

    miClase.miValor = TextBox1.Value
    MsgBox miClase.miValor


End Sub



'In the class module I declare it
'as follows


Private miNuevoValor As String

Public Property Let nuevoValor(ByVal nuevoValor As String)
    miNuevoValor = nuevoValor
End Property

Public Property Get miValor() As String
    miValor = miNuevoValor
End Property

Private Sub Class_Initialize()
    miNuevoValor = UserForm1.TextBox1.Value
End Sub




'And to call it from another instance I do it like this

 Private Sub CommandButton2_Click()
 Dim miClase As New C_ct
   
    MsgBox miClase.miValor

End Sub

[/CODE]
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Dim miClase As New C_ct should be placed at the top of the UserForm module otherwise it goes out of scope when exiting CommandButton1_Click routine.
 
Upvote 0
Dim miClase As New C_ct should be placed at the top of the UserForm module otherwise it goes out of scope when exiting CommandButton1_Click routine.
I already did it but I think I should declare it as a static type but I have problems declaring the static variable at the class module level, that is, establishing the Let, set, get of this type of variables in a class model
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,107
Members
453,021
Latest member
Justyna P

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