Results 1 to 5 of 5

Thread: Coded Search in Excel 2002 not working

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Coded Search in Excel 2002 not working

    i have the following code in a module:

    Code:
    Dim StartDate as Date
    
    ActiveSheet.Range("A1").Select
    
    Activesheet.Cells.Find(What:=StartDate, After:=ActiveCell, LookIn:=xlValues, LookAt _
            :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
    StartDate = 17/01/05 (variable taken from another sheet)

    when i manually search the sheet, it finds the above entry, but when i use the code, it fails with 'Object variable or With block variable not set'

    i have tried formating the date as dd/mm/yyyy, but still not working. I have a feeling it is to do with formats, but not sure, as it will find a string, just not this date.

    I have also tried recording the same search via excel, but it still wont work.

    i found this on MSKB: Run Time Error 91

    could it be something to do with this?

    any ideas?

    TIA

    Brian
    if you fail to plan, you plan to fail

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

    Re: Coded Search in Excel 2002 not working

    It could be that .Find is not evaluating the variable contents, but instead
    searching for the literal text? Test it out by hardcoding the date in the
    What:= parameter.

    HTH
    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Coded Search in Excel 2002 not working

    Quote Originally Posted by RobDog888
    It could be that .Find is not evaluating the variable contents, but instead
    searching for the literal text? Test it out by hardcoding the date in the
    What:= parameter.

    HTH
    yep,

    tried this as well:

    Code:
    Activesheet.Cells.Find(What:="17/01/05", After:=ActiveCell, LookIn:=xlValues, LookAt _
            :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
    still doesnt work, and same error..
    if you fail to plan, you plan to fail

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

    Re: Coded Search in Excel 2002 not working

    If the column is formatted as a Date then try passing a search criteria with
    the '#' character surrounding the date value.
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Coded Search in Excel 2002 not working

    Quote Originally Posted by RobDog888
    If the column is formatted as a Date then try passing a search criteria with
    the '#' character surrounding the date value.
    I had thought of this as well.

    heres a couple of things i have tried:

    Code:
    Cells.Find(What:="#" & StartDate & "#", After:=ActiveCell, LookIn:=xlValues, LookAt _
            :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
    doesnt work

    Code:
    Cells.Find(What:=#StartDate#, After:=ActiveCell, LookIn:=xlValues, LookAt _
            :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
    It doesnt like this code

    or this:
    Code:
    Cells.Find(What:='#StartDate#', After:=ActiveCell, LookIn:=xlValues, LookAt _
            :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
    also:
    Code:
    StartDate = "#" & StartDate & "#"
    prior to the search (had to declare the variable as a variant)

    I cant think how else to do this!

    I have also tried declaring the StartDate variable as a variant, and string and it still doesnt work. variant gives same error, and string doesnt work with other segments of my code so doesnt get this far.

    thanks
    if you fail to plan, you plan to fail

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