I have this sheet where:
If the value in cell in column
I
is "Não marcou férias" then it should search the whole columnA
and find all times that the unique number for that specific person appears more than once.If the unique number appears twice or more and the column
I
of that row is not "Não marcou férias" then it should compare with current year and writeATUAL
in the corresponding cell in columnK
. If it's true then the row in columnK
that have "Não marcou férias" should be blank.
Here is my sheet:
And this is what I want to happen:
As you can see as I have two entries with number 59837
and one of them is "Não marcou férias", I want it to run through column A and find if exist another entry and as it is current, it should write "ATUAL" on it.
Here is my code:
Sub test()
Dim j As Long
Dim lastrow As Long
Dim ws1 As Worksheet
Dim date1 As Date, date2 As Date
Set ws1 = Sheets("Plan1")
lastrow = ws1.Range("A" & Rows.Count).End(xlUp).Row
For j = 2 To lastrow
date1 = ws1.Cells(j, 9)
date2 = ws1.Cells(j, 12)
If ws1.Cells(j, 9) = "Não marcou férias" Then
k = ws1.Cells(j, 1).Value
With ws1.Range("A2:A" & lastrow)
Set c = .Find(k, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
If ws1.Cells(j, 9) <> "Não marcou férias" Then
If Year(date1) = Year(Date) Or Year(date2) = Year(Date) Then
ws1.Cells(j, 13) = "ATUAL"
Else
ws1.Cells(j, 13) = ""
End If
End If
Set c = .FindNext(c)
If c Is Nothing Then
GoTo DoneFinding
End If
Loop While c.Address <> firstAddress
End If
DoneFinding:
End With
End If
Next j
End Sub
But when I try to run I get:
Run-time error 13: Type mismatch
And this line is highlighted:
date1 = ws1.Cells(j, 9)
My Excel is in portuguese so here we use date format like dd/mm/yyyy and all my columns are set up as dates correctly.
Is there anyone with suggestions? I don't know why I'm getting this type mismatch again since I've already applied THIS solution.
EDIT: I've tested the suggested solutions, but I still have the same problem in the same line.
Option Explicit
Sub test2()
Dim j As Long
Dim lastrow As Long
Dim ws1 As Worksheet
Dim date1 As Date, date2 As Date
Dim k As Long
Dim c As Range
Dim firstAddress As String
Set ws1 = Sheets("Plan1")
lastrow = ws1.Range("A" & Rows.Count).End(xlUp).Row
For j = 2 To lastrow
date1 = ws1.Cells(j, 9)
date2 = ws1.Cells(j, 12)
If Not IsDate(ws1.Cells(j, 9)) Then
k = ws1.Cells(j, 1).Value
With ws1.Range("A2:A" & lastrow)
Set c = .Find(k, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
If IsDate(ws1.Cells(j, 9)) Then
If Year(date1) = Year(Date) Or Year(date2) = Year(Date) Then
ws1.Cells(j, 13) = "ATUAL"
Else
ws1.Cells(j, 13) = ""
End If
End If
Set c = .FindNext(c)
If c Is Nothing Then
GoTo DoneFinding
End If
Loop While c.Address <> firstAddress
End If
DoneFinding:
End With
End If
Next j
End Sub
If ws1.Cells(j, 9) = "Não marcou férias" Then
:ws1.Cells(j, 9)
is not a date, sodate1 = ws1.Cells(j, 9)
is of coursetype mismatch
because ofDim date1 As Date
:) . I think you should first check withIf IsDate(ws1.Cells(j, 9) )
and take that into account. – A.S.H