+ Reply to Thread
Results 1 to 24 of 24

How to search in the current workbook

  1. #1
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    How to search in the current workbook

    Hi all,

    I am looking for a way to have a macro look in the current workbook (not a specific one, but the one that is open at the moment) to look for values that aren't in another workbook. I have the following code which works for a specific workbook, but instead of "List Copy" I want the macro to compare whatever file is open now. It will most likely be the List Copy with a different date.

    Sub colorlist()
    Dim SearchRange As Range
    Dim NUmberRange As Range
    Dim Searchvalue As String
    Dim xCell As Variant
    Dim c As Range

    'Where is the search list?
    Set SearchRange = Workbooks("Bill of mat.xlsm").Worksheets("Database").Range("A2:A5000")
    'Where is list of numbers?
    Set NUmberRange = Workbooks("List Copy.xlsm").Worksheets("Sheet1").Range("E2:E100000")

    For Each c In NUmberRange
    Searchvalue = c.Value

    On Error Resume Next
    Set xCell = SearchRange.Find(what:=Searchvalue, matchbyte:=False)
    On Error GoTo 0
    If xCell Is Nothing Then
    c.Interior.ColorIndex = 3
    Else
    c.Interior.ColorIndex = 0
    End If
    Next c

    ActiveSheet.Range("$A$1:$Z$65745").AutoFilter Field:=5, Criteria1:=RGB(255 _
    , 0, 0), Operator:=xlFilterCellColor
    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to search in the current workbook

    This will reference the Active workbook at the time the macro is run.
    Set NUmberRange = ActiveWorkbook.Worksheets("Sheet1").Range("E2:E100000")

    This will reference the last workbook opened.
    Set NUmberRange = Workbooks(Workbooks.Count).Worksheets("Sheet1").Range("E2:E100000")
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    Re: How to search in the current workbook

    Quote Originally Posted by AlphaFrog View Post
    This will reference the Active workbook at the time the macro is run.
    Set NUmberRange = ActiveWorkbook.Worksheets("Sheet1").Range("E2:E100000")

    This will reference the last workbook opened.
    Set NUmberRange = Workbooks(Workbooks.Count).Worksheets("Sheet1").Range("E2:E100000")
    Thanks, can you elaborate on how to use the second one? I put it in but it gives me a rune time error "9".

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to search in the current workbook

    Quote Originally Posted by thelegazy View Post
    Thanks, can you elaborate on how to use the second one? I put it in but it gives me a rune time error "9".
    Replace this...
    Set NUmberRange = Workbooks("List Copy.xlsm").Worksheets("Sheet1").Range("E2:E100000")
    With This...
    Set NUmberRange = Workbooks(Workbooks.Count).Worksheets("Sheet1").Range("E2:E100000")

    Make sure the last opened workbook has a worksheet named "Sheet1". Or use this to reference its 1st sheet regardless of the sheet name.
    Set NUmberRange = Workbooks(Workbooks.Count).Worksheets(1).Range("E2:E100000")

  5. #5
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    Re: How to search in the current workbook

    Quote Originally Posted by AlphaFrog View Post
    Replace this...
    Set NUmberRange = Workbooks("List Copy.xlsm").Worksheets("Sheet1").Range("E2:E100000")
    With This...
    Set NUmberRange = Workbooks(Workbooks.Count).Worksheets("Sheet1").Range("E2:E100000")

    Make sure the last opened workbook has a worksheet named "Sheet1". Or use this to reference its 1st sheet regardless of the sheet name.
    Set NUmberRange = Workbooks(Workbooks.Count).Worksheets(1).Range("E2:E100000")
    Ah okay I got it. But I want to change the initial code. In my original code "List copy" had all the original values and the macro was searching "bill of mat" and showing in "List Copy" what values weren't in "bill of mat". The macro was placed on my computer, but now I would like to place it in the "bill of mat" workbook so anyone who has that file can use it. I tried switching around the workbook names in the code, but it isn't working. Can you help me solve this?

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to search in the current workbook

    Quote Originally Posted by thelegazy View Post
    Ah okay I got it. But I want to change the initial code. In my original code "List copy" had all the original values and the macro was searching "bill of mat" and showing in "List Copy" what values weren't in "bill of mat". The macro was placed on my computer, but now I would like to place it in the "bill of mat" workbook so anyone who has that file can use it. I tried switching around the workbook names in the code, but it isn't working. Can you help me solve this?

    I don't really follow what that means.

    You may want to use the ThisWorkbook keyword. It refers to the workbook that contains the macro regarless of the workbook's name.

    So this...
    Set SearchRange = ThisWorkbook.Worksheets("Database").Range("A2:A5000")
    ...will reference the workbook that has the macro code.

    Is that what you want?

  7. #7
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    Re: How to search in the current workbook

    Quote Originally Posted by AlphaFrog View Post
    I don't really follow what that means.

    You may want to use the ThisWorkbook keyword. It refers to the workbook that contains the macro regarless of the workbook's name.

    So this...
    Set SearchRange = ThisWorkbook.Worksheets("Database").Range("A2:A5000")
    ...will reference the workbook that has the macro code.

    Is that what you want?
    I'm sorry for the confusion, let me restate in a better manner.

    In my intial post I was running the macro from my computer, it wasn't in any specific workbook. It was checking numbers in "Bill of Mat" and comparing them with a list in "List Copy" and then highlighting any cells that weren't in "Bill of Mat". This worked for me, but it wouldn't work if someone else wanted to do it since it was not in any of the workbooks.

    Now, I would like to alter the code so that the macro can be embedded in the "Bill of Mat" workbook and have it look up the list of numbers and compare it against "List Copy" (which I have decided to use the index code so that it will always be the second one open), and still highlight the ones that are missing from "Bill of Mat" in "List Copy". I want to use the index code, because the "List of Copy" file name will always change as it has a date at the end of it which will change day from day.

    I have bolded the parts I changed. I apologize for any miscommunication.

    Sub colorlist()
    Dim SearchRange As Range
    Dim NUmberRange As Range
    Dim Searchvalue As String
    Dim xCell As Variant
    Dim c As Range

    'Where is the search list?
    Set SearchRange = Workbooks(2).Worksheets("Sheet1").Range("E2:E100000")
    'Where is list of numbers?
    Set NUmberRange = Workbooks("S_BOM.xlsm").Worksheets("Database").Range("A2:A10000")

    For Each c In NUmberRange
    Searchvalue = c.Value

    On Error Resume Next
    Set xCell = SearchRange.Find(what:=Searchvalue, matchbyte:=False)
    On Error GoTo 0
    If xCell Is Nothing Then
    c.Interior.ColorIndex = 3
    Else
    c.Interior.ColorIndex = 0
    End If
    Next c

    ActiveSheet.Range("$A$1:$Z$100000").AutoFilter Field:=5, Criteria1:=RGB(255 _
    , 0, 0), Operator:=xlFilterCellColor


    Range("A1:Z100000").Select
    ActiveSheet.Range("$A$1:$Z$65745").RemoveDuplicates Columns:=5, Header:= _
    xlYes
    End Sub

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to search in the current workbook

    I think I better understand except for one part. It looks like your original code is opposite from the newest code.

    Original code:
    Please Login or Register  to view this content.
    SearchRange is in "Bill of mat"
    NUmberRange is in "List Copy"

    Newest code:
    Please Login or Register  to view this content.
    SearchRange is in 2nd workbook (List Copy ?)
    NUmberRange is in "S_BOM.xlsm" (Bill of mat?)

    Are the workbooks swapped?

  9. #9
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    Re: How to search in the current workbook

    Quote Originally Posted by AlphaFrog View Post
    I think I better understand except for one part. It looks like your original code is opposite from the newest code.

    Original code:
    Please Login or Register  to view this content.
    SearchRange is in "Bill of mat"
    NUmberRange is in "List Copy"

    Newest code:
    Please Login or Register  to view this content.
    SearchRange is in 2nd workbook (List Copy ?)
    NUmberRange is in "S_BOM.xlsm" (Bill of mat?)

    Are the workbooks swapped?
    I swapped them because I thought I would have to because I was now looking up the numbers from "Bill of Mat" (Sbom) not the Copy List

  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to search in the current workbook

    Quote Originally Posted by thelegazy View Post
    I swapped them because I thought I would have to because I was now looking up the numbers from "Bill of Mat" (Sbom) not the Copy List
    It looks like it should work then. What part doesn't work? Does it error? If yes, what's the error and what line is highlighted? Need specifics.

  11. #11
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    Re: How to search in the current workbook

    Quote Originally Posted by AlphaFrog View Post
    It looks like it should work then. What part doesn't work? Does it error? If yes, what's the error and what line is highlighted? Need specifics.
    It doesn't error. I just hit run and it runs for a few seconds and when its done neither of the workbooks change.

  12. #12
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to search in the current workbook

    Did you open "S_BOM.xlsm" first and the other workbook (List Copy?) second?
    Do any cells get colored?

    Can you attach your two workbooks? Paperclip icon on the "Advanced" form editor menu.

  13. #13
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    Re: How to search in the current workbook

    Quote Originally Posted by AlphaFrog View Post
    Did you open "S_BOM.xlsm" first and the other workbook (List Copy?) second?
    Do any cells get colored?

    Can you attach your two workbooks? Paperclip icon on the "Advanced" form editor menu.
    Yes I opened them in that order, nothing gets colored. I'll have to make some dummy files and upload them. I'll do it right now.

  14. #14
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: How to search in the current workbook

    Instead of swapping them you could moved the previous code you had to a personal.xlsb
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  15. #15
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    Re: How to search in the current workbook

    Quote Originally Posted by XeRo Solus View Post
    Instead of swapping them you could moved the previous code you had to a personal.xlsb
    Can you please elaborate, I'm not quire sure I'm understanding what you're saying. Sorry.

  16. #16
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: How to search in the current workbook

    Assuming your original code worked-

    1. Assign the macro a shortcut key
    2. SaveAs to Path C:\Users\'Enter User Name'\AppData\Roaming\Microsoft\Excel\XLSTART

    Open any excel workbook you would like to run the macro on and press your shortcut key. Anyone who saves the file "personal" in this path on their computer can run the macro.

  17. #17
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to search in the current workbook

    Quote Originally Posted by XeRo Solus View Post
    Assuming your original code worked-

    1. Assign the macro a shortcut key
    2. SaveAs to Path C:\Users\'Enter User Name'\AppData\Roaming\Microsoft\Excel\XLSTART

    Open any excel workbook you would like to run the macro on and press your shortcut key. Anyone who saves the file "personal" in this path on their computer can run the macro.
    This does not address why the code doesn't work.

  18. #18
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    Re: How to search in the current workbook

    Quote Originally Posted by AlphaFrog View Post
    This does not address why the code doesn't work.
    Okay, so I have attached the two excel sheets.
    Attached Files Attached Files

  19. #19
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to search in the current workbook

    Your original macro code (regardless of workbook names) doesn't work with the example data you provided. So the example pseudo-data was not helpful. You had stated the original code worked except that you wanted to adjust for workbook names. I thought that's what your question was.

    Just visually looking at the pseudo-data, it appears all parts in BOM match the List of Copy. So are they all suppose to be red? A description there could have helped. If yes, swap your two ColorIndex values.

    Below is just a guess because I don't really understand what you want. The fix is likely quite simple. I'm spending most of my time just trying to divine the question.

    Put this code in the BOM workbook.

    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    Re: How to search in the current workbook

    Quote Originally Posted by AlphaFrog View Post
    Your original macro code (regardless of workbook names) doesn't work with the example data you provided. So the example pseudo-data was not helpful. You had stated the original code worked except that you wanted to adjust for workbook names. I thought that's what your question was.

    Just visually looking at the pseudo-data, it appears all parts in BOM match the List of Copy. So are they all suppose to be red? A description there could have helped. If yes, swap your two ColorIndex values.

    Below is just a guess because I don't really understand what you want. The fix is likely quite simple. I'm spending most of my time just trying to divine the question.

    Put this code in the BOM workbook.

    Please Login or Register  to view this content.
    I'm really sorry for the miscommunication on my part. Let me just state what I would like to have happen. I would like to compare the two lists "bill of mat" and "List of Copy". In bill of mat I have parts 1 & 2, but not 3 & 4 which are in "List of Copy". So, I would like the missing values from "Bill of Mat" to be highlighted in "List of Copy". The part that is messing me up is that I would like to run the macro from the "Bill of Mat" workbook.

    Hopefully I have been more precise. I really do appreciate your persistance and I apologize for the lack of details from my side.

  21. #21
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to search in the current workbook

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 07-10-2013 at 01:48 PM.

  22. #22
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    Re: How to search in the current workbook

    Quote Originally Posted by AlphaFrog View Post
    Please Login or Register  to view this content.
    It worked!!! Thank you so much, I really appreciate the help and persistance. I promise I'll be clearer in my description next time.

  23. #23
    Forum Contributor
    Join Date
    06-01-2012
    Location
    va
    MS-Off Ver
    2007
    Posts
    163

    Re: How to search in the current workbook

    NVM figured it out.

  24. #24
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to search in the current workbook

    Quote Originally Posted by thelegazy View Post
    It worked!!! Thank you so much, I really appreciate the help and persistance. I promise I'll be clearer in my description next time.
    You're welcome.

    A good description makes all the difference.

    Mark this thread as solved.

+ 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