使用宏进行多个范围选择

时间:2016-09-29 06:56:03

标签: excel vba excel-vba

可以使范围与" OR"相同条件... 有什么想法可以解决吗?

Sub search()

Dim myArray As Variant
Dim mRange As Range
Dim mFCell As String
Dim mCell As Range
Dim mName As String

Dim i As Integer
Dim finalrow As Integer

myArray = Range("J2:J4").Value

mName = myArray.Value

finalrow = Sheets("Sheet1").Range("A10000").End(xlUp).Row

Set mRange = Sheets("Sheet1").Range("A:A")
Set mCell = mRange.Find(What:=mName, MatchCase:=False, lookAt:=xlPart)

For i = 2 To finalrow

           If Sheets("Sheet1").Cells(i, 1) = mCell.Value Then
               mFCell = mCell.Address
               Range(Cells(i, 1), Cells(i, 1)).Copy
               Range("I1000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
               'Sheets("Sheet2").Range("B1000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
               Set mCell = mRange.FindNext(mCell)

           End If
Next i

End Sub

所以,我需要找到在评论中解释的multipleRange。

1 个答案:

答案 0 :(得分:0)

要将范围的值分配给数组,您需要将数组声明为变量

Dim myArray As Variant
myArray = someRange.Value

结果是一个维度数组(1到行数)x(1到列数),即使范围是一维的(只有一行或一列)。所以

的值
myArray = Range("J2:J4").Value

将是myArray(1, 1)myArray(2, 1)myArray(3, 1)