ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   User-defined Type Issue (https://www.excelbanter.com/excel-programming/435146-user-defined-type-issue.html)

Ryan H

User-defined Type Issue
 
I have a UDF below that is located in a Standard Module.

Type Totals
COGS As Double
Material As Double
Labor As Double
UnitPrice As Currency
QuotePrice As Currency
End Type

I declared a Public Variable in another Standard Module like this.

Public Total As Totals

For some reason when I use Total with the With...End With Statement in the
code below VBA is throwing an exception stating "With object must be
user-defined type, variant or object". Anybody know why?

Sub Test()

With Total

' determines labor effiency % and COGS %
Select Case Val(tbxQuantity)
Case Is = 1
.COGS = 1.1
Case Is = 2
.COGS = 1.08
Case Is = 3
.COGS = 1.05
End Select

.Material = 300
.Labor = 1000
End With

End Sub

--
Cheers,
Ryan

Patrick Molloy[_2_]

User-defined Type Issue
 
to use totals type you'd need to be absolute

ie

some_material = total.material

an alternative would be to open a new CLASS MODULE, name it cTotals and give
it the variables that you gave to the type.

now you'd SET total = New cTotals

as total is an object of class cTotals, you can use the WITH method as you
showed.












"Ryan H" wrote:

I have a UDF below that is located in a Standard Module.

Type Totals
COGS As Double
Material As Double
Labor As Double
UnitPrice As Currency
QuotePrice As Currency
End Type

I declared a Public Variable in another Standard Module like this.

Public Total As Totals

For some reason when I use Total with the With...End With Statement in the
code below VBA is throwing an exception stating "With object must be
user-defined type, variant or object". Anybody know why?

Sub Test()

With Total

' determines labor effiency % and COGS %
Select Case Val(tbxQuantity)
Case Is = 1
.COGS = 1.1
Case Is = 2
.COGS = 1.08
Case Is = 3
.COGS = 1.05
End Select

.Material = 300
.Labor = 1000
End With

End Sub

--
Cheers,
Ryan


Ryan H

User-defined Type Issue
 
I reed the with statement help and it said it could be used with any
user-defined type. Do you know why it wouldn't work in my application?


--
Cheers,
Ryan


"Patrick Molloy" wrote:

to use totals type you'd need to be absolute

ie

some_material = total.material

an alternative would be to open a new CLASS MODULE, name it cTotals and give
it the variables that you gave to the type.

now you'd SET total = New cTotals

as total is an object of class cTotals, you can use the WITH method as you
showed.












"Ryan H" wrote:

I have a UDF below that is located in a Standard Module.

Type Totals
COGS As Double
Material As Double
Labor As Double
UnitPrice As Currency
QuotePrice As Currency
End Type

I declared a Public Variable in another Standard Module like this.

Public Total As Totals

For some reason when I use Total with the With...End With Statement in the
code below VBA is throwing an exception stating "With object must be
user-defined type, variant or object". Anybody know why?

Sub Test()

With Total

' determines labor effiency % and COGS %
Select Case Val(tbxQuantity)
Case Is = 1
.COGS = 1.1
Case Is = 2
.COGS = 1.08
Case Is = 3
.COGS = 1.05
End Select

.Material = 300
.Labor = 1000
End With

End Sub

--
Cheers,
Ryan


Patrick Molloy[_2_]

User-defined Type Issue
 
Your code was fine - all I had to do was set some value in tbxQuantity

making sure
OPTION EXPLICIT
is at the top of each module would have highlighted this

"Ryan H" wrote:

I reed the with statement help and it said it could be used with any
user-defined type. Do you know why it wouldn't work in my application?


--
Cheers,
Ryan


"Patrick Molloy" wrote:

to use totals type you'd need to be absolute

ie

some_material = total.material

an alternative would be to open a new CLASS MODULE, name it cTotals and give
it the variables that you gave to the type.

now you'd SET total = New cTotals

as total is an object of class cTotals, you can use the WITH method as you
showed.












"Ryan H" wrote:

I have a UDF below that is located in a Standard Module.

Type Totals
COGS As Double
Material As Double
Labor As Double
UnitPrice As Currency
QuotePrice As Currency
End Type

I declared a Public Variable in another Standard Module like this.

Public Total As Totals

For some reason when I use Total with the With...End With Statement in the
code below VBA is throwing an exception stating "With object must be
user-defined type, variant or object". Anybody know why?

Sub Test()

With Total

' determines labor effiency % and COGS %
Select Case Val(tbxQuantity)
Case Is = 1
.COGS = 1.1
Case Is = 2
.COGS = 1.08
Case Is = 3
.COGS = 1.05
End Select

.Material = 300
.Labor = 1000
End With

End Sub

--
Cheers,
Ryan


Rick Rothstein

User-defined Type Issue
 
I just tried it out and your code worked fine in my test. When it fails,
what line does debugger stop on and what is the error message it shows you?

--
Rick (MVP - Excel)


"Ryan H" wrote in message
...
I reed the with statement help and it said it could be used with any
user-defined type. Do you know why it wouldn't work in my application?


--
Cheers,
Ryan


"Patrick Molloy" wrote:

to use totals type you'd need to be absolute

ie

some_material = total.material

an alternative would be to open a new CLASS MODULE, name it cTotals and
give
it the variables that you gave to the type.

now you'd SET total = New cTotals

as total is an object of class cTotals, you can use the WITH method as
you
showed.












"Ryan H" wrote:

I have a UDF below that is located in a Standard Module.

Type Totals
COGS As Double
Material As Double
Labor As Double
UnitPrice As Currency
QuotePrice As Currency
End Type

I declared a Public Variable in another Standard Module like this.

Public Total As Totals

For some reason when I use Total with the With...End With Statement in
the
code below VBA is throwing an exception stating "With object must be
user-defined type, variant or object". Anybody know why?

Sub Test()

With Total

' determines labor effiency % and COGS %
Select Case Val(tbxQuantity)
Case Is = 1
.COGS = 1.1
Case Is = 2
.COGS = 1.08
Case Is = 3
.COGS = 1.05
End Select

.Material = 300
.Labor = 1000
End With

End Sub

--
Cheers,
Ryan



Ryan H

User-defined Type Issue
 
Yes I have OPTION EXPLICIT at the top of all my modules. But I'm really
scratching my head on why the compile error is happening saying "With object
must be user-defined type, variant or object". My Total is a UDF, so why am
I still getting this error?

Thanks in advance!
--
Cheers,
Ryan


"Patrick Molloy" wrote:

Your code was fine - all I had to do was set some value in tbxQuantity

making sure
OPTION EXPLICIT
is at the top of each module would have highlighted this

"Ryan H" wrote:

I reed the with statement help and it said it could be used with any
user-defined type. Do you know why it wouldn't work in my application?


--
Cheers,
Ryan


"Patrick Molloy" wrote:

to use totals type you'd need to be absolute

ie

some_material = total.material

an alternative would be to open a new CLASS MODULE, name it cTotals and give
it the variables that you gave to the type.

now you'd SET total = New cTotals

as total is an object of class cTotals, you can use the WITH method as you
showed.












"Ryan H" wrote:

I have a UDF below that is located in a Standard Module.

Type Totals
COGS As Double
Material As Double
Labor As Double
UnitPrice As Currency
QuotePrice As Currency
End Type

I declared a Public Variable in another Standard Module like this.

Public Total As Totals

For some reason when I use Total with the With...End With Statement in the
code below VBA is throwing an exception stating "With object must be
user-defined type, variant or object". Anybody know why?

Sub Test()

With Total

' determines labor effiency % and COGS %
Select Case Val(tbxQuantity)
Case Is = 1
.COGS = 1.1
Case Is = 2
.COGS = 1.08
Case Is = 3
.COGS = 1.05
End Select

.Material = 300
.Labor = 1000
End With

End Sub

--
Cheers,
Ryan


Ryan H

User-defined Type Issue
 
If I put Dim Total As Totals at the top of each module the code wants to
work. But for some reason VBA does not want to recognize Public Total As
Totals in another module, why?

--
Cheers,
Ryan


"Patrick Molloy" wrote:

Your code was fine - all I had to do was set some value in tbxQuantity

making sure
OPTION EXPLICIT
is at the top of each module would have highlighted this

"Ryan H" wrote:

I reed the with statement help and it said it could be used with any
user-defined type. Do you know why it wouldn't work in my application?


--
Cheers,
Ryan


"Patrick Molloy" wrote:

to use totals type you'd need to be absolute

ie

some_material = total.material

an alternative would be to open a new CLASS MODULE, name it cTotals and give
it the variables that you gave to the type.

now you'd SET total = New cTotals

as total is an object of class cTotals, you can use the WITH method as you
showed.












"Ryan H" wrote:

I have a UDF below that is located in a Standard Module.

Type Totals
COGS As Double
Material As Double
Labor As Double
UnitPrice As Currency
QuotePrice As Currency
End Type

I declared a Public Variable in another Standard Module like this.

Public Total As Totals

For some reason when I use Total with the With...End With Statement in the
code below VBA is throwing an exception stating "With object must be
user-defined type, variant or object". Anybody know why?

Sub Test()

With Total

' determines labor effiency % and COGS %
Select Case Val(tbxQuantity)
Case Is = 1
.COGS = 1.1
Case Is = 2
.COGS = 1.08
Case Is = 3
.COGS = 1.05
End Select

.Material = 300
.Labor = 1000
End With

End Sub

--
Cheers,
Ryan


Dave Peterson

User-defined Type Issue
 
Just a guess...

You didn't declare another variable (within that sub or at module level in that
sub's module) as Total, did you?

If you did, then your code will find that one before it sees the public
declaration in a different module.

Ryan H wrote:

I have a UDF below that is located in a Standard Module.

Type Totals
COGS As Double
Material As Double
Labor As Double
UnitPrice As Currency
QuotePrice As Currency
End Type

I declared a Public Variable in another Standard Module like this.

Public Total As Totals

For some reason when I use Total with the With...End With Statement in the
code below VBA is throwing an exception stating "With object must be
user-defined type, variant or object". Anybody know why?

Sub Test()

With Total

' determines labor effiency % and COGS %
Select Case Val(tbxQuantity)
Case Is = 1
.COGS = 1.1
Case Is = 2
.COGS = 1.08
Case Is = 3
.COGS = 1.05
End Select

.Material = 300
.Labor = 1000
End With

End Sub

--
Cheers,
Ryan


--

Dave Peterson

Ryan H

User-defined Type Issue
 
Ok figured out why. The Module Name was Total as well. Excel must have
gotten the module Name Total and the Type Total confused. Everything works
fine now.

Thanks for the help!
--
Cheers,
Ryan


"Ryan H" wrote:

If I put Dim Total As Totals at the top of each module the code wants to
work. But for some reason VBA does not want to recognize Public Total As
Totals in another module, why?

--
Cheers,
Ryan


"Patrick Molloy" wrote:

Your code was fine - all I had to do was set some value in tbxQuantity

making sure
OPTION EXPLICIT
is at the top of each module would have highlighted this

"Ryan H" wrote:

I reed the with statement help and it said it could be used with any
user-defined type. Do you know why it wouldn't work in my application?


--
Cheers,
Ryan


"Patrick Molloy" wrote:

to use totals type you'd need to be absolute

ie

some_material = total.material

an alternative would be to open a new CLASS MODULE, name it cTotals and give
it the variables that you gave to the type.

now you'd SET total = New cTotals

as total is an object of class cTotals, you can use the WITH method as you
showed.












"Ryan H" wrote:

I have a UDF below that is located in a Standard Module.

Type Totals
COGS As Double
Material As Double
Labor As Double
UnitPrice As Currency
QuotePrice As Currency
End Type

I declared a Public Variable in another Standard Module like this.

Public Total As Totals

For some reason when I use Total with the With...End With Statement in the
code below VBA is throwing an exception stating "With object must be
user-defined type, variant or object". Anybody know why?

Sub Test()

With Total

' determines labor effiency % and COGS %
Select Case Val(tbxQuantity)
Case Is = 1
.COGS = 1.1
Case Is = 2
.COGS = 1.08
Case Is = 3
.COGS = 1.05
End Select

.Material = 300
.Labor = 1000
End With

End Sub

--
Cheers,
Ryan


Jacob Skaria

User-defined Type Issue
 
It is considered as a good practice to follow some sort of naming convention
for variables, modules, userforms, user controls etc; so as to avoid such
confusions ..

modTotal/gmdTotal to represent module/general module
typTotal to represent custom Type..
frmMain to represent userform
strMessage to represent string variable
cmdOK to represent command button
clsMain to represent Class module

If this post helps click Yes
---------------
Jacob Skaria


"Ryan H" wrote:

Ok figured out why. The Module Name was Total as well. Excel must have
gotten the module Name Total and the Type Total confused. Everything works
fine now.

Thanks for the help!
--
Cheers,
Ryan


"Ryan H" wrote:

If I put Dim Total As Totals at the top of each module the code wants to
work. But for some reason VBA does not want to recognize Public Total As
Totals in another module, why?

--
Cheers,
Ryan


"Patrick Molloy" wrote:

Your code was fine - all I had to do was set some value in tbxQuantity

making sure
OPTION EXPLICIT
is at the top of each module would have highlighted this

"Ryan H" wrote:

I reed the with statement help and it said it could be used with any
user-defined type. Do you know why it wouldn't work in my application?


--
Cheers,
Ryan


"Patrick Molloy" wrote:

to use totals type you'd need to be absolute

ie

some_material = total.material

an alternative would be to open a new CLASS MODULE, name it cTotals and give
it the variables that you gave to the type.

now you'd SET total = New cTotals

as total is an object of class cTotals, you can use the WITH method as you
showed.












"Ryan H" wrote:

I have a UDF below that is located in a Standard Module.

Type Totals
COGS As Double
Material As Double
Labor As Double
UnitPrice As Currency
QuotePrice As Currency
End Type

I declared a Public Variable in another Standard Module like this.

Public Total As Totals

For some reason when I use Total with the With...End With Statement in the
code below VBA is throwing an exception stating "With object must be
user-defined type, variant or object". Anybody know why?

Sub Test()

With Total

' determines labor effiency % and COGS %
Select Case Val(tbxQuantity)
Case Is = 1
.COGS = 1.1
Case Is = 2
.COGS = 1.08
Case Is = 3
.COGS = 1.05
End Select

.Material = 300
.Labor = 1000
End With

End Sub

--
Cheers,
Ryan


Patrick Molloy[_2_]

User-defined Type Issue
 
i wonder if the issue might have been picked up if you'd celected DEBUG /
COMPILE from the menu? I got "Expected variable or procedure, not module"



"Ryan H" wrote:

Ok figured out why. The Module Name was Total as well. Excel must have
gotten the module Name Total and the Type Total confused. Everything works
fine now.

Thanks for the help!
--
Cheers,
Ryan


"Ryan H" wrote:

If I put Dim Total As Totals at the top of each module the code wants to
work. But for some reason VBA does not want to recognize Public Total As
Totals in another module, why?

--
Cheers,
Ryan


"Patrick Molloy" wrote:

Your code was fine - all I had to do was set some value in tbxQuantity

making sure
OPTION EXPLICIT
is at the top of each module would have highlighted this

"Ryan H" wrote:

I reed the with statement help and it said it could be used with any
user-defined type. Do you know why it wouldn't work in my application?


--
Cheers,
Ryan


"Patrick Molloy" wrote:

to use totals type you'd need to be absolute

ie

some_material = total.material

an alternative would be to open a new CLASS MODULE, name it cTotals and give
it the variables that you gave to the type.

now you'd SET total = New cTotals

as total is an object of class cTotals, you can use the WITH method as you
showed.












"Ryan H" wrote:

I have a UDF below that is located in a Standard Module.

Type Totals
COGS As Double
Material As Double
Labor As Double
UnitPrice As Currency
QuotePrice As Currency
End Type

I declared a Public Variable in another Standard Module like this.

Public Total As Totals

For some reason when I use Total with the With...End With Statement in the
code below VBA is throwing an exception stating "With object must be
user-defined type, variant or object". Anybody know why?

Sub Test()

With Total

' determines labor effiency % and COGS %
Select Case Val(tbxQuantity)
Case Is = 1
.COGS = 1.1
Case Is = 2
.COGS = 1.08
Case Is = 3
.COGS = 1.05
End Select

.Material = 300
.Labor = 1000
End With

End Sub

--
Cheers,
Ryan



All times are GMT +1. The time now is 08:24 AM.

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