Search code examples
excelvbaexcel-2010

If Columns C cells have value, then write "OK" in Column C Sheet 2


I want to add text to Column C Sheet 2 with "OK", IF there is a value in Column C Sheet 1.

Here is an example of the data on Sheet 1:

NIP Name Graduated
8593 Ariana 04/12/2023
8594 Mike
8595 Harry 08/20/2023

Data on Sheet 2:

NIP Name Recommendation
8593 Ariana
8595 Harry
8596 John

There should be an index match function to column A on both sheets, because the sheets don't contain the same lists. And I wish to make it dynamic range.

Here is my current code but it gave runtime type mismatch

Dim lw As Long
lw = sws.Cells(sws.Rows.Count, "A").End(xlUp).Row

Dim p As Long
p = sws.Range("C" & Rows.Count).End(xlDown).Rows
Dim u As Long

For u = 2 To lw
    If p <> 0 Then
        dws.Cells(i, "C").Value = "OK"
    End If
Next u

Solution

  • Try this:

    Base sheet is Sheet1 Recom sheet is Sheet2

    Sub acar()
     Set sws = Worksheets("Base")
     Set dws = Worksheets("recom")
     
     Dim lw As Long, p As Long, u As Long
        lw = sws.Cells(sws.rows.Count, "A").End(xlUp).Row
        
        p = dws.Range("A" & rows.Count).End(xlUp).Row
        
        For u = 2 To p
            Set v = sws.Range("A2:A" & lw).Find(dws.Range("A" & u), , xlFormulas, xlWhole)
            If Not v Is Nothing Then
                If sws.Range("C" & v.Row) <> "" Then
                  dws.Cells(u, "C").Value = "OK"
                Else
                  dws.Cells(u, "C").Value = ""
                End If
            End If
        Next u
    End Sub
    

    enter image description here