I'm new to Stack Overflow and VBA. I try to write some little VBA-code to select all rows (from A to E) in Excel containing a certain number.
(Parts of) My code as far
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
But this doesn't work...
Can somebody help me?
You were very close:
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

You just need to create ploeg2 before adding to it with Union().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With