VBA Classes

PanzerRanger

New Member
Joined
Jan 3, 2018
Messages
20
Hi!

Is it possible to get and let data between classes, and if yes, how?

delclaring as follows resets the information in the class despite that the class has´nt been terminated and I have´nt figured out how to calling it in a other way
Dim x as class1
Set x = new class1

Thanks!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I think you'll need to give more information. The code you posted is just creating a new instance of a class, not passing any information anywhere.
 
Upvote 0
I think you'll need to give more information. The code you posted is just creating a new instance of a class, not passing any information anywhere.

The problem, as I think, is I have multiple subs and in those I need to get information from the class i.e.

Sub1
Dim p as project
Set p = new project
yada yadha
End Sub

sub2dim p as project
set p = new project
yada yadha
end sub

As sub2 defines p again it erases/resets the values in the class project which holds information that is needed. I´ve tried
Dim p as Object and Set p = project but neither of thoses works
 
Upvote 0
If you need both routines to use the same class instance, you need to declare it outside both procedures:

Code:
Dim p as project
Sub1
Set p = new project
yada yadha
End Sub

sub2
yada yadha with p.Property
end sub
 
Upvote 0
I assume that works for a modul but is it possible to somehow pull it off within a class?
 
Last edited:
Upvote 0
It's really not clear what you mean. A class is a module, so that will work.
 
Upvote 0
It's really not clear what you mean. A class is a module, so that will work.

I´ll try and refine the code tonight or tomorrow to show the whole thing. I may been in over my head as Im just starting to lear about classes. What is the best why to post the code, it uses aform with a sub (main sub) and two classes that needs to pass the information between eachother
 
Upvote 0
Just paste the sub, then the code for each class, indicating which is which. And please use code tags when doing so.
 
Upvote 0
Better late then never =/
Code:
'    Sub in Userform
Sub Button_Click
Dim Pplan as PersonalPlaning, P as project
Set Pplan = New PersonalPlaning
Set P = New Project

P.ProjectNumber = Me.ProjectNumber
'    This writes then projectnumber to class which in return gets
'    all the projectinformation from the databas

'Code to loop form values to plan personal

If Pplan.Days = 1 Then Call Pplan.PlanDay
end sub

'    Sub in classmodual PersonalPlaning
Public Sub PlanDay()
    Dim P as project
    Set P = New Project
    
    'lots of code checking for criterias
    
    Day.ClearComments
    Day.AddComment P.ProjectNumber '    Here I need the information from my classmoudal named Project
    Day.Value = P.Project

End Sub

From my understanding, as soon as I declare (in this case in PlanDay sub) a new instance of the project class it terminates the information within even thouh the main sub has´nt ended
Is there anyway for me to get the information from the project class modual to the projectplaner classmoudal without terminating the information with the project class
 
Last edited by a moderator:
Upvote 0
It doesn't terminate anything, but you are creating a new Project instance, which therefore has only its default values. You could simply pass a Project object:

Rich (BB code):
Sub Button_Click
Dim Pplan as PersonalPlaning, P as project
Set Pplan = New PersonalPlaning
Set P = New Project

P.ProjectNumber = Me.ProjectNumber
'    This writes then projectnumber to class which in return gets
'    all the projectinformation from the databas

'Code to loop form values to plan personal

If Pplan.Days = 1 Then Call Pplan.PlanDay(P)
end sub

'    Sub in classmodual PersonalPlaning
Public Sub PlanDay(P as Project)
    
    'lots of code checking for criterias
    
    Day.ClearComments
    Day.AddComment P.ProjectNumber '    Here I need the information from my classmoudal named Project
    Day.Value = P.Project

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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