0

Need to insert a statement that when VDB (i,35) = Deleted that the entire row interior color turns color index 22 (coral).

I need this step to occur within this block, without needing to add code in a new block (unless absolutely necessary), as sheet has 20K plus entries. I am assuming since this is where I am identifying when the item has a status of "Deleted", and placing "Deleted" into column 35, I should be able to color it in this same step/block, and it would be most efficient method. I may be wrong..

Is there another line I can add after the last line, that will color these entries that ="Deleted" in col index 35?

I have tried passing vDB(i,35) to another variable as a range, and setting it, and then using if it = Deleted to change the entirerow.interior.color index = 22 , but I can't get the phrasing right, and may be taking wrong approach. I am still in learning curve but try to figure out my own issues, before bugging the group, but I can't seem to get it right.

Here is snip it.

'Execute Find (Vlookup)

For i = 1 To UBound(vDB, 1)
'If sht.Cells(i, 1).Value <> "" Then
    If vDB(i, 1) <> "" Then
        Set rng = rngData.Find(vDB(i, 1), LookIn:=xlValues, Lookat:=xlWhole) 'Matches entire contents of cell for an exact match
        If Not rng Is Nothing Then
           'If found return value of column 9 of ABC Recalc Cycle Count Remainder Browse (offset by 2), into ABC Matrix monthly ABC Code column, as determined by variable
            vDB(i, ABCCodeCell) = rng.Offset(, 7)
            'If found, return the value of column 7 of ABC Recalc Cycle Count Remainder Browse (offset by 2), into ABC Matrix column 27
            vDB(i, 27) = rng.Offset(, 5)
            'If found, return the value of column 11 of ABC Recalc Cycle Count Remainder Browse (offset by 2), into ABC Matrix column 34
            vDB(i, 33) = rng.Offset(, 9)
            'If found, place value of ABCMatrixMonthSelect.ComboBox1 in column AO Col Index 41
            vDB(i, 41) = ABCMatrixMonthSelect.ComboBox1.value
        Else
            vDB(i, 35) = "Deleted"
            vDB(i, 41) = ABCMatrixMonthSelect.ComboBox1.value
            With vDB(i, 1) = sht.Cells.Interior.Color = RGB(247, 119, 109) 'Light Red
            End With
        End If
    End If
    If vDB(i, ABCCodeCell) = vDB(i, lastMonthABCCode) Then
    vDB(i, 36) = "No"
    Else
    vDB(i, 36) = "Yes"
    End If
DoEvents
Next
rngDB = vDB

Dim LR As Long
LR = sht.Cells(Rows.Count, 1).End(xlUp).Row
sht.Cells.FormatConditions.Delete
With sht.Range("1:" & LR)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$AI1=""Deleted""" 'Searches for value "Deleted" in Range 1 to last row
    With .FormatConditions(.FormatConditions.Count)
       .SetFirstPriority
       With .Interior
           .Color = RGB(247, 119, 109) 'Light Red
       End With
    End With
End With

'Reset Excel Status Bar
Application.StatusBar = False
e here
  • Have you tried using conditional formatting? – Cyril Aug 30 '17 at 19:25
  • I need this VBA, for a report. Not something I need to set, or rely on a user to set, unless you are saying that by VBA I can set the conditional formatting, verses coloring the rows.. that would be different. This is a large macro, takes values from various reports, and compiles month after month. The rows with "Deleted" value in 35, would be highlighted until the next month the report was processed, then I would turn them back clear, rerun steps to identify the new "Deleted:.. But I can just as easily wipe out their value in 35 (as that is hidden for my own intents), if that is better – SharePoint0508 Aug 30 '17 at 19:37
  • I'm open to whatever way makes the most sense, is best approach, and works the fastest, as this is such a large sheet. – SharePoint0508 Aug 30 '17 at 19:41

1 Answers1

0

If you can use conditional formatting, use the rule type: Use formula to determine which cells to format, in the formula bar type =$AJ35="Deleted", then define your range such as =1:1000. The Position of the $ in the formula is the key to that. You then would choose what happens when the criteria is met and set the rule.

You can do this from VBA, setting conditional formatting, such as:

Dim LR as Integer
LR = Cells(Rows.Count, 1).End(xlUp).Row 'just finding the row # of the last row
sht.Cells.FormatConditions.Delete
With sht.Range("1:" & LR) 'The range you're working with... specify the Rows() to ensure the whole row is colored 
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$AI1=""Deleted""" 'Where you put the cell you want to evaluate and criteria
    With .FormatConditions(.FormatConditions.Count)
        .SetFirstPriority
        With .Interior
            .Color = RGB(0, 70, 255) 'Arbitrary; you will need to find the specific color you want.
            .TintAndShade = 0.8
        End With
    End With
End With

Note that I remove existing conditional each time.

Additionally, another option is to just say, which doesn't use conditional formatting:

With sht.Rows(i).Interior
    .Color=RGB(0,255,0) 'arbitrary color
    .TintAndShade = 0.8
End With

Edit: With your adjusted code and desire for columns 1 to 35 to be color-coded:

Else
    vDB(i, 35).Value = "Deleted"
    With sht.Range(sht.Cells(i,1),sht.Cells(i,35)).Interior
        .Color = RGB(247, 167, 184) 'light red
        .TintAndShade = 0.8
    End With
End If

I'm thinking something is up with your vDB, so maybe it will be more appropriate to just use Cells?

Else
    Cells(i, 35).Value = "Deleted"
    With sht.Range(sht.Cells(i,1),sht.Cells(i,35)).Interior
        .Color = RGB(247, 167, 184) 'light red
        .TintAndShade = 0.8
    End With
End If

Similarly:

Dim i, LR as Integer
LR = Cells(Rows.Count,1).End(xlUp).Row 'Assumes row 1 is contiguous... probably not, given the rest of this code
For i = 1 To LR
    If Cells(i, 1) <> "" Then
        Set rng = rngData.Find(Cells(i, 1), LookIn:=xlValues, Lookat:=xlWhole)
        If Not rng Is Nothing Then
            Cells(i, ABCCodeCell).Value = rng.Offset(, 7).Value
            Cells(i, 27).Value = rng.Offset(, 5).Value
            Cells(i, 34).Value = rng.Offset(, 9).Value
        Else
            Cells(i, 35).Value = "Deleted"
            With sht.Range(sht.Cells(i,1),sht.Cells(i,35)).Interior
                .Color = RGB(247, 167, 184) 'light red
                .TintAndShade = 0.8
            End With
Cyril
  • 6,448
  • 1
  • 18
  • 31
  • The value is in AI, or column index 35, however it needs to be phrased, and wherever the value of any cell within columns AI = "Deleted", it needs to be light red/coral color index 22. I don't need to color just the cell, I need to highlight the entire row, or the range from A to AI, in any row where the value of AI is deleted, until the last row the sheet contains. Month after month, the last row will move, as about 1500 rows are added each monthly update. – SharePoint0508 Aug 30 '17 at 19:55
  • I tried using the conditional format, I did not see where to define the range, or set the entire row. I can't seem to get that to work – SharePoint0508 Aug 30 '17 at 19:57
  • also it is giving a Syntax error on second line : With sht.Range(sht.Rows(1), sht.Rows(sht.Cells(sht.Rows.Count.End(xlUp).Row, 1)) – SharePoint0508 Aug 30 '17 at 20:05
  • Give me a sec to update and I'll leave some comments in where you put your specifics. As listed, the entire rows will be colored, not just the cell. The formulas are based on the cell value, though. – Cyril Aug 30 '17 at 20:06
  • Try now, for the conditional formatting piece. If you are adding more items regularly, and the code will have to keep re-applying the conditional formatting, the straight VBA could (last option) would be more appropriate, as it is input directly to your code in the Else part of the if statement. – Cyril Aug 30 '17 at 20:13
  • Thank you!!! Btw... we meet again! I believe you have helped guide me before. I am thinking honestly coding it into the macro without formatting might be best approach. I did this. I see it came out a lovely shade of green. ONLY ONE PROBLEM.. it appears to have skipped a few that have a value of "Deleted" - any reason why this might happen? – SharePoint0508 Aug 30 '17 at 20:30
  • There is not any spaces, or user entry, or other on these cells in the column. The step above in the code is the ONLY ACTION that touches this column, and places a value in it. Why would it recognize some deleted, and not others? - Other than that it is PERFECT, and I think the direction I am going to take. Just need it to not miss any. – SharePoint0508 Aug 30 '17 at 20:32
  • and.. last but not least. Is there a way it highlights A to AI, instead of row? if the value of AI is "Deleted" – SharePoint0508 Aug 30 '17 at 20:33
  • sht.Rows(i) is a Worksheet and a Range. You can define the Range however you would like, such as Range(Cells(i,1),Cells(i,35)) to specify only so many columns being affected. As for the "Deleted" not being picked up, see that there isn't a space, e.g., "Deleted ". Regarding the color, record a macro and fill a cell with your desired color; copy/paste that into the macro for the desired result. – Cyril Aug 30 '17 at 20:40
  • That is the first thing I checked for was hidden spacing. It isn't plausible as right before I am setting the color, I am setting the value of any cells that do not run through the previous logic statements with an Else statement that sets those to "Deleted". It is not manual entry, or user entry, or performed in any other action. – SharePoint0508 Aug 31 '17 at 12:29
  • Is it because I do not have something written to say (but in proper syntax) "If value of 35 of any cell = Deleted, color it red 22"? I say this because if may be shading but maybe I don't have that correct logic. I am going to update the code snip it above with what the current code is now incorporating your suggestion. :) BTW... thank you for all your help, we meet again :) – SharePoint0508 Aug 31 '17 at 12:31
  • No worries; hopefully we can get this fixed for you. I can't make any promises, but we can look through what you've added and hopefully there is something jarring! – Cyril Aug 31 '17 at 12:33
  • Edited it. Thank you! You are very kind. I am doing rather well and have wrapped up 99% of this, but this last thing was throwing me for a loop, and I find that I overthink where steps could go, and in conjunction with other steps, or in their own block. That is what tends to stump the chump so to speak! lol I am more a workflow Goddess as of late, and I get stuck in that mindset of order of operations, which in a macro, doesn't necessarily need to flow that same way.. – SharePoint0508 Aug 31 '17 at 12:40
  • Just ran it again, same result. It is still missing highlighting about 25% of these. It is set to autofilter the result and copy it to another sheet later, in steps after this, but before macro ends, so I see it right away when it completes. Not sure why it skips these values.. :( – SharePoint0508 Aug 31 '17 at 12:50
  • So, the code looks correct, regarding your Else statement. My guess is your loop For i = 1 to UBound(vDB,1) is causing an issue that makes the loop skip cells. Have you tried making the backend of the loop a single variable, defined as an integer, e.g., *Dim LR as Integer*? Also, specify the Step in that scenario to really make sure, e.g.. *For i = 1 to LR Step 1*. That's about all i can guess at this point. You may want to use F8 and step-through the code to see if the cells are being assessed or not. – Cyril Aug 31 '17 at 13:12
  • I've tried to use F8, but it goes so fast, that it literally blinks and changes the cells red. It doesn't select or show anything. There is 20K rows on this sheet, so that probably accounts for why as well. – SharePoint0508 Aug 31 '17 at 14:06
  • I am not sure how to accomplish what you stated for the loop. The loop is identifying the cells as deleted correctly, it isn't missing any. What it tends to skip is the formatting step. After defining ones that are deleted, it does not seem to color them all, without any rhyme or reason that I can see, what it is skipping some. I wish I could post a screenshot, and show you. If I can't do it during this step, then I need to write a block of syntax that looks for cells with value of "Deleted" in AI, and highlights them red. – SharePoint0508 Aug 31 '17 at 14:09
  • I am also struggling with A:AI turning red, verses entire row. I have tried to set a new range called colorRng, to refer to, using the A, I and AI, I, and cannot seem to get it work without giving me error. – SharePoint0508 Aug 31 '17 at 14:11
  • See my edit; hopefully I hit on all topics. I do believe you've an issue with the vDB and how the UBound you're using in your loop is being utilized. – Cyril Aug 31 '17 at 14:51
  • Ok.. here is where I am at. I ran it using the other way this time, in a stand alone block, and not placed with the VDB, I literally pasted what you wrote as the first option. – SharePoint0508 Aug 31 '17 at 16:28
  • Specifically the Dim LR as Integer, copied and pasted it exact. It is working better.. BUT... It seems to stop. Is there a limit to how many rows this method will work on? It quits after row 20,580 . anything after that, it wont color. It is also coloring entire row. I need to DIM a range to color A : AI, and that will take from first available row of results to last row of results of autofilter by "delete" typically this will be MUCH LESS a return than what it is giving now. Around 1308 have value of delete. Normally it will be under 100 I assume. – SharePoint0508 Aug 31 '17 at 16:33
  • Whoops, the whole row is my bad. I displayed it in my code, but didn't put it in the "similarly:" section with your actual code. Regarding the LR, it is based on CONTIGUOUS cells, so they must have information filled into the adjacent cells. I commented in that line of code that it most likely will NOT work as is, because i don't know what column fulfills the contiguous requirement. In the LR = Cells(Rows.Count,1) section, that 1 is the column, so you can change that number to 4 for D, or put in "D" and it will read the same. – Cyril Aug 31 '17 at 17:21
  • Made few changes.. 3, 333,333 time trying this better be the charm! LOL :) None the less.. This really is giving mixed results. I tried just changing to cells, and it cycled right through it. Acted like it worked..Didn't even put one "DELETED" in. Same result if I change the whole block to the cell method... it literally is not running that else condition, but runs statements that I through in there after to fill columns, and you guessed it.. is working till last row.. Why oh why Excel????!! – SharePoint0508 Sep 01 '17 at 20:16
  • best result so far has been with first option, but it literally doesn't work to last row and stops for whatever reason around 20,580. But any other conditional formatting I have created manually (I have some icons) are working till last row perfectly. I may start another question to try to troubleshoot the exact problem with my code method from where we are now.. that is working the best. This is really frustrating me. I am almost done with this thing.. I just have a set of red to make for deleted, and a set of green for added. Then I AM DONE!! and I can't get there.. ughh! – SharePoint0508 Sep 01 '17 at 20:18
  • I have made a new question and tagged you (hopefully you received notification, and that is okay, I don't want to wear out my welcome or take advantage of your kindness). I thought it was best to start a new thread as this original problem you answered, and it has taken on new life.. with just not executing correctly. – SharePoint0508 Sep 01 '17 at 20:38
  • Tag didn't show up, but no worries; I'm sure someone else will be able to weigh in. My only other suggestion in the mean time is to try hard coding some values to ensure that you're not having *another* issue, e.g., define your search range from 2 to row 99,999 and run the loop. If something gets missed, you have a better understanding of the problem. – Cyril Sep 05 '17 at 12:51
  • I have no solution as of yet. The only thing people have stated is to change LR to = Long, not integer, but when I do that, the next statements give an error – SharePoint0508 Sep 05 '17 at 13:14
  • It is now giving me a 438 object error on .formatconditions.add statement – SharePoint0508 Sep 05 '17 at 13:16
  • Dim LR as Long versus Dim LR as Integer are about the same for this, though Long is the safer bet regarding storage and depending on your system. See https://stackoverflow.com/questions/26409117/why-use-integer-instead-of-long . Not sure what's causing the 438 error (object not supported), provided you use: Dim LR as Long LR = Cells(Rows.Count, 1).End(xlUp).Row . Unless you're executing from a different sheet than what the data is being pulled from; you would have sht.Range(sht.Rows(1),sht.Row(LR)) to ensure it pulls correct data (at least an option). – Cyril Sep 05 '17 at 13:44
  • I ended up deleting the block as visually they looked the same, then I added it back in, and it is working fine. I have LR as integer. But will attempt as Long. I played around with this.. I got it working.. but.. it is highlighting the entire row. :( but it is making it to the end. – SharePoint0508 Sep 05 '17 at 13:56
  • I had to have it work in two spots to achieve this. But now I have another area. As always, user requirements changed again, and they want icons to identify rows with certain conditions (past due, etc). I have this working, but it is by conditional formatting. If I delete the conditional formatting rules, I need to put them back in using VBA to make the icons, or I need a method that will not use conditional formatting, so I do not need to wipe it out before the coloring. – SharePoint0508 Sep 05 '17 at 14:12
  • Two things: .1) we need to close out this thread if possible, or it stays in an open state indefinitely. .2) I would recommend doing all the conditional formatting in VBA as the ranges will be changing, per earlier discussion. Take a look at this link to help with using icons: https://stackoverflow.com/questions/18232480/how-can-i-use-vba-to-format-symbols-icons-into-cells-without-using-conditional – Cyril Sep 05 '17 at 14:17
  • I just updated the code in my question above, to show you the two spots I am running the same block essentially. The combination of these two efforts are running to Last row perfectly. It just is highlighting whole row, which at this point Cyril... I am over it! LOL! It can highlight the entire row, just as long as it works. If I remove either block, it does not work till end, and produces the EXACT SAME RESULT. It is crazy! The both together, work. My only issue now is to change the second block to not use conditional formatting. – SharePoint0508 Sep 05 '17 at 14:18
  • lol, we've solved the issue with the row versus the specific columns! With sht.Range("1:" & LR) becomes With sht.Range(sht.Cells(i,1),sht.Cells(i,35)), provided it is within the loop (not what you've currently written). If you want the loop to apply colors, without using conditional formatting, that is also in the answer to this post: With sht.Rows(i).Interior .Color=RGB(0,255,0) 'arbitrary color .TintAndShade = 0.8 End With. Similarly, you would change the sht.Rows(i) to the suggested range with .range(.cells(),.cells()). – Cyril Sep 05 '17 at 14:32