+ Reply to Thread
Results 1 to 17 of 17

Search Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Exclamation Search Macro

    This is my first time writing a macro, although I took a Visual Basic class before. Any help would be greatly appreciated.

    I have several vehicles each in their own tab. In each tab, there are assessments listed, and then equipment in the vehicle at time of assessment. What I need to do is create a way to click any equipment's part number, and search through the entire file to make a list of all vehicles that contained the same equipment, and at which assessment.

    Is this something that a macro can help with?

    Thanks ahead of time

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search Macro

    Hello Xorin,

    Welcome to the Forum!

    In order to help you with this project, you should post your workbook. This will help answers a lot of questions and let us see how the data is laid out before writing any code.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Search Macro

    Attached is a very simplified layout of how the data is organized. Anything else I can do to help clarify?
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Search Macro

    Also, there are going to be many many part numbers, so for now I think place the results in a .txt file?

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search Macro

    Hello Xorin,

    Unless in the actual workbook the assessment data is unique, shouldn't you also include the block number with the data.

  6. #6
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Search Macro

    Assessment data will be unique, but if the solution includes the block as well, I certainly won't be upset =)

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Search Macro

    Why not merge all of the data on all of the tabs into a single table, with columns for vehicle number, block, date, title, .... That's how Excel was designed to work.
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Search Macro

    Because I simplified the example... there are too many vehicles, blocks, assessments, and way too many parts to put it in one. This isn't a database, it's supposed to be a nicely organized interface.

    So is there anyone who'd like to try and show me a little something about VBA code for searching like this?
    I just need a push in the right direction and hopefully my VB will kick in.
    Last edited by Xorin; 07-15-2010 at 08:20 AM.

  9. #9
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Search Macro

    Just to keep things interesting, there's been a little change. There's going to be a cover tab that will have a igoogle-like layout with a search tool, and the results will be shown in that tab under the search. Thanks guys for your patience

  10. #10
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Search Macro

    Alright well here's what I have so far I guess:

    Public Sub Search()
    
    Range("C13:P45") = ""                                                           'Clear Old Search
    DatatoFind = Range("D5")                                                        'Assign Search Term
    If DatatoFind = "" Then Exit Sub                                                'Cancel Blank Searches
    
    x = 14                                                                          'Start on row 14
    For Each wkst In Worksheets                                                     'Loop Through Sheets
        If wkst.Name <> "Search" And _
        wkst.Name <> "Compliances" And _
        wkst.Name <> "Acronyms" Then                                                'Skip These Sheets
            Range("C" & x) = wkst.Name                                              'Display Platform
                    
            With Worksheets(wkst.Name).Range("D:D")                                 'Set Range
                Set c = .Find(DatatoFind)                                           'Find Data
                    If Not c Is Nothing Then                                        'If Found
                        firstAddress = c.Address                                    'Get first terms address
                        Do
                            Worksheets("Search").Range("E" & x) = c                 'Display in search
                            Set c = .FindNext(c)                                    'Find Next Data
                            x = x + 1                                               'Next row
                        Loop While Not c Is Nothing And c.Address <> firstAddress   'Loop until entire sheet is complete
                    End If
            End With
            x = x + 1                                                               'Next row
    
        End If
    Next wkst
    
    End Sub
    All thats displayed are the tab names right now. "If not c is nothing" never passes. I'm not sure if I'm searching right... can someone help?

  11. #11
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Search Macro

    I've think I fluked and got it to work, but now its not. anyways, also if someone know how to copy over without formatting that'd be good too. =) thanks

    PS if it's possible to ignore dashes in search thatd be even better
    Last edited by Xorin; 07-16-2010 at 09:16 AM.

  12. #12
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Search Macro

    So my search was working it was just the dashes messing it up. I have more problems though:

    Public Sub Search()
    
    Range("C13:P45") = ""                                                           'Clear Old Search
    DatatoFind = Range("D5")                                                        'Assign Search Term
    If DatatoFind = "" Then Exit Sub                                                'Cancel Blank Searches
    
    x = 14                                                                          'Start on row 14
    For Each wkst In Worksheets                                                     'Loop Through Sheets
        If wkst.Name <> "Search" And _
        wkst.Name <> "Compliances" And _
        wkst.Name <> "Acronyms" Then                                                'Skip These Sheets
       
        With Worksheets(wkst.Name).Range("D:D")                                 'Set Range
            Set c = .Find(DatatoFind)                                           'Find Data
                If Not c Is Nothing Then                                        'If Found
                    firstAddress = c.Address                                    'Get first terms address
                    Do
                        Worksheets("Search").Range("C" & x) = wkst.Name         'Display Platform
                        Worksheets("Search").Range("E" & x) = c.Offset(0, -3)   'Display Block
                        Worksheets("Search").Range("G" & x) = c.Offset(0, -2)   'Display Assessment
                        Worksheets("Search").Range("J" & x) = c.Offset(0, -1)   'Display Name
                        Worksheets("Search").Range("M" & x) = c                 'Display P/N
                        Set c = .FindNext(c)                                    'Find Next Data
                        x = x + 1                                               'Next row
                    Loop While Not c Is Nothing And c.Address <> firstAddress   'Loop until entire sheet is complete
                End If
        End With
        x = x + 1                                                               'Next row
        
        End If
    Next wkst
    
    End Sub
    Now the block and assessment date are in merged cells, and offsetting is not getting the values of the cells unless they are in the first row because that's where the merged cell value is... is there a way to adjust for this?

    I also only want the first line for these merged cells since i used a line break alt + enter

    So things I need help with:
    • Ignore the formatting when displaying to search
      Ignore dashes while searching
      Getting Merged Cell Values
      Getting only the first line in some cases

  13. #13
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Unhappy Re: Search Macro

    Well I guess I'll just keep documenting my work... lol

    Public Sub Search()
    
    Range("C15:P50") = ""                                                                   'Clear Old Search
    DatatoFind = Range("D5")                                                                'Assign Search Term
    If DatatoFind = "" Then Exit Sub                                                        'Cancel Blank Searches
    Range("D11") = "Last Search: " & DatatoFind                                             'Show Search Term
    
    x = 15                                                                                  'Start on row 15
    For Each wkst In Worksheets                                                             'Loop Through Sheets
        If wkst.Name <> "Search" And _
        wkst.Name <> "Compliances" And _
        wkst.Name <> "Acronyms" Then                                                        'Skip These Sheets
       
        With Worksheets(wkst.Name).Range("D:D")                                             'Set Range
            Set c = .Find(DatatoFind)                                                       'Find Data
            If Not c Is Nothing Then                                                        'If Found
                firstAddress = c.Address                                                    'Get first terms address
                Do
                    With Worksheets("Search")
                        .Range("C" & x) = Worksheets(wkst.Name).Range("A1")                 'Display Platform
                        .Range("E" & x) = c.Offset(0, -3).MergeArea(1, 1)                   'Display Block
                        .Range("E" & x) = Replace(.Range("E" & x), Chr(10), "   ")
                        .Range("G" & x) = c.Offset(0, -2).MergeArea(1, 1)                   'Display Date
                        .Range("G" & x) = Replace(.Range("G" & x), Chr(10), "   ")
                        .Range("H" & x) = c.Offset(0, -1)                                   'Display Name
                        .Range("H" & x) = Replace(.Range("H" & x), Chr(10), "   ")
                        .Range("O" & x) = c                                                 'Display P/N
                        .Range("O" & x) = Replace(.Range("O" & x), Chr(10), "   ")
                        .Range(x & ":" & x).WrapText = False                                'No word wrap
                    End With
                    Set c = .FindNext(c)                                                    'Find next data
                    x = x + 1                                                               'Next row
                    If x = 50 Then
                        Range("H50") = "Too many results, please be more specific."         'Too many results
                        Exit Sub
                    End If
                                            
                Loop While Not c Is Nothing And c.Address <> firstAddress                   'Loop through entire sheet
            End If
        End With
       
        End If
    Next wkst
    
    If Range("C15") = "" Then Range("H15") = "No Search Results Found."                     'No results
    
    End Sub
    So I still haven't been able to ignore the dashes since wildcards dont work as far as my google searches have told me.
    I've subsituted line breaks for spaces, but I can't only get the first line (without wildcards again) so I've instead adjusted the column size since most of the dates are around the same pixel width.
    Fixed formating with a word wrap property
    Found how to get merged cell data

    ...I don't know if anyone will see this though =(

  14. #14
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Search Macro

    I don't want to be rude, but can someone please help me out?

  15. #15
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Search Macro

    Soooo, I'm guessing that's a no?

  16. #16
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Search Macro

    Xorin, I told you your data was laid out poorly (spread out over multiple sheets, merged cells, ...) and should be arranged in database fashion; you said it's not a database, it's an interface, it has to be that way, and that's not what your real data is like anyway.

    I'm wondering why you're wondering why people aren't jumping in to help.

  17. #17
    Registered User
    Join Date
    07-12-2010
    Location
    Internet
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Search Macro

    Yes, that's a good summary of the thread.

    I thought it might be understood that most of the time data is not submitted in the most efficient way, and that this is how it comes to me. Also that there are not 3 tabs but 300 tabs, and that the end user wants to be able to search for and then go to the desired tab to see only applicable data, not a database of everything that ever came through. I thought that people may understand that the data is sensitive and cannot just be shown publicly online.

    I do not think these assumptions I made are ridiculous. I think they are rather common when it comes to excel use. I also would think that help would be more friendly and less snippy.

    I'm sorry if I was wrong in my thinking. I guess I'll try somewhere else for help. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1