有人可以解释这里发生了什么吗?最好是直线。我很难绕过这一点发生的事情。
a = Application.Transpose(a)
For i = 1 To UBound(a, 2)
If UCase(a(1, i)) Like "*" & temp & "*" Or _
UCase(a(2, i)) Like "*" & temp & "*" Then
n = n + 1
For ii = 1 To UBound(a, 1)
a(ii, n) = a(ii, i)
Next
End If
Next
我也遇到上述“类型不匹配”错误。见下面的完整Sub。
Private Sub TextBox_Search_Change()
Select Case True
Case OptionButton_User_Name.Value
Dim a, i As Long, ii As Long, n As Long, temp As String
If Len(Me.TextBox_Search.Value) Then
temp = UCase(Me.TextBox_Search.Value)
With Sheets("ToolData")
a = Union(.Range("B:B"), .Range("F:F"), .Range("G:G")).Value
End With
a = Application.Transpose(a)
For i = 1 To UBound(a, 2)
If UCase(a(1, i)) Like "*" & temp & "*" Or _
UCase(a(2, i)) Like "*" & temp & "*" Then
n = n + 1
For ii = 1 To UBound(a, 1)
a(ii, n) = a(ii, i)
Next
End If
Next
If n > 0 Then
ReDim Preserve a(1 To UBound(a, 1), 1 To n)
Me.ListBox_History.Column = a
End If
Else
With Sheets("ToolData")
Me.ListBox_History.List = Union(.Range("B:B"), .Range("F:F"), .Range("G:G")).Value
End With
End If
Case Else
End Select
答案 0 :(得分:3)
您不能在多区域范围内使用.Value
:您只能从第一列(B)获取值。
答案 1 :(得分:1)
我会将True
语句的If
部分重构为
temp = UCase(Me.TextBox_Search.Value)
Dim rngValues As Variant
With Sheets("ToolData")
rngValues = .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).Resize(, 6).Value
End With
For i = 1 To UBound(rngValues, 1)
'Check columns B & F for matching values
If UCase(rngValues(i, 1)) Like "*" & temp & "*" Or _
UCase(rngValues(i, 5)) Like "*" & temp & "*" Then
'Store columns B, F & G for displaying in the ListBox
n = n + 1
ReDim Preserve a(1 To 3, 1 To n)
a(1, n) = rngValues(i, 1)
a(2, n) = rngValues(i, 5)
a(3, n) = rngValues(i, 6)
End If
Next
'If anything found, replace the ListBox contents. Otherwise leave it as it was.
If n > 0 Then
Me.ListBox_History.Column = a
End If
因此摆脱了将所有1048576行表单读入内存的代码,以及需要Transpose
(它不会对大量数据进行处理)。
您还需要以类似的方式更改False
的{{1}}段。