我有这张表:
如果I
列中的单元格中的值为“Nãomarfoférias”,那么它应搜索整个列A
并查找该特定人的唯一编号出现的所有时间不止一次。
如果唯一编号出现两次或更多且该行的列I
不是“Nãarkoufoférias”那么它应该与当前年份进行比较并在相应的单元格中写入ATUAL
在K
列中。如果确实如此,则K
列中包含“Nãomarfoférias”的行应为空白。
这是我的表格:
这就是我想要发生的事情:
正如你所看到的,因为我有两个数字为59837
的条目,其中一个是“Nãomarfourérias”,我希望它通过A列运行并查找是否存在另一个条目,因为它是最新的,它应该写上“ATUAL”。
这是我的代码:
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
但是当我尝试跑步时,我得到了:
运行时错误13:输入不匹配
这一行突出显示:
date1 = ws1.Cells(j, 9)
我的Excel是葡萄牙语,所以我们在这里使用日期格式,如dd / mm / yyyy,我的所有列都正确设置为日期。
有人有建议吗?我不知道为什么我再次遇到此类型不匹配,因为我已经应用了THIS解决方案。
编辑:我已经测试了建议的解决方案,但我仍然在同一行中遇到同样的问题。
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
答案 0 :(得分:1)
Option Explicit
。msgbox ws1.Cells(j, 9).address
即可。 I6
,说假期和葡萄牙语预订。 在您的代码中,您可以添加If IsDate(ws1.cells(j,9)) then
之类的内容。它会检查给定值是否为日期。在您的情况下,Não marcou férias
无法解析为Date
格式,因此您会收到错误消息。或者只是尝试替换代码位置,如下所示:
For j = 2 To lastrow
If ws1.Cells(j, 9) <> "Não marcou férias" Then
date1 = ws1.Cells(j, 9)
date2 = ws1.Cells(j, 12)
k = ws1.Cells(j, 1).Value
With ws1.Range("A2:A" & lastrow)
如果价值不是&#34; Nao marcou ferias&#34;那么它可能是一个约会(尽管最好使用IsDate()
)。
玩得开心,TGIF