Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select all rows in excel containing same value in certain column

Tags:

excel

vba

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?

like image 657
stefvr Avatar asked Mar 05 '23 11:03

stefvr


1 Answers

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

enter image description here

You just need to create ploeg2 before adding to it with Union().

like image 138
Gary's Student Avatar answered May 06 '23 06:05

Gary's Student