|
-
Dec 28th, 2005, 06:58 AM
#1
Thread Starter
New Member
Searching Excel sheet from VB
Ive written some basic code from reading the values in certain cells in vb and putting them into a combo box. Now ive got a text box for membership numbers, (this is just an exercise im not from a company or anything ) How can i get VB to search the excel sheet for that number?
If the number is found then open formx else "Sorry you have enter the number incorrectly"
Cheers
VB Code:
Dim appExcel As Object
Set appExcel = CreateObject("Excel.Application")
appExcel.DisplayAlerts = False
appExcel.Visible = False
appExcel.ScreenUpdating = True
appExcel.Workbooks.Open FileName:="C:\names.xls"
appExcel.Worksheets("sheet1").Select
Private Sub Command1_Click()
If Form1.NamesCombo.ListIndex < 0 Then
MsgBox "You must select a student!", vbExclamation, "Error"
Exit Sub
End If
On Error GoTo errhandle:
Dim appExcel As Object
Set appExcel = CreateObject("Excel.Application")
Private Sub Command1_Click()
appExcel.DisplayAlerts = False
appExcel.Visible = False
appExcel.ScreenUpdating = True
appExcel.Workbooks.Open FileName:="C:\names.xls"
appExcel.Worksheets("sheet1").Select
appExcel.Save
appExcel.Quit
MsgBox "The records have been updated"
Exit Sub
errhandle:
Set appExcel = Nothing
MsgBox "An error has occured: " & Err.Description
appExcel.Quit
End Sub
This is the code i am using for opening and closing VB once data has been entered. Now what i want to do, once a value is typed into a text box i want to be able to search the excel sheet for that exact value, if it is not found then "error message" else load formX
Regards
Last edited by RobDog888; Dec 29th, 2005 at 12:47 AM.
Imposible is a word only to be found in the dictionary of fools.
-
Dec 29th, 2005, 12:57 AM
#2
Re: Searching Excel sheet from VB
You have two Command1_Click events and only one End Sub. also, code outside any procedures.
You need to use the .Find method off of the Range object.
VB Code:
With Worksheets(1).Range("A1:A500")
Set c = .Find(UserForm1.txtSearch.Text, LookIn:=xlValues)
If Not c Is Nothing Then
MsgBox c.Value
End If
End With
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|