yui79
02-23-2004, 09:00 PM
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 :huh: , 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!!!
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.
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 :rolleyes:
yui79
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 :huh: , 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!!!
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.
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 :rolleyes:
yui79