ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Reset a User Defined Types Values (https://www.excelbanter.com/excel-programming/420275-reset-user-defined-types-values.html)

RyanH

Reset a User Defined Types Values
 
I have a UDT that is used by a userform to calculate the price of a product.
Is there a way to reset the UDT? For example, you can reset an object with
Set Object = Nothing, or reset a variable with Variable = Empty.

The problem occurs when I fill out the userform, set Checkbox1 = True, and
click my CommandButton1 below, a price calculates and the userform remains
open. But if I set Checkbox1 = False then click the buttton, the price
remains the same. The price should be $300 less. This is why I need to
reset the UDT. I have alot of If...Then statements and I would prefer not to
do Else .Illuminated = 0 .Mounted = 0.

'in standard module
Type Sign
Height As Double
Width As Double
Illuminated As Currency
Mounted As Currency
End Type

' userform code
Option Explicit
Base Option 1

Dim TEST As Sign

Sub CommandButtton1_Click()

With TEST
.Height = textbox1
.Width = textbox2
If Checkbox1 = True Then
.Illuminated = 100
.Mounted = 200
End If
End With
End Sub
--
Cheers,
Ryan

joel

Reset a User Defined Types Values
 
Use an else in you IF statement


Sub CommandButtton1_Click()

With TEST
.Height = textbox1
.Width = textbox2
If Checkbox1 = True Then
.Illuminated = 100
.Mounted = 200
Else
.Illuminated = 0
.Mounted = 0
End If
End With
End Sub



"RyanH" wrote:

I have a UDT that is used by a userform to calculate the price of a product.
Is there a way to reset the UDT? For example, you can reset an object with
Set Object = Nothing, or reset a variable with Variable = Empty.

The problem occurs when I fill out the userform, set Checkbox1 = True, and
click my CommandButton1 below, a price calculates and the userform remains
open. But if I set Checkbox1 = False then click the buttton, the price
remains the same. The price should be $300 less. This is why I need to
reset the UDT. I have alot of If...Then statements and I would prefer not to
do Else .Illuminated = 0 .Mounted = 0.

'in standard module
Type Sign
Height As Double
Width As Double
Illuminated As Currency
Mounted As Currency
End Type

' userform code
Option Explicit
Base Option 1

Dim TEST As Sign

Sub CommandButtton1_Click()

With TEST
.Height = textbox1
.Width = textbox2
If Checkbox1 = True Then
.Illuminated = 100
.Mounted = 200
End If
End With
End Sub
--
Cheers,
Ryan


UglyChicken

Reset a User Defined Types Values
 
On Nov 20, 8:35*am, RyanH wrote:
I have a UDT that is used by a userform to calculate the price of a product. *
Is there a way to reset the UDT? *For example, you can reset an object with
Set Object = Nothing, or reset a variable with Variable = Empty.

The problem occurs when I fill out the userform, set Checkbox1 = True, and
click my CommandButton1 below, a price calculates and the userform remains
open. *But if I set Checkbox1 = False then click the buttton, the price
remains the same. *The price should be $300 less. *This is why I need to
reset the UDT. *I have alot of If...Then statements and I would prefer not to
do Else .Illuminated = 0 .Mounted = 0.

'in standard module
Type Sign
* *Height As Double
* *Width As Double
* *Illuminated As Currency
* *Mounted As Currency
End Type

' userform code
Option Explicit
Base Option 1

Dim TEST As Sign

Sub CommandButtton1_Click()

* *With TEST
* * * .Height = textbox1
* * * .Width = textbox2
* * * If Checkbox1 = True Then
* * * * *.Illuminated = 100
* * * * *.Mounted = 200
* * * End If
* *End With
End Sub
--
Cheers,
Ryan


Try placing Dim TEST as sign within the commandButton procedure. It's
scope then will mean it is "reset" each time the procedure is run

Chip Pearson

Reset a User Defined Types Values
 

There is no built-in way to do it. You could, however, write a very
simple procedure that resets the values, and then call that procedure,
passing it the UDT to reset. For example,


Type Sign
Height As Double
Width As Double
Illuminated As Currency
Mounted As Currency
End Type

Sub ResetUDT(ByRef UDT As Sign)
' this resets the values of the Sign UDT
' back to defaults
With UDT
.Height = 0
.Illuminated = 0
.Mounted = 0
.Width = 0
End With
End Sub


Here, the ResetUDT procedure sets the elements back to defaults. You
can use this code as shown below, in particular the line marked with
<<<<. Once you have the ResetUDT proc written, it is a one-liner to
reset the UDT back to defaults.


Sub Test()
Dim UDT As Sign
' setup some values and print them
With UDT
.Height = 100
.Width = 200
.Illuminated = 120
.Mounted = 1234
Debug.Print .Height, .Illuminated, .Mounted, .Width
End With

' reset the UDT back to factory defaults
ResetUDT UDT ' <<<< RESET UDT

' confirm reset values
With UDT
Debug.Print .Height, .Illuminated, .Mounted, .Width
End With
End Sub

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




On Wed, 19 Nov 2008 11:35:01 -0800, RyanH
wrote:

I have a UDT that is used by a userform to calculate the price of a product.
Is there a way to reset the UDT? For example, you can reset an object with
Set Object = Nothing, or reset a variable with Variable = Empty.

The problem occurs when I fill out the userform, set Checkbox1 = True, and
click my CommandButton1 below, a price calculates and the userform remains
open. But if I set Checkbox1 = False then click the buttton, the price
remains the same. The price should be $300 less. This is why I need to
reset the UDT. I have alot of If...Then statements and I would prefer not to
do Else .Illuminated = 0 .Mounted = 0.

'in standard module
Type Sign
Height As Double
Width As Double
Illuminated As Currency
Mounted As Currency
End Type

' userform code
Option Explicit
Base Option 1

Dim TEST As Sign

Sub CommandButtton1_Click()

With TEST
.Height = textbox1
.Width = textbox2
If Checkbox1 = True Then
.Illuminated = 100
.Mounted = 200
End If
End With
End Sub


Per Jessen[_2_]

Reset a User Defined Types Values
 
Hi Ryan

As I see it you will have to use Else statements unless you can
set .Illuminated = 0 .Mounted = 0 at the beginning of the Sub.

Hopes it helps.

Regards,
Per

On 19 Nov., 20:35, RyanH wrote:
I have a UDT that is used by a userform to calculate the price of a product. *
Is there a way to reset the UDT? *For example, you can reset an object with
Set Object = Nothing, or reset a variable with Variable = Empty.

The problem occurs when I fill out the userform, set Checkbox1 = True, and
click my CommandButton1 below, a price calculates and the userform remains
open. *But if I set Checkbox1 = False then click the buttton, the price
remains the same. *The price should be $300 less. *This is why I need to
reset the UDT. *I have alot of If...Then statements and I would prefer not to
do Else .Illuminated = 0 .Mounted = 0.

'in standard module
Type Sign
* *Height As Double
* *Width As Double
* *Illuminated As Currency
* *Mounted As Currency
End Type

' userform code
Option Explicit
Base Option 1

Dim TEST As Sign

Sub CommandButtton1_Click()

* *With TEST
* * * .Height = textbox1
* * * .Width = textbox2
* * * If Checkbox1 = True Then
* * * * *.Illuminated = 100
* * * * *.Mounted = 200
* * * End If
* *End With
End Sub
--
Cheers,
Ryan



RyanH

Reset a User Defined Types Values
 
Chip,

Do you have any information on UDTs on your website? If not, do you know a
good reference website or book with advanced programming text for VBA?

Is there a way to loop thru the UDT? I will need to reset the UDT for 15
Userforms so it would be benifical to have a "one size fits all" procedure to
reset the UDT instead of explicitly setting each variable in the type to 0.
For example,

Sub ResetUDT(ByRef UDT As Variant)

Dim var As Double

For Each var In UDT
var = 0
Next var

End Sub
--
Cheers,
Ryan


"Chip Pearson" wrote:


There is no built-in way to do it. You could, however, write a very
simple procedure that resets the values, and then call that procedure,
passing it the UDT to reset. For example,


Type Sign
Height As Double
Width As Double
Illuminated As Currency
Mounted As Currency
End Type

Sub ResetUDT(ByRef UDT As Sign)
' this resets the values of the Sign UDT
' back to defaults
With UDT
.Height = 0
.Illuminated = 0
.Mounted = 0
.Width = 0
End With
End Sub


Here, the ResetUDT procedure sets the elements back to defaults. You
can use this code as shown below, in particular the line marked with
<<<<. Once you have the ResetUDT proc written, it is a one-liner to
reset the UDT back to defaults.


Sub Test()
Dim UDT As Sign
' setup some values and print them
With UDT
.Height = 100
.Width = 200
.Illuminated = 120
.Mounted = 1234
Debug.Print .Height, .Illuminated, .Mounted, .Width
End With

' reset the UDT back to factory defaults
ResetUDT UDT ' <<<< RESET UDT

' confirm reset values
With UDT
Debug.Print .Height, .Illuminated, .Mounted, .Width
End With
End Sub

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




On Wed, 19 Nov 2008 11:35:01 -0800, RyanH
wrote:

I have a UDT that is used by a userform to calculate the price of a product.
Is there a way to reset the UDT? For example, you can reset an object with
Set Object = Nothing, or reset a variable with Variable = Empty.

The problem occurs when I fill out the userform, set Checkbox1 = True, and
click my CommandButton1 below, a price calculates and the userform remains
open. But if I set Checkbox1 = False then click the buttton, the price
remains the same. The price should be $300 less. This is why I need to
reset the UDT. I have alot of If...Then statements and I would prefer not to
do Else .Illuminated = 0 .Mounted = 0.

'in standard module
Type Sign
Height As Double
Width As Double
Illuminated As Currency
Mounted As Currency
End Type

' userform code
Option Explicit
Base Option 1

Dim TEST As Sign

Sub CommandButtton1_Click()

With TEST
.Height = textbox1
.Width = textbox2
If Checkbox1 = True Then
.Illuminated = 100
.Mounted = 200
End If
End With
End Sub



Jim Thomlinson

Reset a User Defined Types Values
 
Your one size fits all will not work that well. The problem is that a UDT can
hold anything. Ranges, Worksheets, Arrays, string, numbers... You can just
reset a udt...
--
HTH...

Jim Thomlinson


"RyanH" wrote:

Chip,

Do you have any information on UDTs on your website? If not, do you know a
good reference website or book with advanced programming text for VBA?

Is there a way to loop thru the UDT? I will need to reset the UDT for 15
Userforms so it would be benifical to have a "one size fits all" procedure to
reset the UDT instead of explicitly setting each variable in the type to 0.
For example,

Sub ResetUDT(ByRef UDT As Variant)

Dim var As Double

For Each var In UDT
var = 0
Next var

End Sub
--
Cheers,
Ryan


"Chip Pearson" wrote:


There is no built-in way to do it. You could, however, write a very
simple procedure that resets the values, and then call that procedure,
passing it the UDT to reset. For example,


Type Sign
Height As Double
Width As Double
Illuminated As Currency
Mounted As Currency
End Type

Sub ResetUDT(ByRef UDT As Sign)
' this resets the values of the Sign UDT
' back to defaults
With UDT
.Height = 0
.Illuminated = 0
.Mounted = 0
.Width = 0
End With
End Sub


Here, the ResetUDT procedure sets the elements back to defaults. You
can use this code as shown below, in particular the line marked with
<<<<. Once you have the ResetUDT proc written, it is a one-liner to
reset the UDT back to defaults.


Sub Test()
Dim UDT As Sign
' setup some values and print them
With UDT
.Height = 100
.Width = 200
.Illuminated = 120
.Mounted = 1234
Debug.Print .Height, .Illuminated, .Mounted, .Width
End With

' reset the UDT back to factory defaults
ResetUDT UDT ' <<<< RESET UDT

' confirm reset values
With UDT
Debug.Print .Height, .Illuminated, .Mounted, .Width
End With
End Sub

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




On Wed, 19 Nov 2008 11:35:01 -0800, RyanH
wrote:

I have a UDT that is used by a userform to calculate the price of a product.
Is there a way to reset the UDT? For example, you can reset an object with
Set Object = Nothing, or reset a variable with Variable = Empty.

The problem occurs when I fill out the userform, set Checkbox1 = True, and
click my CommandButton1 below, a price calculates and the userform remains
open. But if I set Checkbox1 = False then click the buttton, the price
remains the same. The price should be $300 less. This is why I need to
reset the UDT. I have alot of If...Then statements and I would prefer not to
do Else .Illuminated = 0 .Mounted = 0.

'in standard module
Type Sign
Height As Double
Width As Double
Illuminated As Currency
Mounted As Currency
End Type

' userform code
Option Explicit
Base Option 1

Dim TEST As Sign

Sub CommandButtton1_Click()

With TEST
.Height = textbox1
.Width = textbox2
If Checkbox1 = True Then
.Illuminated = 100
.Mounted = 200
End If
End With
End Sub



Chip Pearson

Reset a User Defined Types Values
 
Do you have any information on UDTs on your website? If not, do you know a
good reference website or book with advanced programming text for VBA?


I have little explanatory material on the web site regard UDTs, and
that is only as prelude to the discussion on classes and objects. You
can't loop through the contents of a UDT. The is no way to write a
generic proc to reset a UDT. You have to have one for each type.

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Wed, 19 Nov 2008 12:23:15 -0800, RyanH
wrote:

Chip,

Do you have any information on UDTs on your website? If not, do you know a
good reference website or book with advanced programming text for VBA?

Is there a way to loop thru the UDT? I will need to reset the UDT for 15
Userforms so it would be benifical to have a "one size fits all" procedure to
reset the UDT instead of explicitly setting each variable in the type to 0.
For example,

Sub ResetUDT(ByRef UDT As Variant)

Dim var As Double

For Each var In UDT
var = 0
Next var

End Sub



All times are GMT +1. The time now is 05:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com