我试图同时选择多个范围。 IE浏览器。 B2:C2,B12:C12,B14:C14。
更新了代码
Sub MarkForDMSLookup()
Dim MarkingString As String
Dim MarkingRange As String
j = 0
For i = 3 To 20 'LastRow()
If Cells(i, 5) = "Yes" Then
j = j + 1
MarkingRange = "B" & i & ":C" & i
'MsgBox MarkingRange
If j = 1 Then MarkingString = MarkingRange
If j > 1 Then MarkingString = MarkingString & ", " & MarkingRange
If Cells(i, 5) = "No" Then Cells(i, 5).EntireRow.Hidden = True
End If
Next i
'MsgBox MarkingString
Union(MarkingString).Select
End Sub
Union
代码为我提供了类型不匹配。任何有助于调整类型的帮助都将非常感激。
当将for-loop限制为2到20时,我可以使用Range
标记而不是Union
。
答案 0 :(得分:1)
我只使用自动过滤而不是循环:
Sub MarkForDMSLookup()
Dim j As Long
j = 0
With Range("E2:E" & LastRow())
.AutoFilter field:=1, Criteria1:="Yes"
On Error Resume Next
.Resize(.Rows.Count - 1).Offset(1, -3).Resize(, 2).SpecialCells(xlCellTypeVisible).Select
On Error GoTo 0
End With
End Sub
Function LastRow()
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
End Function
答案 1 :(得分:0)
Sub MarkForDMSLookup()
Dim MarkingString As String, _
MarkingRange As String, _
FirstRun As Boolean, _
RgMarking As Range, _
RgTotal As Range, _
CounT As Long, _
ToTaL As Long
FirstRun = True
CounT = 0
ToTaL = 0
Set RgTotal = Nothing
For i = 3 To LastRow
If Cells(i, 5) <> "Yes" Then
If Cells(i, 5) = "No" Then Cells(i, 5).EntireRow.Hidden = True
Else
MarkingRange = "B" & i & ":C" & i
CounT = CounT + 1
ToTaL = ToTaL + 1
If FirstRun Then
MarkingString = MarkingRange
FirstRun = False
Else
MarkingString = MarkingString & ", " & MarkingRange
End If
If CounT <> 20 Then
Else
On Error GoTo ErrHandler
Set RgMarking = Range(MarkingString)
Set RgTotal = Union(RgTotal, RgMarking)
On Error GoTo 0
MarkingString = vbNullString
FirstRun = True
CounT = 0
End If
End If
Next i
Set RgTotal = Union(RgTotal, RgMarking)
RgTotal.Select
Exit Sub
ErrHandler:
MsgBox Err.Description & vbCrLf & Err.Number
Resume Next
End Sub