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.
我是Stack Overflow和VBA的新手。我尝试编写一些小的VBA代码来选择包含特定数字的Excel中的所有行(从A到E)。
(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?
有人能帮助我吗?
1 个解决方案
#1
6
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()
.
您只需要在使用Union()添加ploeg2之前创建它。
#1
6
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()
.
您只需要在使用Union()添加ploeg2之前创建它。