Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > Worksheet Function error when running


Reply
 
Thread Tools Display Modes
  #1  
Old 10-10-2003, 11:41 AM
ehllhjh123 ehllhjh123 is offline
Newcomer
 
Join Date: Aug 2003
Posts: 7
Default Worksheet Function error when running


I have taken some sample code from another thread to integrate in my program.

The code compiles fine, but when it is ran I get the error:

Unable to get the Match property of the WorksheetFunction class.

I get the same error when trying to implement the Vlookup worksheet function from within VB.

The code:
<vb/>
Public Sub getdesc()
Dim s As String, result As String

s = Range("C1").Value

result = WorksheetFunction.Index(Columns(2), WorksheetFunction.match(s, Columns(1), 0))

Range("D1").Value = result

End Sub
</vb>

Any help would be greatly appreciated! You guys are incredible at debugging these issues!

Thanks!
Reply With Quote
  #2  
Old 10-10-2003, 12:48 PM
mathiasb's Avatar
mathiasb mathiasb is offline
Centurion
 
Join Date: Jul 2003
Location: San Francisco, CA
Posts: 183
Default

In the result expression, you use columns(1) or columns(2) which are not defined. I guess you want to use the worksheet function columns(), but then you would have to specify that this is a worksheet function, otherwise it won't be recognized as such in your code.

Hope that helps,


Quote:
Originally Posted by ehllhjh123
I have taken some sample code from another thread to integrate in my program.

The code compiles fine, but when it is ran I get the error:

Unable to get the Match property of the WorksheetFunction class.

I get the same error when trying to implement the Vlookup worksheet function from within VB.

The code:
<vb/>
Public Sub getdesc()
Dim s As String, result As String

s = Range("C1").Value

result = WorksheetFunction.Index(Columns(2), WorksheetFunction.match(s, Columns(1), 0))

Range("D1").Value = result

End Sub
</vb>

Any help would be greatly appreciated! You guys are incredible at debugging these issues!

Thanks!

__________________
Mathias Brandewinder,
Applied Strategies Technology
Reply With Quote
  #3  
Old 10-10-2003, 01:28 PM
ehllhjh123 ehllhjh123 is offline
Newcomer
 
Join Date: Aug 2003
Posts: 7
Default continuing issues

For some reason I feel like this error is being generated because my excel application isn't configured correctly (ie. missing a plugin). Does the error look familiar to something like that?

I haven't been successful running any code w/ "worksheetfunction" objects.
Reply With Quote
  #4  
Old 10-10-2003, 01:46 PM
herilane's Avatar
herilane herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default

As Mathias points out, you're just not referring to the ranges correctly.
Try this:
Code:
result = WorksheetFunction.Index(Range("B:B"), WorksheetFunction.match(s, Range("A:A"), 0))
Reply With Quote
  #5  
Old 10-10-2003, 02:35 PM
ehllhjh123 ehllhjh123 is offline
Newcomer
 
Join Date: Aug 2003
Posts: 7
Default continuing issues

hmmm... unfortunately I get the same error even w/ your editions herilane... it's very possible that I'm making a dumb mistake, but for some reason I still feel like the error refers to some missing link for Worksheetfunctions w/in VB.

THanks for your help!
Reply With Quote
  #6  
Old 10-12-2003, 05:56 AM
herilane's Avatar
herilane herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default

OK, let's take a step back... are you doing this in VB6 or VBA?
Reply With Quote
  #7  
Old 10-13-2003, 09:18 PM
tboltfrank tboltfrank is offline
Senior Contributor
 
Join Date: Jul 2003
Posts: 1,022
Default

*** See edit at end, as I finaly realized that the variables are declared
as strings, so indications are from that, that the data is strings. * And naturaly numbers would not work with the code that way ***

ehllhjh123,

Are you searching for strings or numbers?

I experimented with this code and when I used numbers in columns(A) & (B) and cell C1, I got the same error that you're describing. But when I used string data, it worked.

Another possibility might be if your Data is dates. If it is, I think that you may have to convert them to strings, before using the function.

I'm very weak on my knowledge of formulas, (Herilane is far my superior in that and all coding) but if you are using dates with this and he doesn't happen back before you need to try something new, I'd be happy to share what I successfully used as a work around, if no better solutions are offered.

*Edit* Based on the fact that you have your variables declared as strings, I guess I should have made the assumtion that you are using string data. - If you are using numbers though, just change your variable declarations to Integer, or Variant, instead of String, like you have it. And if they are dates, change the date to it's serial number and change the format to a number with zero decimal places, run your routine, then change the format back to a date. If that does the trick, that can all be added to your code.

Last edited by tboltfrank; 10-14-2003 at 12:33 AM.
Reply With Quote
  #8  
Old 10-14-2003, 11:02 AM
Insomniac Insomniac is offline
Contributor

* Expert *
 
Join Date: Mar 2003
Location: Adelaide,Australia
Posts: 681
Default

I think tboltfrank may have a point here with the data type!

I got both these ways to work on sample data:
Code:
[D1] = WorksheetFunction.Index(Columns(2), WorksheetFunction.Match([C1], Columns(1), 0)) [D1] = WorksheetFunction.Index([B:B], WorksheetFunction.Match([C1], [A:A], 0))

If its a data type issue you can just pass the range itself to the function and let Excel worry about the data type:
Code:
Public Sub getdesc() Dim s As Range, result As Variant Set s = [C1] On Error Resume Next result = WorksheetFunction.Index([B:B], WorksheetFunction.Match(s, [A:A], 0)) If result = Empty Then MsgBox "no match" [D1] = result Set s = Nothing End Sub
Reply With Quote
  #9  
Old 10-14-2003, 02:31 PM
tboltfrank tboltfrank is offline
Senior Contributor
 
Join Date: Jul 2003
Posts: 1,022
Default

As you already know, that works great Insomniac.

This has been an issue that has caused me to write extra code, to work around it, a few times.

As I'm sure you know, using DateValue can be implemented for some similar situations, too. - I learned that from one of Herilanes other post today. - Just out of curiosity, do you see that as something that could be used here? - You would likely know the answer to that in an instant, whereas it would take me awhile to play around with it, to decide. If it didn't work, it might take quite awhile, before I could conclude that it couldn't be done.

Thanks
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Registry: queue list of keys? robplatt File I/O and Registry 3 07-02-2003 01:00 PM
need help! i'm a beginner geekool General 7 04-26-2003 12:44 AM
Shareware Registry Protection Technigue karachi999 General 2 01-21-2002 02:40 PM
MS Winsocket gibson General 3 07-20-2001 10:03 AM
link function to textboxes golash General 2 05-03-2001 11:57 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->