Checking and Filling Excel Cells

Asked

Viewed 2,756 times

2

I have the following spreadsheet as image below, I would like to create a VBA to check if the content inserted in the range of cells of this spreadsheet exists in the list of the second image below, if it exists, fill in green the cell of the contents of the image 1.

NOTE: I did not use conditional formatting because the added numbers are always dragged to other cells in some document revisions.

Planilha onde verificar e preencher células

Lista onde vou verificar os nomes existentes

2 answers

1

Hello!

I think you don’t need VBA for this, you can use condicinal formatting as follows:

  1. Select all cells you want to color, go to conditional formatting and click New Rule

inserir a descrição da imagem aqui

  1. Select the last option Use smokes formula...., as shown below, insert the formula below and click on the button Format... and do the formatting as you wish

inserir a descrição da imagem aqui

Formula:

=SE(ÉERROS(PROCV(B5;$D$20:$D$22;1;FALSO));FALSO;VERDADEIRO)

[IMPORTANT] The part of the formula B5 will be your first selected cell, note that in my sample image is in column B on line 5. And remove the dollar sign $.

The part of the formula $D$20:$D$22 is where you have the verification data (your second image).

EDITION 1

Following image below is how to format to look the way you suggested in the question. The screen below will appear after clicking on Format..., click Fill and choose the fill color:

inserir a descrição da imagem aqui

I hope I’ve helped!

  • Look at the remark I added please. Thanks anyway

  • This conditional formatting will continue to work. And if you use Excel tables for the validation data (code in the table) you will get even better.

  • I select the cell and drag to another position.. Usually when I drag, the cell that is empty loses its properties

  • @Gustavodawson I tested here and does not lose when moving a cell with the mouse to another cell... I will try to make a gif and post in the answer.

  • Follow my model https://1drv.ms/x/s! Aiqrcjor1qmhmi4njhswugfqdkzjka please note that I am using Excel 2013, however I believe that until 2010 tb is the same way. Coming home I can test until 2007 and post comment.

  • do the following test and you will see the problem I find: Take this sheet that you sent me and drag one of the cells that contains "P9" for example, when dragging it remains "green", but the old cell where "P9" was lost property, if you write "P9" in the old cell again he does not paint green.

  • @Gustavodawson this will only happen if where you are dragging the cell does not have the same formatting. Do the same test there by dragging to a location where you have the same desired formatting and you will see that it works.

Show 2 more comments

1

Assuming the search data is in the C column of the Sheet(1) and You will search in the Sheet(2), this is code with the function . Find

Dim Rng As Range, rng2 As Range

ncell = Sheets(1).Cells(Sheets(1).Rows.Count, 3).End(xlUp).Row

    Set Rng = Sheets(2).Cells 'range para procurar
    Set rng2 = Rng(1, 1)

    For j = 1 To ncell
    pesquisar = Sheets(1).Cells(j, 3).Value 'referência de procura na coluna 3 (ou seja, C)
        With Rng
        Set cellFound = .Find(what:=pesquisar, After:=rng2, LookIn:=xlValues)
            If Not cellFound Is Nothing Then
                FirstAddress = cellFound.Address
                Do
                    Sheets(2).Range(cellFound.Address).Interior.ColorIndex = 4
                    Set cellFound = .FindNext(cellFound)
                Loop While Not cellFound Is Nothing And cellFound.Address <> FirstAddress
             End If
        End With
    Next

End Sub

Another alternative is the use of an Object Dictionary, this is faster and optimizes the processing time. Recommended for spreadsheets with lots of data.

  • I’m going to test it. I added a remark to my question. Thank you so much

  • This code paints green only if you change it. You need to change the code to check this. How is the change performed? Manually?

  • Yes, it is done manually, selects the cell and drags it to another position.. Usually when I drag, the cell that is empty loses its properties

  • Well, you can switch to a logic of the type: on changes in the used spreadsheet (Worksheet_Change), the selected cell has a white background: Range.Interior.ColorIndex = 2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.