排除不同工作表中的列值
问题描述:
我一直在寻找解决方案。虽然我得到一个相当晦涩的'类型不匹配'错误,我不知道它来自哪里。排除不同工作表中的列值
所以我的目标是创建一个通用函数,它从不同的工作表取两列并验证一列中的值是否不显示在第二列中。
Public Function ExcludeColumns(needle_sheet As Worksheet, needle_column As Integer, haystack_sheet As Worksheet, haystack_column As Integer) As Boolean
Dim var As Variant
' Loop column in needle sheet
For Each rw In needle_sheet.Rows
If needle_sheet.Cells(rw.Row, needle_column).Value = "" Then
Exit For
End If
var = Application.Match(Cells(rw.Row, needle_column).Value, Worksheets(haystack_sheet).Columns(haystack_column), 0)
If Not IsError(var) Then
MsgBox ("Value found")
Else:
MsgBox ("Value not found")
End If
Next rw
End Function
答
与Application.Match
行包含两个错误:
- 第一个参数,你必须给其中的细胞来源于信息。在
Cells
之前添加needle_sheet
,否则Excel将从activesheet
- 中获取对于第二参数
haystack_sheet
已经是工作表了。
此外,你必须限制你的循环(例如到UsedRange
),否则它将循环,直到纸张的最末端。
试试这个:
Public Function ExcludeColumns(needle_sheet As Worksheet, needle_column As Integer, haystack_sheet As Worksheet, haystack_column As Integer) As Boolean
Dim var As Variant, rw as Range
' Loop column in needle sheet
For Each rw In needle_sheet.UsedRange.Rows
DoEvents
If needle_sheet.Cells(rw.Row, needle_column).Value = "" Then
Exit For
End If
var = Application.Match(needle_sheet.Cells(rw.Row, needle_column).Value, haystack_sheet.Columns(haystack_column), 0)
If Not IsError(var) Then
MsgBox ("Value found")
Else:
MsgBox ("Value not found")
End If
Next rw
End Function
+0
噢,我想我一直在盯着相同的代码太长时间了...谢谢你非常多 – Iso
上哪一行,你得到类型不匹配的错误? – Luuklag