0

My issue now is with my conditional formatting rules - they are working as expected up to a certain point in column W, which is where the cells should be either highlighted or passed over. I have included a set of data from column W and column Z (the reference data) where there are matches that are not being highlighted. In the Column W data I have bolded the numbers that should be highlighted.

Column Z - Z506-Z550

  • 233892
  • 233899
  • 959460
  • 156311
  • 515114
  • 549794
  • 562793
  • 372953
  • 230659
  • 230717
  • 2051205586
  • 364834
  • 790760
  • 334588
  • 538149
  • 288261
  • 19326
  • 267428
  • Net 90
  • 473853
  • 3211221994
  • 264556
  • 260798
  • 156271
  • 509597
  • 2211211506
  • 800990
  • 597593
  • 431759
  • 377289
  • 224118
  • 178966
  • 276840
  • 430269
  • 431923
  • 431986
  • 547439
  • 512399
  • 234975
  • 512203
  • 602547
  • 443537
  • 376759
  • 284287
  • 608745

Column W - W1144-W1155

  • 233892
  • 367164
  • 368384
  • 344813
  • 233899
  • 233899
  • 233895
  • -
  • 233917
  • 284287
  • 376759
  • 443537

The conditional formatting formula I have is =VLOOKUP($W4,$Z4:$Z922,1,FALSE) 'Applies To' =$W$4:$W$3600

I am not sure what is causing this conditional formatting to fail here.

Ignore below - now working with conditional formatting instead of vba

I am trying to automate a manual process of cross referencing data and highlighting a cell if the contents are found anywhere in another column of data. However, the amount of data in both of these columns is not the same. And unfortunately, the column I need to loop through and check each cell often has either blank cells or cells that are dashed ("---").

I started with conditional formatting but it was not working properly so I am now on VBA.

Private Sub Workbook_Open()

Dim LastRow As Long
Range("W4").Select
LastRow = Range("W4").End(xlDown).Row
Do Until ActiveCell.Row = LastRow
   If Not IsEmpty(Application.Match(ActiveCell.Value, Range("Z:Z"), 0)) 
Then
   ActiveCell.Interior.Color = vbGreen
   ActiveCell.Offset(1, 0).Select
   End If
Loop


End Sub

Right now the code has a couple issues. It is not finding the last row correctly - when debugging it shows as 65, but should be 3,535 in the test case I'm using. Additionally, my match statement is not working, as it is highlighting every cell instead of only those whose content is found in column Z. And, it highlights up to row 410, which means my Do Until loop must be wrong as well.

After figuring out the one column I eventually need to allow checking columns AA and AB for content matches.

Thanks!

Miles
  • 1
  • 2
  • Any reason why not to use conditional formatting for this? – Pᴇʜ Jun 18 '19 at 13:47
  • I was using =match($W1,$Z:$Z,0) for the conditional formatting formula, but when testing I found plenty of matches in column Z that were not highlighted in column W. Is there something I could change with that formula to help with that? – Miles Jun 18 '19 at 13:50

2 Answers2

1

As mentioned in the comment, conditional formatting is the way to go. I just tried the following as a conditional format.

=VLOOKUP($A2,$C$2:$C$7,1,FALSE)

A column of numbers in A as the numbers to be cross-referenced, and a list in column C which are the numbers to be checked. It works perfectly.

Deepstop
  • 3,627
  • 2
  • 8
  • 21
  • Ah, I don't know how I hadn't thought of a vlookup, thanks. Modifying for my columns it is pretty much working correctly. However, some of the dashed cells in column W are being highlighted, which shouldn't be possible, as I did a search in column Z and found none. Some of those cells do have a dash in them, but with the FALSE in the vlookup I didn't think that would be an issue. Any idea what might be causing this? – Miles Jun 18 '19 at 19:48
  • What is the formula you are using in your conditional formatting? – Deepstop Jun 18 '19 at 19:53
  • Nevermind on that, my formula had gotten tweaked a little but is fine now – Miles Jun 18 '19 at 19:54
  • Okay I actually am still running into some issues. There are corresponding values in columns W and Z that still are not being highlighted in column W. For example, the number 233892 is located at W1144 and Z506 but is not being selected. The formula I have in my conditional formatting rule is =VLOOKUP($W4,$Z4:$Z922,1,FALSE) and applies to =$W$4:$W$3600. I am not sure what the deal is here – Miles Jun 18 '19 at 20:22
  • Look at the formula in W1144 and make sure it is =VLOOKUP($W1144,$Z4:$Z922,1,FALSE) – Deepstop Jun 18 '19 at 20:26
  • Okay if I'm checking that correctly (clicked on W1144->conditional formatting for current selection->double click on rule) then no, the formula still has $W4 – Miles Jun 18 '19 at 20:30
  • Sorry, that was a wild goose chase. It should be $W2. Very strange as it works fine here. Do you have a lot of matches, and just the occasional error, or is it the other way around? – Deepstop Jun 18 '19 at 20:39
  • There are definitely a lot of non-matches that should be matches. The last highlighted cell is W780. From 781-3600 there should be plenty more highlighted. Is there something that's causing the conditional formatting to stop looking? 'Stop if true' is not checked. I have also made sure that the cell format is Number for all cells used, so shouldn't be a type error – Miles Jun 18 '19 at 20:49
  • @Miles Actually it would be a great idea to provide a sample set of your data, so we can see what is highlighted wrong and right. That would make it much easiert and shorten the process a lot. You can edit your original question and put some well selected data (not all of it) that shows the actual issue. Reading [mcve] might also help here. – Pᴇʜ Jun 19 '19 at 05:59
  • Just submitted the edit - please let me know if that is not enough, thanks – Miles Jun 19 '19 at 13:11
  • The sample you provided shows a perfect match between the lists. I get the same result using the same formula on my computer. So I'm wondering what is different about the list you are using. I assume that (a) you are using the format painter to copy the conditional formats to all the cells, and (b) that you have checked that the formats on the list your looking up are the same for all cells. – Deepstop Jun 21 '19 at 00:10
0

I recommend to use Conditional Formatting. The following is just to explain what was wrong with your code:

  1. Always define which worksheet you mean and avoid using Select in Excel VBA.
  2. Using End(xlDown) will find the next free cell (not the last used). Instead use End(xlUp) from the very last cell of the worksheet.
  3. Application.Match does not return a cell but a row number. Therefore IsEmpty does not work.

In the end something like this should work:

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("SheetName")

Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "W").End(xlUp).Row

Dim MatchRow As Variant

Dim iRow As Long
For iRow = 4 To LastRow
    MatchRow = 0 'initialize
    MatchRow = Application.Match(ws.Cells(iRow, "W").Value, ws.Range("Z:Z"), 0)
    If MatchRow > 0 Then
        ws.Cells(iRow, "W").Interior.Color = vbGreen
    End If
Next iRow

Note that Contidional Formatting would be a much better solution.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • 1
    Thank you for your insight. I am still not too familiar with vba so this is very helpful. – Miles Jun 18 '19 at 19:48