我有這樣的表,其中:擅長:使用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