+ Reply to Thread
Results 1 to 10 of 10

Failing FIND method?

Hybrid View

  1. #1
    Registered User
    Join Date
    11-28-2017
    Location
    Sweden
    MS-Off Ver
    2016
    Posts
    21

    Question Failing FIND method?

    Hello guys.

    I have a scenario where i need to set the interiour color of all cells in a sheet according to it's values.
    The correct color is stored in a list on another sheet and the value that represent the color is loaded into an array along with the colors long value.

    It works pretty well, except it won't set the color on cells that contain a formula instead of a value (despite lookin:=xlValues).
    Can you spot what might be wrong at the first glance of would you like me to attach the xlsx file?
    I don't understand why the code keeps ignoring it, despite that the fact that the FIND method is indeed finding the value.

    Sub DoColors_v2()
      Dim Picker As Variant
      Dim Rws As Long, j As Long
      Dim i As Integer
      Dim k As Integer
      Dim c As Range
      Dim FirstAddress
      Application.StatusBar = "Coloring..."
      Application.ScreenUpdating = False
      
      'load search strings and colors into Picker array
      With Worksheets("Colors").Range("A2:B257")
        ReDim Picker(1 To .Rows.Count, 1 To 2)
        For i = 1 To .Rows.Count
          Picker(i, 1) = .Cells(i, 1).Value
          Picker(i, 2) = .Cells(i, 2).Value
        Next i
      End With
      
      
      
      
      'search the test range, changing backgrounds as required
      With Worksheets("Setup 1").Range("A3:AA2000")
        For k = 1 To UBound(Picker)
          With Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
              Set c = .Find(Picker(k, 1), After:=Range("A3"), SearchOrder:=xlByRows, SearchDirection:=xlNext, LookIn:=xlValues, MatchCase:=False, Lookat:=xlWhole, SearchFormat:=False)
             
              If Not c Is Nothing Then
                  FirstAddress = c.Address
                  Do
                      c.Interior.Color = Picker(k, 2)
                      Set c = .FindNext(c)
                  Loop While Not c Is Nothing And c.Address <> FirstAddress
              End If
          End With
        Next k
      End With
      
    Application.ScreenUpdating = True
    Application.StatusBar = ""
    End Sub

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: Failing FIND method?

          With Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
    This skips the cells that have formula.
    Change something like
          With .Cells
    And
      With Worksheets("Colors").Range("A2:B257")
        ReDim Picker(1 To .Rows.Count, 1 To 2)
        For i = 1 To .Rows.Count
          Picker(i, 1) = .Cells(i, 1).Value
          Picker(i, 2) = .Cells(i, 2).Value
        Next i
      End With
    could be rewrite like
    Picker = Worksheets("Colors").Range("A2:B257").Value
    Last edited by jindon; 12-06-2017 at 05:37 AM.

  3. #3
    Registered User
    Join Date
    11-28-2017
    Location
    Sweden
    MS-Off Ver
    2016
    Posts
    21

    Re: Failing FIND method?

    Thats it!

    Well, almost.
    Making the first change you suggested would cause the code to run very slow (had to break).

    But removing the entire WITH statement did the trick!
    Thanks a lot for getting me on the right track.

  4. #4
    Registered User
    Join Date
    11-28-2017
    Location
    Sweden
    MS-Off Ver
    2016
    Posts
    21

    Re: Failing FIND method?

    Actually..
    I realized this code is backwards.

    The way its written now, it loads 257 variables into an array and then it searches each and every cell in the range for a match from those 257 variables.
    When i first wrote this, it was 16 variables, but now the code wont work anymore (it takes forever).
    Is there a way to do the other way around?
    Check every cell in range to see it the value in that cell exists in the array?
    Or as an alternative skip cells that are empty?
    The range should really be dynamic, but it can grow horizontally or vertically (or both) so i'm not sure which method to use last cell by rows of colums? Or it doesn't matter?

      With Application.ActiveSheet.Range("A3:AA2000")
        For k = 1 To UBound(Picker)
          'With Cells
              Set c = .Find(Picker(k, 1), After:=Range("A3"), SearchOrder:=xlByRows, SearchDirection:=xlNext, LookIn:=xlValues, MatchCase:=False, Lookat:=xlWhole, SearchFormat:=False)
             
              If Not c Is Nothing Then
                  FirstAddress = c.Address
                  Do
                      c.Interior.Color = Picker(k, 2)
                      Set c = .FindNext(c)
                  Loop While Not c Is Nothing And c.Address <> FirstAddress
              End If
          'End With
        Next k
      End With

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: Failing FIND method?

    Too hard without seeing your workbook.

    Completely untested.
    Sub DoColors_v2()
        Dim dic As Object, a, i As Long, r As Range, rng As Range, myPtn As String
        Application.StatusBar = "Coloring..."
        Application.ScreenUpdating = False
        Set dic = CreateObject("Scripting.Dictionary")
        dic.CompareMode = 1
        a = Worksheets("Colors").Range("A2:C257").Value
        For i = 1 To UBound(a, 1)
            If Trim(a(i, 1)) <> "" Then dic(Trim(a(i, 1))) = i
            Set a(i, 3) = Nothing
        Next
        Set rng = Worksheets("Setup 1").Range("A3:AA2000")
        myPtn = Join(dic.keys, Chr(2))
        With CreateObject("VBScript.RegExp")
            .Global = True
            .Pattern = "([()^|\\\[\]{}+*?.-])"
            myPtn = Replace(.Replace(myPtn, "\$1"), Chr(2), "|")
            .Pattern = "^(" & myPtn & ")$"
            For Each r In rng
                If .test(r.Value) Then
                    If a(dic(r.Value), 3) Is Nothing Then
                        Set a(dic(r.Value), 3) = r
                    Else
                        Set a(dic(r.Value), 3) = Union(a(dic(r.Value), 3), r)
                    End If
                End If
            Next
        End With
        For i = 1 To UBound(a, 1)
            If Not a(i, 3) Is Nothing Then a(i, 3).Interior.Color = a(i, 2)
        Next
        Application.ScreenUpdating = True
        Application.StatusBar = ""
    End Sub

  6. #6
    Registered User
    Join Date
    11-28-2017
    Location
    Sweden
    MS-Off Ver
    2016
    Posts
    21

    Re: Failing FIND method?

    I understand it's rather hard to re-invent it just based on this. The "Colors" sheet is just a two column list and all "color identifiers" are max 3 chars.
    IŽll try your code out..

    Kort	Long
    l01	9687785
    l02	5481413
    l03	5879001
    l04	15594224
    l05	10920610
    l06	7567488
    l07	2301035
    l08	5584927
    l09	3293713
    l10	2828583
    l11	2303131
    l12	12732191
    l13	5341952
    l14	46583
    l15	5854282
    l16	945890
    f02	7351040
    f06	5854282
    f07	1843005
    f12	6502144
    f13	2045962
    f16	11576222
    f17	2434211
    f18	1710362
    f23	2713126
    f26	13422541
    f27	1843115
    f30	13429756
    f32	11231511
    f33	8038021
    f37	1314665
    f39	1850347
    f41	10239331
    f42	4527872
    f47	1185735
    f49	2055904
    f50	15003894
    f62	12950635
    f69	54271
    f73	12247999
    f80	16777215
    f88	1710362
    f90	16777215
    f96	14474460
    DF	8960717
    BY	8696016
    DG	7187154
    BL	43257
    DH	40676
    DI	2002635
    DJ	37090
    64	36072
    DK	5210287
    DL	2600925
    20	13031907
    DM	10142941
    17	11916006
    AZ	3726833
    23	5286390
    AC	3197690
    34	8032164
    DN	6655904
    08	46838
    45	46583
    DO	5017530
    DP	65535
    DQ	950183
    AP	39935
    85	41954
    63	1874681
    29	5414123
    BR	7373712
    BJ	4154496
    DR	37616
    AE	28378
    82	1788090
    AK	2243007
    BO	2652406
    BX	218082
    DS	413183
    DT	45823
    68	2190317
    78	480222
    19	2645456
    96	945890
    DU	5072341
    62	2440850
    BF	2107815
    59	2303131
    09	2171803
    46	2235014
    57	2301035
    32	2038105
    DV	2236478
    DW	2962541
    37	2303097
    BM	7177414
    AV	2436759
    DX	7697355
    BK	10920152
    AF	3095974
    65	6116811
    DY	4865991
    07	1056443
    DZ	5597647
    CR	2174463
    EA	1780479
    AW	3942315
    EB	2370764
    AG	3617958
    EC	2301296
    ED	2964133
    BD	8610177
    EE	4930701
    BA	9200068
    AJ	3677797
    AD	10119286
    AN	7549584
    EF	3941447
    AL	8539268
    EG	9602717
    BU	7815356
    EH	8872814
    EI	8350571
    15	741169
    EJ	674831
    58	66816
    24	5584927
    AH	2629145
    60	67584
    13	920119
    EK	4471339
    36	810786
    06	67328
    EL	3943194
    10	65792
    AB	5452057
    14	986211
    AU	65792
    BB	67840
    11	65797
    BE	67840
    CK	460288
    05	65792
    BS	5909794
    75	920130
    26	1117792
    EM	789025
    EN	5386255
    EO	6294332
    16	3476022
    27	2644274
    EP	3953488
    EQ	394754
    81	3293713
    ER	3029308
    ES	2241068
    ET	2765879
    EU	2766119
    EV	3738445
    48	5864556
    EW	3816752
    EX	5928573
    EZ	3490119
    BG	3554621
    FA	459008
    BP	4197464
    84	3870561
    04	11325113
    FB	3097143
    76	7838090
    FC	2569018
    CN	524544
    AT	3869534
    FD	461056
    61	11909758
    22	4346929
    BN	393472
    FE	5377059
    12	789830
    88	11316346
    FF	2444569
    FG	460804
    FH	262400
    FI	131328
    89	9341050
    41	10327692
    FJ	6518913
    FK	6911610
    83	10197915
    25	7040620
    AA	6187638
    FL	4021876
    FM	5791837
    67	5659736
    AQ	6117714
    43	6184279
    92	5788495
    28	4341304
    02	3420719
    CL	4475468
    BC	7766144
    30	5130565
    CP	4539191
    AX	8752786
    47	658011
    49	10596533
    31	7635583
    FN	7309458
    01	12896197
    93	9606039
    33	8027002
    87	11120816
    BW	6186603
    50	10591896
    AM	9540238
    90	5263951
    35	11056055
    69	9802381
    AI	9078399
    86	13093064
    FO	7568257
    FP	4090249
    CS	2843293
    FQ	4083065
    97	2509694
    98	3230093
    FR	2770288
    FS	2443890
    FT	2504794
    44	2831206
    91	2504010
    FU	2502494
    55	2108236
    FV	2699076
    AS	3487293
    FW	1578778
    95	2709412
    FX	3690617
    40	4675701
    FY	2767441
    FZ	3227775
    BH	13820137
    21	13358551
    66	15199468
    GA	2894635
    18	1052174
    80	10527137
    79	8488327
    03	14806257
    56	2828583
    94	15397105
    BT	2763050
    BI	12897224
    CI	8619397
    GB	8027001

  7. #7
    Registered User
    Join Date
    11-28-2017
    Location
    Sweden
    MS-Off Ver
    2016
    Posts
    21

    Red face Re: Failing FIND method?

    Well, that quickly went out or range. ^^

    I really appreciate your time. If you feel up for it, i've uploaded my WB on dropbox. (didn't manage to open the attach dialogue on this forum correctly)
    https://www.dropbox.com/s/behlyum1ny...ning.xlsm?dl=0

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: Failing FIND method?

    Change
        For i = 1 To UBound(a, 1)
            If Trim(a(i, 1)) <> "" Then dic(Trim(a(i, 1))) = i
            Set a(i, 3) = Nothing
        Next
    to
        For i = 1 To UBound(a, 1)
            If Trim(a(i, 1)) <> "" Then dic(a(i, 1)) = i
            Set a(i, 3) = Nothing
        Next

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: Failing FIND method?

    One more change
    Insert one line
        With CreateObject("VBScript.RegExp")
            .Global = True
            .IgnoreCase = True  '★--- this line
            .Pattern = "([()^|\\\[\]{}+*?.-])"

  10. #10
    Registered User
    Join Date
    11-28-2017
    Location
    Sweden
    MS-Off Ver
    2016
    Posts
    21

    Re: Failing FIND method?

    Ah, that's right, or it will fail the regex when the case differs.
    I never though of using regex here, but your solution is much better than mine.
    It still takes a while if the area is huge but atleast it's doable.

    Thank you so much jindon! <3

+ 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. Workbooks.Open method failing to run
    By JasonLeisemann in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-21-2013, 02:49 PM
  2. Select Method failing
    By j_Southern in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-18-2011, 05:34 AM
  3. CELLS.Select method failing
    By ctmurray in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-11-2008, 06:51 PM
  4. copy method of object failing
    By vj2india in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-19-2006, 09:25 PM
  5. Select Method Failing
    By cmk18 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2005, 07:05 PM
  6. [SOLVED] Publish method failing, can't understand why
    By Mark in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-22-2005, 03:05 PM
  7. [SOLVED] Excel Copy Method Failing.
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-11-2005, 01:06 PM
  8. [SOLVED] add method of validation failing
    By mark kubicki in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-19-2005, 04:06 PM

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