选择excel中包含特定列中相同值的所有行

ste*_*fvr 4 excel vba excel-vba

我是Stack Overflow和VBA的新手.我尝试编写一些小的VBA代码来选择包含特定数字的Excel中的所有行(从A到E).

(部分)我的代码到目前为止

Dim ploeg as range
Dim ploeg2 as range

For v = 1 To 100
If Cells(v, 6) = 1 Then 
Set ploeg = Range(Cells(v, 1), Cells(v, 5))
Set ploeg2 = Union(ploeg2, ploeg)
End if
Next v

Ploeg2.Select
Run Code Online (Sandbox Code Playgroud)

但这不起作用......

有人能帮助我吗?

Gar*_*ent 7

你非常接近:

Sub dural()
    Dim ploeg As Range
    Dim ploeg2 As Range

    For v = 1 To 100
        If Cells(v, 6) = 1 Then
            Set ploeg = Range(Cells(v, 1), Cells(v, 5))
            If ploeg2 Is Nothing Then
                Set ploeg2 = ploeg
            Else
                Set ploeg2 = Union(ploeg2, ploeg)
            End If
        End If
    Next v
    ploeg2.Select
End Sub
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

您只需要ploeg2在添加之前创建Union().