Define Variable using UDF?

CVDK

New Member
Joined
Jul 22, 2009
Messages
35
Hi, I thought I could do this thing but no. Therefore I'm looking for help from someone smarter than me.

I'm creating "adress labels" and "order labels" using VBA macro based on a list in matrix form.

I've used links to the list which I copy and paste to another sheet that's going to be printed by someone. Doing this all the way throug the list could take som time. Therefore I created a macro to do the job. I use the macro to edit/correct the links so the labels are made using every row.

In order to correct the links and use the next row i used a function (ExtractNumber, found on Ozgrid.com, thank you!) to extract numbers from strings. I converted the formula/link to a string and put it in a variable, Cformula.

I Dimmed my variable HNumber and tried to define it using the function which did not work. I get the error messag ByRef argument type mismatch. Actually no matter what I put in the () I get the same error message.

Code:
HNumber = ExtractNumber(Cformula)

If I use the code below, there's no problem

Code:
HNumber = Right(Cformula,2)

Unfortunately there can be both 1, 2 and 3 digits. I could use a select case or if statement but I just want to understand why I'm having problems.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
How is CFormula declared? As String or Variant or other?
 
Upvote 0
CFormula is declared as String but I also tried variant. HNumber is also declared as String.

The Extractnumber function output is declared as Double if I understand the below correct?

Code:
Function Extractnumber(rCell As Range, _
     Optional Take_decimal As Boolean, Optional Take_negative As Boolean) As Double

This is what VBA help has to say about the error:


"An argument passed ByRef (by reference), the default, must have the precise data type expected in the procedure. This error has the following cause and solution:
  • You passed an argument of one type that could not be coerced to the type expected. "
 
Upvote 0
There's your issue then - ExtractNumber is expecting a Range, not a String.
 
Upvote 0
So the problem is that the content of the cell is a formula, which I can't turn into an object. I can define the activecell as an object but it only works when the activecell does not contain a formula or it takes the formula output and extracts the number(s) from that.

I guess the solution is to rebuild/change the function?
 
Upvote 0
If the formula is in a cell, then you can probably just pass the cell to the function rather than its formula.
 
Upvote 0
Ha, it worked. Thank you for your inspiration.

It was incredibly simple, I just added ".formula" to the input string variable definition of the ExtractNumber function. (Did that make sense?)
 
Upvote 0
Not really, but if it works that's fine. :)
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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