+ Reply to Thread
Results 1 to 21 of 21

This macro works... kind of. Please spot my error.

Hybrid View

Alias1431 This macro works... kind of.... 01-09-2014, 06:28 PM
gsnidow Re: This macro works... kind... 01-09-2014, 06:37 PM
Alias1431 Re: This macro works... kind... 01-09-2014, 06:43 PM
gsnidow Re: This macro works... kind... 01-09-2014, 06:48 PM
Alias1431 Re: This macro works... kind... 01-09-2014, 06:50 PM
stnkynts Re: This macro works... kind... 01-09-2014, 06:38 PM
Alias1431 Re: This macro works... kind... 01-09-2014, 06:42 PM
gsnidow Yes, tomorrow. That is, if... 01-09-2014, 08:26 PM
shg Re: This macro works... kind... 01-09-2014, 08:55 PM
gsnidow Re: This macro works... kind... 01-10-2014, 07:38 AM
Alias1431 Re: This macro works... kind... 01-10-2014, 10:45 AM
Alias1431 Re: This macro works... kind... 01-10-2014, 01:41 PM
gsnidow Re: This macro works... kind... 01-10-2014, 01:48 PM
Alias1431 Re: This macro works... kind... 01-10-2014, 01:52 PM
gsnidow Re: This macro works... kind... 01-10-2014, 01:59 PM
gsnidow Re: This macro works... kind... 01-10-2014, 01:56 PM
Alias1431 Re: This macro works... kind... 01-10-2014, 02:03 PM
gsnidow Re: This macro works... kind... 01-10-2014, 02:07 PM
Alias1431 Re: This macro works... kind... 01-10-2014, 02:10 PM
gsnidow Re: This macro works... kind... 01-10-2014, 02:24 PM
Alias1431 Re: This macro works... kind... 01-10-2014, 02:31 PM
  1. #1
    Registered User
    Join Date
    11-13-2013
    Location
    Richland, Washington
    MS-Off Ver
    Excel 2010
    Posts
    23

    This macro works... kind of. Please spot my error.

    The following macro works great... except when I add more terms to the array. In all, I have about 1000+ terms. Every single time I try to add in the rest, I get an error. Code follows:

    Option Explicit

    Sub BPO()
    Dim TargetList
    TargetList = Array("ABRB026", "ABRB026A", "ABRB026AA", "ABRB027")

    'Store the active cell in order to return here
    Dim ActualCel As Range
    Set ActualCel = ActiveCell

    Dim cel As Range, Er As Boolean
    Dim i As Long

    For i = 0 To UBound(TargetList)
    On Error GoTo ErrorHandler

    'Verify first cell in worksheet
    Cells(1, 1).Activate
    If UCase(ActiveCell) = UCase(TargetList(i)) Then
    ActiveCell.Interior.Color = vbGreen
    End If

    Er = False
    Set cel = Nothing

    Do While Not Er
    Set cel = Cells.Find(What:=TargetList(i), After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False)

    cel.Activate 'Here an error occured if the text is not found
    ActiveCell.Interior.Color = vbGreen

    Set cel = Cells.FindNext(After:=ActiveCell)
    Er = (cel.Interior.Color = vbGreen) 'If a previous cell is founded again Er=True
    Loop

    NextI:
    Next i

    Ex:
    MsgBox ("Who's awesome? You're awesome.")
    Exit Sub

    ErrorHandler:
    If i = UBound(TargetList) Then
    Resume Ex
    Else
    Resume NextI
    End If
    End Sub

  2. #2
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: This macro works... kind of. Please spot my error.

    You are going to type out an array list of over 1000 items? Why not put them in a column in another sheet, then load them from there? I get no errors when I run your code, even if I add some arbitrary items to the array. Not sure what it is doing though.

    Greg
    Just a guy trying to make work stuff easier.

  3. #3
    Registered User
    Join Date
    11-13-2013
    Location
    Richland, Washington
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: This macro works... kind of. Please spot my error.

    Quote Originally Posted by gsnidow View Post
    You are going to type out an array list of over 1000 items? Why not put them in a column in another sheet, then load them from there? I get no errors when I run your code, even if I add some arbitrary items to the array. Not sure what it is doing though.

    Greg
    Load them from another sheet? What do you mean by this?

  4. #4
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: This macro works... kind of. Please spot my error.

    Quote Originally Posted by Alias1431 View Post
    Load them from another sheet? What do you mean by this?
    What I'm saying is, if you've got a list of stock codes to process, I'm sure you have them in a document somewhere right? Put that list of stock codes into your workbook. Then, instead of iterating through your array items, you would set the list as a range, and iterate through the range items one by one the same way you are iterating through the array.

  5. #5
    Registered User
    Join Date
    11-13-2013
    Location
    Richland, Washington
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: This macro works... kind of. Please spot my error.

    That sounds ideal. Can you point me towards a guide that will show me how to do that?

  6. #6
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: This macro works... kind of. Please spot my error.

    You are not going to be able to store 1000+ strings in an array. What is it you are trying to do, there might be a better way.

  7. #7
    Registered User
    Join Date
    11-13-2013
    Location
    Richland, Washington
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: This macro works... kind of. Please spot my error.

    Ahh, I feared as much. Essentially I have a long list of stock codes that I would like highlighted when I run a macro on the excel reorder report.

  8. #8
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150
    Yes, tomorrow. That is, if someone does not beat me to it

  9. #9
    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: This macro works... kind of. Please spot my error.

    Try this:

    Sub BPO()
        Dim avsTgt      As Variant
        Dim iTgt        As Long
        Dim rFind       As Range
        Dim sAddr       As String
    
        avsTgt = Array("ABRB026", "ABRB026A", "ABRB026AA", "ABRB027")
    
        For iTgt = LBound(avsTgt) To UBound(avsTgt)
            Set rFind = Cells.Find(What:=avsTgt(iTgt), _
                                   LookAt:=xlWhole, _
                                   MatchCase:=False)
            If Not rFind Is Nothing Then
                sAddr = rFind.Address
                
                Do
                    rFind.Interior.Color = vbGreen
                    Set rFind = Cells.FindNext(After:=rFind)
                Loop While rFind.Address <> sAddr
            End If
        Next iTgt
    End Sub
    Please take a few minutes to read the forum rules, and then edit your post to add CODE tags.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: This macro works... kind of. Please spot my error.

    Back to what I mentioned yesterday, about using a range of stock items against which to check orders, rather than an array, you can try this. And, I took lead from shg, in that I did not start with your original code. I think a lot of it can be pared down, so this is a simple starting point. It assumes your list of stock items is in sheet2, and your orders are in sheet1. You can change as needed. The benefits of using a range instead of an array are 1) you are not going to run out of memory if your list of stock items gets too large, and 2) you are not going to have to type in all the values in your array.

    Greg

    Sub BPO2()
    
        Dim rngStock As Range
        Dim rngOrders As Range
        Dim WSStock As Worksheet
        Dim WSOrders As Worksheet
        Dim rFind As Range
        Dim c As Variant
        
        Application.ScreenUpdating = False
        
        Set WSStock = Sheets("Sheet2")
        Set WSOrders = Sheets("Sheet1")
        
        Set rngStock = WSStock.Range("A1:A" & WSStock.Cells(Rows.Count, "A").End(xlUp).Row)
        Set rngOrders = WSOrders.Range("A1:A" & WSOrders.Cells(Rows.Count, "A").End(xlUp).Row)
        
        With rngStock
            For Each c In rngOrders
                Set rFind = .Cells.Find(What:=c.Value, _
                                        LookIn:=xlValues, _
                                        MatchCase:=False)
                If Not rFind Is Nothing Then
                    c.Interior.Color = vbGreen
                End If
            Next c
        End With
    
        Application.ScreenUpdating = True
        
    End Sub

  11. #11
    Registered User
    Join Date
    11-13-2013
    Location
    Richland, Washington
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: This macro works... kind of. Please spot my error.

    Thanks for the input! I will take a look at this and get back to you guys with the results.

  12. #12
    Registered User
    Join Date
    11-13-2013
    Location
    Richland, Washington
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: This macro works... kind of. Please spot my error.

    Ok, two things. First, it appears to have highlighted all the blank cells as well as the requested ones. Also, would it be possible to search from multiple columns on the seconds sheet? Some stock codes have an A or AA at the end, and I would like to include those possible variations in the search. Thanks for your help, guys!

  13. #13
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: This macro works... kind of. Please spot my error.

    I'm pretty sure the one I supplied only makes green the values on sheet 1 that are also in the list on sheet 2. If your stock codes are sometimes appended with extra characters, like "A" or "AA", would it make sense to only match on the first N characters? Based on your initial 4 array elements, that would be 7 characters.

    Greg

  14. #14
    Registered User
    Join Date
    11-13-2013
    Location
    Richland, Washington
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: This macro works... kind of. Please spot my error.

    That's good thinking. Here is a picture I took for you showing the excess green highlights:

    http://i.imgur.com/YlKGvMx.png

  15. #15
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: This macro works... kind of. Please spot my error.

    Quote Originally Posted by Alias1431 View Post
    That's good thinking. Here is a picture I took for you showing the excess green highlights:

    http://i.imgur.com/YlKGvMx.png

    Can you attach your file? I'm not able to get the code I supplied to behave like that.

  16. #16
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: This macro works... kind of. Please spot my error.

    Quote Originally Posted by Alias1431 View Post
    Some stock codes have an A or AA at the end, and I would like to include those possible variations in the search. Thanks for your help, guys!
    DOH! As it turns out, the cells.find method as posted will look for any instance of the search term in the range. So, for example, if your current search term is "CODE01", and the List of stock codes includes "CODE01A" only, then it would be found. This works to your advantage in certain situations, but I can also be a detriment, like if you only want to find exact matches. I would recommend looking for exact matches, but that's just me.

    Greg

  17. #17
    Registered User
    Join Date
    11-13-2013
    Location
    Richland, Washington
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: This macro works... kind of. Please spot my error.

    Hopefully this link works for you:

    http://fs10u.sendspace.com/upload?SP...TINATION_DIR=6

  18. #18
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: This macro works... kind of. Please spot my error.

    Nope. My company blocks lots of stuff, but I am able to open the files folks attach to posts here. That is probably the only way I'll get it.

  19. #19
    Registered User
    Join Date
    11-13-2013
    Location
    Richland, Washington
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: This macro works... kind of. Please spot my error.

    Think that did it.
    Attached Files Attached Files

  20. #20
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: This macro works... kind of. Please spot my error.

    Ok, it makes perfect sense now, after seeing your workbook. You had blank cells in the range of stock items. So, when the code encountered a blank cell in the search range, then found a blank cell in the lookup list, it returned a match. If you eliminate all blank rows in your stock item list this will not happen. If, for some reason, you need them in there, I added a non-blank validation. Let me know if this does not solve your issue.
    Sub BPO()
    
        Dim rngStock As Range
        Dim rngOrders As Range
        Dim WSStock As Worksheet
        Dim WSOrders As Worksheet
        Dim rFind As Range
        Dim c As Variant
        
        Application.ScreenUpdating = False
        
        Set WSStock = Sheets("Sheet2")
        Set WSOrders = Sheets("Sheet1")
        
        Set rngStock = WSStock.Range("A1:A" & WSStock.Cells(Rows.Count, "A").End(xlUp).Row)
        Set rngOrders = WSOrders.Range("A1:A" & WSOrders.Cells(Rows.Count, "A").End(xlUp).Row)
        
        With rngStock
            For Each c In rngOrders
                If c.Value <> "" Then
                    Set rFind = .Cells.Find(What:=c.Value, _
                                            LookIn:=xlValues, _
                                            MatchCase:=False)
                    If Not rFind Is Nothing Then
                        c.Interior.Color = vbGreen
                    End If
                End If
            Next c
        End With
    
        Application.ScreenUpdating = True
        
    End Sub
    Greg

  21. #21
    Registered User
    Join Date
    11-13-2013
    Location
    Richland, Washington
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: This macro works... kind of. Please spot my error.

    This works perfectly. Good Guy Greg. Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Spot the error... VLOOKUP
    By emilyrose in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-22-2012, 09:11 AM
  2. [SOLVED] Macro error when ran on multiple sheets on workbook (even though macro works)
    By steven_e in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2012, 06:09 PM
  3. [SOLVED] VBA Code is copying a single cell only. Please spot the error for me.
    By AB33 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-01-2012, 03:30 AM
  4. Automation Error At Odd Spot in Code Execution
    By Jenn68 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 05-02-2012, 11:50 AM
  5. cant spot the error in if statement!!
    By tanbio in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 12-05-2006, 04:14 PM

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