Results 1 to 2 of 2

Thread: Searching Excel sheet from VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Location
    UK
    Posts
    3

    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:
    1. Dim appExcel As Object
    2. Set appExcel = CreateObject("Excel.Application")
    3.  
    4. appExcel.DisplayAlerts = False
    5. appExcel.Visible = False
    6. appExcel.ScreenUpdating = True
    7. appExcel.Workbooks.Open FileName:="C:\names.xls"
    8. appExcel.Worksheets("sheet1").Select
    9.  
    10. Private Sub Command1_Click()
    11.  
    12. If Form1.NamesCombo.ListIndex < 0 Then
    13. MsgBox "You must select a student!", vbExclamation, "Error"
    14. Exit Sub
    15. End If
    16.  
    17. On Error GoTo errhandle:
    18.  
    19. Dim appExcel As Object
    20. Set appExcel = CreateObject("Excel.Application")
    21.  
    22. Private Sub Command1_Click()
    23. appExcel.DisplayAlerts = False
    24. appExcel.Visible = False
    25. appExcel.ScreenUpdating = True
    26. appExcel.Workbooks.Open FileName:="C:\names.xls"
    27. appExcel.Worksheets("sheet1").Select
    28.  
    29. appExcel.Save
    30. appExcel.Quit
    31.  
    32. MsgBox "The records have been updated"
    33.  
    34. Exit Sub
    35. errhandle:
    36. Set appExcel = Nothing
    37. MsgBox "An error has occured: " & Err.Description
    38. appExcel.Quit
    39.  
    40. 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.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. With Worksheets(1).Range("A1:A500")
    2.         Set c = .Find(UserForm1.txtSearch.Text, LookIn:=xlValues)
    3.         If Not c Is Nothing Then
    4.             MsgBox c.Value
    5.         End If
    6.     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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width