Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net
Go Back  Xtreme Visual Basic Talk > > > Use of the VBA Find Statement in VB.net


Reply
 
Thread Tools Display Modes
  #1  
Old 02-23-2004, 10:00 PM
yui79 yui79 is offline
Newcomer
 
Join Date: Feb 2004
Location: Adelaide, Australia
Posts: 3
Default Use of the VBA Find Statement in VB.net


Hi All,

I am writing a program using VB.net to interogate a data set within an excel spreadsheet. In short, after recieving a string of text from a barcode reader, the program searches down a column to find a match. If one exists then it reads the cell next door and depending on its content (ie, "Keep", "Cull" or otherwise) performs a specific action. As I am a complete novice , I initially used the For...Next statments to search throught the excel dataset until a match was found. However the dataset can be up to 30,000 rows long.... and therefore made the code run Vslow!!!! So now I am trying to use the Find command from VBA (Excel 2000) to make it quicker. But I am stuck...... please help (I got the skeleton of the code to work in VBA, but not .net).

I have posted the For... next code to show what I was trying to do, and then my atempt at using the Find comand from VBA...... where am I going wrong? VB.net is telling me that my syntax is wrong in the Range(cells etc) statement, the LookIn part of the .Find statement, and the c.Address statement.

For...Next code ...... VSLOW!!!

Code:
For x = 1 To NoSample If x < NoSample Then MyXLApp.Sheets("Sheet1").Select() If ID = MyXLApp.Cells(x, 1).Value Then If MyXLApp.Cells(x, 2).Value = "Keep" Then 'Do Stuff,manipulate data, play sound etc ElseIf MyXLApp.Cells(x, 2).Value = "Cull" Then 'Do Other Stuff,manipulate data, play sound etc Else 'Do Other Stuff,manipulate data, play sound etc End If Exit For End If Else Dim sound As New ClsSound sound.PlayWav("C:\Program Files\KeepCull\Error.wav") End If Next x

FIND code..... it don't wanna work.


Code:
Dim NoSample As Integer Dim Barcode As String Dim firstAddress As MyXLApp.Range Dim Action1 as String Barcode = "Cat" 'Barcode usually user-defined via Form NoSample = 10 'NoSample usually user-defined via Form With MyXLApp.Range(Cells(1, 1), Cells(NoSample, 1)) Set c = .Find(Barcode, LookIn:=xlValues) If Not c Is Nothing Then firstAddress = c.Address MyXLApp.Range(firstAddress).Select() MyXLApp.ActiveCell.Offset(0, 1).Select() Action1 = MyXLApp.ActiveCell.Value If Action1 = "Cull" Then 'Do stuff, manipulate data, play sound etc ElseIf Action1 = "Keep" Then 'Do other stuff Else 'Do other stuff EndIf Else End If End With

I hope this is all clear.... first time at using the forum.

Thanks in advance kind peoples

yui79
Reply With Quote
  #2  
Old 02-24-2004, 01:07 AM
Timbo's Avatar
TimboUse of the VBA Find Statement in VB.net Timbo is offline
Green-Eyed

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Bangkok, Thailand
Posts: 10,261
Default

I'm no .Net expert, but from gossip I've overheard, 'Set x = Object' statements are replaced by 'x = Object' in .Net.

Check out the 'VB.Net Office Integration FAQ' at the top of this board...
__________________
"He's not the Messiah. He's a very naughty boy!" - Brian's mum

Can't find the answer? >> Try something new!
Become a Professional
Reply With Quote
  #3  
Old 02-24-2004, 01:18 AM
yui79 yui79 is offline
Newcomer
 
Join Date: Feb 2004
Location: Adelaide, Australia
Posts: 3
Default

Hi Timbo,

Thanks for the reply.... I should have removed the Set from the 'Set x=obj' code, thankfully, .Net actually does this for you if you enter the code (as I did) directly from VBA.

Sadly, this does not fix the problems (ie the LookIn part).....

Cheers anyway

yui79
Reply With Quote
  #4  
Old 02-24-2004, 09:11 AM
Wamphyri's Avatar
WamphyriUse of the VBA Find Statement in VB.net Wamphyri is offline
Variable not defined

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Ottawa, Ontario
Posts: 4,793
Default

You need to declare c as a range object and firstAddress should be a string object rather than a range object
__________________
-Carl
Reply With Quote
  #5  
Old 02-24-2004, 11:25 PM
yui79 yui79 is offline
Newcomer
 
Join Date: Feb 2004
Location: Adelaide, Australia
Posts: 3
Default

Well, declaring c as Range helped.... thanks,

Any ideas where I have gone wrong in the syntax for the following bit?

Code:
With MyXLApp.Range(Cells(1, 1), Cells(NoSample, 1))
Reply With Quote
  #6  
Old 02-25-2004, 04:37 AM
herilane's Avatar
herilaneUse of the VBA Find Statement in VB.net herilane is offline
Unashamed geek

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

Cells is an Excel property and therefore needs to be fully referenced. Try this:
Code:
With MyXLApp.Range(MyXLApp.Cells(1, 1), MyXLApp.Cells(NoSample, 1))
Reply With Quote
  #7  
Old 02-25-2004, 08:00 AM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

In addition to making these fixes to get your code actually running properly, the last issue you'll have to consider is having your App close and to be able to close Excel. That is, I believe if you look in your Task Manager, you are almost certainly spawning hanging copies of Excel, running in the background.

This is a lot to throw at a beginner, but you should have a look at the VB.Net Office Integration Tutorial which emphasizes that one must be extremely explicit about the creation and disposal of your COM objects. The first thing that you will have to abandon is the use of the With .. End With construct.

Sorry.

Automation in .Net is quite tricky.

-- Mike
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb

Last edited by Mike Rosenblum; 02-25-2004 at 07:17 PM.
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
I am a total beginner: where to start VBA/ VB.NET... edward1975 Tech Discussions 3 01-18-2004 02:12 PM
Newbie vb.net Ques: How do you find difference in time? andru General 1 11-01-2003 03:36 AM
VBA Question (conditional statement) nickg96 Database and Reporting 5 10-22-2002 03:04 PM
Where can I find the timer control in VBA. chepe40 Word, PowerPoint, Outlook, and Other Office Products 1 02-11-2002 02:26 PM

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
Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net
 
Use of the VBA Find Statement in VB.net
Use of the VBA Find Statement in VB.net
 
-->