2017-07-07 35 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