+ Reply to Thread
Results 1 to 5 of 5

Macro ignores search code when 1 of the 2 search parameters is not found

  1. #1
    Registered User
    Join Date
    03-15-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    14

    Macro ignores search code when 1 of the 2 search parameters is not found

    I am developing a macro that allows a user to enter area codes into Column A of a "Search" and state abbreviations into column "B". The macro then searches a table of data (Results), looking for either criteria and when it finds a match, it copies the row and pastes it to a different tab (AcctDetailGeo)

    I want people to be able to search and return results if they search for area codes or states (or both). Currently, my code will only return results if Column A has a value or if Columns A and B have values. If Column A is empty but Column B has values, it does not work. Can someone take a peak at this and tell me what I need to change in order for the macro to look at Column B even if Column A has no value

    Thanks in advance for the help


    CODE


    Dim rcnt1 As Long, rcnt2 As Long, cnt As Long

    rcnt1 = Sheets("Results").Range("A" & Rows.Count).End(xlUp).Row
    rcnt2 = Sheets("Search").Range("A" & Rows.Count).End(xlUp).Row
    cnt = Sheets("AcctDetailGeo").Range("A" & Rows.Count).End(xlUp).Row

    For j = 2 To rcnt2
    For i = 2 To rcnt1
    If (Sheets("Search").Range("A" & j).Value = Sheets("Results").Range("L" & i).Value) Or (Sheets("Search").Range("B" & j).Value = Sheets("Results").Range("M" & i).Value) Then
    cnt = cnt + 1
    Sheets("Results").Rows(i & ":" & i).Copy (Sheets("AcctDetailGeo").Range("A" & cnt))
    End If
    Next
    Next
    Last edited by BrodyNoochie; 04-25-2012 at 03:55 PM.

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Macro ignores search code when 1 of the 2 search parameters is not found

    try
    Please Login or Register  to view this content.
    This will allow column A to be empty or B to be empty and will use the last row of either A or B.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Registered User
    Join Date
    03-15-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Macro ignores search code when 1 of the 2 search parameters is not found

    I may be missing something but when I plug it in I am getting a compile error: Sub or Function not defined.

    Any ideas?

  4. #4
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Macro ignores search code when 1 of the 2 search parameters is not found

    Oops. I forgot that VBA doesn't have a Max function. try this instead
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-15-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Macro ignores search code when 1 of the 2 search parameters is not found

    thanks blane. that worked well.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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