定义两个范围,然后join它们
我目前想知道如何selectVBA的范围,值的范围,并希望有人可以协助下面的一段代码。
eoline = 50 first = 5 For i = 0 To eoline multi = 2 * i + 1 a = Cells(first + 1, 1).Select s1 = Range(Selection, Selection.End(xlDown)).select %select 1st range of values b = Cells(first + 1, 2 * (i + 1)).Select s2 = Range(Selection, Selection.End(xlDown)).select %select 2nd range of values next i
其实我想select并复制S1和S2,我已经尝试失败
myval = Union(s1, s2).select selection.copy
谢谢
像这样的东西
- 创build范围时使用
Set
- 不要使用
Select
。 - 当设置范围看起来是自下而上,而不是select下来(如果有的话,它将停在一个数据差距)
码
Dim rng1 As Range Dim rng2 As Range Dim rng3 As Range Dim lngCnt As Long Dim lngCn2 As Long lngCnt = 5 lngCnt2 = 2 Set rng1 = Range(Cells(5, 1), Cells(Rows.Count, 1).End(xlUp)) Set rng1 = Range(Cells(lngCnt + 1, 1), Cells(Rows.Count, 1).End(xlUp)) Set rng2 = Range(Cells(lngCnt + 1, 2 * (lngCnt2 + 1)), Cells(Rows.Count, 2 * (lngCnt2 + 1)).End(xlUp)) Set rng3 = Union(rng1, rng2)
我find了解决scheme。 这是我的问题的完整解决scheme:
Dim s1, s2, myval As Range eoline = 50 first = 5 multi = 2 * i + 1 a = Cells(first + 1, 1).Select Set s1 = Range(Selection, Selection.End(xlDown)) b = Cells(first + 1, 2 * (i + 1)).Select Set s2 = Range(Selection, Selection.End(xlDown)) Set myval = Union(s1, s2) myval.Select