[英]vlookup vba stops when find the first value from multiple worksheet how to make it continue works and find the next value in same cell
我有 vba 代码用于在整个工作表中查找员工编号并返回他的总工作日,
但员工编号可以在不同工作表中的 2 个不同总天数的地方工作
因此,当它找到员工编号时,它会从工作表中返回第一个总天数并停止它不会继续查找其他总天数
下面是 VBA 代码
Function VLOOKUPWORKBOOK( _
lookup_value As Variant, _
table_array As Range, _
col_index_num As Integer, _
Optional range_lookup As Boolean, _
Optional sheets_to_exclude_1 As String, _
Optional sheets_to_exclude_2 As String, _
Optional sheets_to_exclude_3 As String, _
Optional sheets_to_exclude_4 As String, _
Optional sheets_to_exclude_5 As String _
)
Dim mySheet As Worksheet
Dim value_to_return As Variant
Dim sheets_to_exclude As Variant
Dim CellsEmpty As Boolean
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
On Error Resume Next
sheets_to_exclude = Array(sheets_to_exclude_1, sheets_to_exclude_2, sheets_to_exclude_3, sheets_to_exclude_4, sheets_to_exclude_5)
For Each mySheet In ActiveWorkbook.Worksheets
If Not (UBound(Filter(sheets_to_exclude, mySheet.Name)) > -1) Then
With mySheet
Set table_array = .Range(table_array.Address)
value_to_return = Application.VLookup(lookup_value, table_array, col_index_num, range_lookup)
End With
If Not IsEmpty(value_to_return) Then
Exit For
End If
End If
Next mySheet
VLOOKUPWORKBOOK = value_to_return
End Function
Exit For 是为什么你只得到一个结果。 您的循环在找到一个值后退出。 移除 Exit For 并将 value_to_return 更改为可以返回多个值的数组。
Function VLOOKUPWORKBOOK( _
lookup_value As Variant, _
table_array As Range, _
col_index_num As Integer, _
Optional range_lookup As Boolean, _
Optional sheets_to_exclude_1 As String, _
Optional sheets_to_exclude_2 As String, _
Optional sheets_to_exclude_3 As String, _
Optional sheets_to_exclude_4 As String, _
Optional sheets_to_exclude_5 As String _
)
Dim mySheet As Worksheet
Dim value_to_return() As Variant
Dim ValCount As Long
Dim sheets_to_exclude As Variant
Dim CellsEmpty As Boolean
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
On Error Resume Next
sheets_to_exclude = Array(sheets_to_exclude_1, sheets_to_exclude_2, sheets_to_exclude_3, sheets_to_exclude_4, sheets_to_exclude_5)
For Each mySheet In ActiveWorkbook.Worksheets
If Not (UBound(Filter(sheets_to_exclude, mySheet.Name)) > -1) Then
With mySheet
Set table_array = .Range(table_array.Address)
ReDim Preserve value_to_return(ValCount)
value_to_return(ValCount) = Application.VLookup(lookup_value, table_array, col_index_num, range_lookup)
ValCount = ValCount + 1
End With
End If
Next mySheet
VLOOKUPWORKBOOK = value_to_return
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.