我有这样的表,其中:擅长:使用VBA类型不匹配找到
如果
I
列单元格的值是“孬marcouFérias酒店”,那么它应该搜索整个列A
并找到所有倍该特定人员的唯一号码不止一次出现。如果出现独特数量的两倍以上,该行的列
I
不是“NAO marcouFérias酒店”,那么它应该与当年相比并在相应的单元格中K
列写ATUAL
。如果这是真的,那么具有“Nãomarcouférias”的列K
中的行应该是空白的。
这里是我的表:
而这正是我希望发生的:
正如你所看到的,因为我有两个条目与数59837
,其中之一是“Nãomarcouférias”,我希望它能够贯穿A列并查找是否存在其他条目,因为它是curr它应该在其上写上“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
'如果ws1.Cells(J,9)= “NAO marcouFérias酒店” Then':'ws1.Cells(J,9)'是不是一个日期,所以'DATE1 = WS1 .Cells(j,9)'当然是'类型不匹配',因为'Dim date1 As Date' :)。我想你应该首先检查'If IsDate(ws1.Cells(j,9))'并考虑到这一点。 –
@ A.S.H我真的明白了,尽管我需要在该专栏中做这个比较。例如,我尝试了Vityata的解决方案,并且消息框指定I6作为日期。我可以尝试改变我的比较。 – paulinhax