Type / Function Help

Darren Arnold

New Member
Joined
Jul 20, 2011
Messages
10
OK, so I'm trying to return values for an object based on it's name.

Let's say it's a brick, and I call it as Brick("S") to say it's a standard brick.

Inside the function it determines the various dimensions of an "S" brick, I want to return those valuses without writing a function to return each individually.

So I want, from the Spreadsheet Cell to simply call it as say

=BRICK("S").LENGTH

and have it return a value in the cell.

I want to be able to do others like =BRICK("S").WIDTH and =BRICK("S").WIDTH*BRICK("S").HEIGHT

Now.. this is as far as I get:

I defined a type called Dimensions in Visual Basic:

Public Type Dimensions

Area As Double
Length As Double
Height As Double
Volume As Double
Width As Double

End Type

So far so good.

I scratched up a test function:

Function Brick() As Dimensions

Brick.Area = 0
Brick.Length = 230
Brick.Height = 0
Brick.Volumne = 0
Brick.Width = 0

End Function

If I call it as =Brick.Length in the spreadhseet it pops up the VB windows gives me:
"User defined type not defined"

If I call it as Brick().Length it says the formula I typed contains an error


Help!!!

I think logically I'm on the right track but having one of those derailments you have at 4am when you forget to factor in that sine can go negative or that some twit put a 0 where it shouldn't be and everything tried to divide by it.

From
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You can't do that in a cell (you're basically attempting to write VBA in a cell). You would have to have a wrapper function that you pass a string containing the name of the property you want back (or an index number) and you'd need a class rather than a UDT so that you can use CallByName.
 
Upvote 0
Hi Rory,

I did all the coding in Visual Basic

Now all I want to do is in the spreadsheet cell call it like:

=BRICK("S").length

Instead of having to write a function that calls Brick and returning a value from the second function.
 
Upvote 0
I should explain, although figuring out Types and getting into visual basic makes me Mr Fancypants at work, I'm not actually very good or understand much of it. What I'm trying to do is refine equations for estimating so I don't have to type complex unreadable formulas like IF(A1=B1,Pi()*(A1/2)^2)),IF(A1=C1.......... you get the drift.

Is there any way to say =a(1).b in a cell and get a formula that way and if so, how, pretty much precisley, because I'm an idiot
 
Upvote 0
I know, but you can't do that. You're effectively trying to refer to an object in a cell and that won't work.
 
Upvote 0
At the most simple level:
1. Add a class module and rename it Dimensions.
in its code module add:
Code:
Public Area As Double
Public Length As Double
Public Height As Double
Public Volume As Double
Public Width As Double

2. Remove your User-defined Type.
3. Change the function code to:
Code:
Function Brick(strType As String)
   Dim objDim As Dimensions
   Set objDim = New Dimensions
   With objDim
      .Area = 0
      .Length = 230
      .Height = 0
      .Volume = 0
      .Width = 0
   End With
   Brick = CallByName(objDim, strType, VbGet)
End Function

4. Change the cell call to:
=Brick("Length")
 
Upvote 0
OK, Thanks, Rory, back to when I started then which was individual function names. I think the medium I'm going to have to go for is


=BrickDimension("Type","Length") and code the rest to return a value based on the parameters. I think that's how the CELL function works.

I do appreciate your swift answer, probably saved me 12 hours of swearing in the middle of the night determined that it MUST be possible.
 
Upvote 0
OK, I kind of see that, Rory, I'll wrap my head around it. Instead of doing a whole bunch of case statements to figure out what is being done, that will allow me just to name it and let it find itself.

Awesome.

As soon as I unfry my brain and understand it, I'll probably do as you suggested. Thanks again.
 
Upvote 0
I know, but you can't do that. You're effectively trying to refer to an object in a cell and that won't work.
OK that worked for me, less elegant than I liked but I don't own a gold plated toilet either.

I really appreciate your help - here's what I got:

Public Area As Double
Public Depth As Double
Public Length As Double
Public Height As Double
Public Volume As Double
Public Width As Double

Function Brick(BrickType, BrickDimension As String)

Dim BrickDimensions As Dimensions
Set BrickDimensions = New Dimensions
With BrickDimensions
.Area = 0
.Depth = 0
.Length = 0
.Height = 0
.Volume = 0
.Width = 0
End With

Select Case UCase(BrickType)
Case "ST", "SD", "STD", "STA", "STAN", "STANDARD"
With BrickDimensions
.Area = 0.01748
.Depth = 0
.Length = 110
.Height = 76
.Volume = 0.0019228
.Width = 230
End With
Case "MA", "MB", "MAX", "MXI", "MAXI", "MAXIBRICK"
With BrickDimensions
.Area = 0.04941
.Depth = 0
.Length = 90
.Height = 162
.Volume = 0.004469
.Width = 305
End With
Case "LO", "LR", "LON", "LOR", "LONG", "LONGREACH"
With BrickDimensions
.Area = 0.02318
.Depth = 0
.Length = 90
.Height = 76
.Volume = 0.0020862
.Width = 305
End With
Case Else
With BrickDimensions
.Area = 0
.Depth = 0
.Length = 0
.Height = 0
.Volume = 0
.Width = 0
End With
End Select

Brick = CallByName(BrickDimensions, BrickDimension, VbGet)

End Function

Apart from the dangerous BrickDimension/BrickDimensions names being close, it otherwise does what I want.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,578
Messages
6,179,654
Members
452,934
Latest member
mm1t1

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