PDA

View Full Version : Solved: Add-In Help



jmenche
08-16-2006, 01:34 PM
Howdy,

I am trying to write my first Add-In and need a little help.

The worksheet that I am trying to convert has a custom function that loops through named ranges on another sheet. However, when I save the worksheet as an add-in, the function returns an error.

Could somebody take a look?

:beerchug:

mdmackillop
08-16-2006, 02:21 PM
Segmentme is not dimmed.
On which items is the function meant to operate. (Too lazy to decipher the code to determine this)

Bob Phillips
08-16-2006, 03:04 PM
It is not a good idae to hard-code ranges in the UDF, especially if it will work across workbooks, as it will invariably be pointig at the wrong range.

Add 3 range arguiments to the UDF and pass the ranges to the UDF as parameters.

jmenche
08-17-2006, 05:31 AM
Thanks fellas.

Here's my dilemma then. I need to use what is in the ranges as part of the UDF. The UDF will essentially look up what is part of an item description and replace it with a more meaningful word. How can I use the ranges then? I was thinking that I would need arrays and then use the arrays in the UDF for matching and not the ranges. Unfortunately, I am not that savvy.

Bob Phillips
08-17-2006, 07:08 AM
I am suggesting changing it like so



Public Function SegmentFootcare(Item As Range, _
SubbrandsFootcare As Range, _
BrandsFootcare As Range, _
AttributesFootcare As Range) As String
Dim cel As Range
Dim start As Integer

SegmentMe = Item.Value

Subbrands:

For Each cel In SubbrandsFootcare
On Error Resume Next
If InStr(1, SegmentMe, cel.Text, vbTextCompare) = 1 Then
SegmentMe = Replace(SegmentMe, cel.Value, cel.Offset(0, 1).Value)
start = Len(cel.Offset(0, 1))
GoTo Attributes
End If
Next

Brands:

For Each cel In BrandsFootcare
On Error Resume Next
If InStr(1, SegmentMe, cel.Text, vbTextCompare) = 1 Then
SegmentMe = Replace(SegmentMe, cel.Value, cel.Offset(0, 1).Value)
start = Len(cel.Offset(0, 1))
GoTo Attributes
End If
Next

Attributes:

For Each cel In AttributesFootcare
On Error Resume Next
If InStr(start, SegmentMe, cel.Text, vbTextCompare) > 0 Then
SegmentMe = Application.WorksheetFunction.Trim(Replace(SegmentMe, _
cel.Value, cel.Offset(0, 1).Text))
End If
Next

End Function


and call like this

=SegmentFootcare(A1,SubbrandsFootcare,BrandsFootcare,AttributesFootcare)

jmenche
08-17-2006, 09:20 AM
Thanks xld. I'll try it that way.

Bob Phillips
08-17-2006, 10:22 AM
BTW, you don't need worksheetfunction Trim, VBA has a Trim function.

jmenche
08-17-2006, 11:35 AM
The VBA Trim function only gets rid of leading and trailing spaces. The worksheet function gets rid of extra spaces in between too. Is there a VBA function for that?

Bob Phillips
08-17-2006, 11:43 AM
Ah, that is differentr then. There are ways, but they are so convoluted, the function route is probably the best way.

Zack Barresse
08-17-2006, 01:11 PM
Convoluted?

cel.Replace " ", ""

???

mdmackillop
08-17-2006, 01:27 PM
A word of caution.
Trim and similar functions will not clear Char(160) blanks which I've come across in data exported from other applications into csv files and the like.

Bob Phillips
08-17-2006, 02:02 PM
Convoluted?

cel.Replace " ", ""
???
Yes, to do it properly, it is



Dim s
s = "Bob Phillips"

MsgBox Replace(s, " ", "")

MsgBox Application.Trim(s)

Zack Barresse
08-17-2006, 02:10 PM
Any reason you'd use that over..

Dim s
s = " a b c "

s = Replace(s, " ", "")

MsgBox s & Len(s)

Bob Phillips
08-17-2006, 03:51 PM
I'm not getting the point you are making. I demonstrated that replacing space is no good, it gives the wrong result.

jmenche
08-18-2006, 05:51 AM
Firefytr,

Xld's method would return BobPhillips (which is not the desired result).

Thanks everyone for the discussion!

Zack Barresse
08-18-2006, 07:31 AM
I'm sorry Bob, how is that the wrong results? I get all spaces replaced. Am I missing something here?

Edit: Nevermind, saw the last post. Misunderstood the OP's wishes. :banghead:

Bob Phillips
08-18-2006, 08:10 AM
Firefytr,

Xld's method would return BobPhillips (which is not the desired result).

Thanks everyone for the discussion!

That was not xld's solution!

Bob Phillips
08-18-2006, 08:10 AM
I'm sorry Bob, how is that the wrong results? I get all spaces replaced. Am I missing something here?

Edit: Nevermind, saw the last post. Misunderstood the OP's wishes. :banghead:

So you can see why I said it was convolutred toi get the Op's desired result :)

Zack Barresse
08-18-2006, 08:28 AM
Yes, I was misunderstanding. Need to read a little better next time! :yes