2017-07-07 57 views
1

我有這樣的表,其中:擅長:使用VBA類型不匹配找到

  • 如果I列單元格的值是「孬marcouFérias酒店」,那麼它應該搜索整個列A並找到所有倍該特定人員的唯一號碼不止一次出現。

  • 如果出現獨特數量的兩倍以上,該行的列I不是「NAO marcouFérias酒店」,那麼它應該與當年相比並在相應的單元格中K列寫ATUAL。如果這是真的,那麼具有「Nãomarcouférias」的列K中的行應該是空白的。

這裏是我的表:

enter image description here

而這正是我希望發生的:

enter image description here

正如你所看到的,因爲我有兩個條目與數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 
+3

'如果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))'並考慮到這一點。 –

+0

@ A.S.H我真的明白了,儘管我需要在該專欄中做這個比較。例如,我嘗試了Vityata的解決方案,並且消息框指定I6作爲日期。我可以嘗試改變我的比較。 – paulinhax

回答

1
  • 始終確保您包括Option Explicit您的代碼的頂部。
  • 只需在錯誤行上方的行上寫上msgbox ws1.Cells(j, 9).address即可。
  • 檢查出錯前的MsgBox。
  • 我可以打一個虛擬啤酒,地址指向的是一個值,這不是日期。例如在您的屏幕截圖中輸入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) 

如果該值不是「孬marcouFérias酒店」,那麼它可能是一個日期(儘管,它能夠更好地使用IsDate())。

玩得開心,TGIF

+0

讓我們來看看這啤酒是否會發生!無論如何,我已經測試了兩件事:當我不使用'Option Explicit'時,代碼運行良好,並且消息框指定爲'I6'也作爲日期。然後它再次得到類型不匹配。然後,當我用'Option Explicit'嘗試時,我得到'k = ws1.Cells(j,1).Value'突出顯示,變量未定義。 – paulinhax

+0

'Option Explicit'的想法是告訴你,你忘記了在頂部寫'Dim k as Something'。在你的屏幕截圖中'I6'不是日期格式。你說MessageBox指定'I6'作爲日期又是什麼意思? – Vityata

+0

是的,是的,我現在宣佈我所有的變量。我試着說的是,'I6'不是日期,但整個列被格式化爲日期,即使其上有文本。所以我認爲它會以同樣的方式工作。 – paulinhax