|
本帖最后由 Excelhome112233 于 2020-8-13 17:29 编辑
VSTO数组中Contains属性用法,两个单列对比源码分享!!
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
' 1、数组Contains属性,用于判断数组是否包含某个值,如果是返回是True
' 2、数组Contains属性只支持一维数组
' 3、所有App.range.value=Arr全部是二维数组,用到Contains则要把二维数组转换成一维数组
Dim Aarr() As Object, Barr() As Object
Dim Carr() As Object, Darr() As Object, Earr() As Object, Farr() As Object
Dim Arr1 As Array, Arr2 As Array
Dim A As Int64, B As Int64, ai As Int64, bi As Int64
Dim i As Int64, j As Int64
Arr1 = app.Range("A1:A10").Value
Arr2 = app.Range("B1:B5").Value
Arr1 = Arr1.Cast(Of Object).ToArray '把Arr1转成一维数组
Arr2 = Arr2.Cast(Of Object).ToArray '把Arr2转成一维数组
Aarr = Arr1
Barr = Arr2
REM ==================判断第一列数字=====================================================
For i = 0 To UBound(Aarr, 1)
If Barr.Contains(Arr1(i)) = True Then
ai = ai + 1
ReDim Preserve Carr(0 To ai - 1)
Carr(ai - 1) = Aarr(i)
Else
bi = bi + 1
ReDim Preserve Darr(0 To bi - 1)
Darr(bi - 1) = Aarr(i)
End If
Next
REM ==================判断第一列数字=====================================================
REM ==================判断第二数字=======================================================
For j = 0 To UBound(Barr, 1)
If Aarr.Contains(Barr(j)) = True Then
A = A + 1
ReDim Preserve Earr(0 To A - 1)
Earr(A - 1) = Barr(j)
Else
B = B + 1
ReDim Preserve Farr(0 To B - 1)
Farr(B - 1) = Barr(j)
End If
Next
REM ==================判断第二数字=======================================================
app.ScreenUpdating = False
app.Range("c1").Resize(bi, 1).Value = "第二列中无"
app.Range("c1").Offset(0, 1).Resize(bi, 1).Value = app.WorksheetFunction.Transpose(Darr)
app.Range("c1").Offset(bi, 0).Resize(ai, 1).Value = "第二列中有"
app.Range("c1").Offset(bi, 1).Resize(ai, 1).Value = app.WorksheetFunction.Transpose(Carr)
app.Range("c1").Offset(0, 3).Resize(B, 1).Value = "第一列中无"
app.Range("c1").Offset(0, 4).Resize(B, 1).Value = app.WorksheetFunction.Transpose(Farr)
app.Range("c1").Offset(B, 3).Resize(A, 1).Value = "第一列中有"
app.Range("c1").Offset(B, 4).Resize(A, 1).Value = app.WorksheetFunction.Transpose(Earr)
app.Range("c1").Offset(0, 2).EntireColumn.ColumnWidth = 1
app.Range("c1").EntireColumn.AutoFit()
app.Range("c1").Offset(0, 1).EntireColumn.AutoFit()
app.Range("c1").Offset(0, 3).EntireColumn.AutoFit()
app.Range("c1").Offset(0, 4).EntireColumn.AutoFit()
app.ScreenUpdating = True
End Sub
|
|