VBA學習筆記之Union方法

2022-05-14 17:33:57 字數 1612 閱讀 6700

1 union([a1:a5], range("

a10")).select2 union(cells(1, 1), range("

a10")).select3

'union("a1", "a10").select union這樣寫區域是不行的

'經常利用變數與union進行單元格的連線

1

sub 連線符單元格連線()2

dim rng as range3

foreach rngs in [b2:b10]4 adss = rngs.address5 ads = ads & rngs.address & ","

6next

7 ad = left(ads, len(ads) - 1)8

end sub

1

sub union單元格連線()2

dim rng as range, rngs as range3

set rng = [b2]4

foreach rngs in [b2:b10]5 adss = rngs.address6

set rng = union(rng, rngs) '

關鍵union連線語句

7 ads = rng.address8

next

9end sub

1

sub 條件篩選自己寫()2

foreach rng in range("

b2if rng > 90

then

4 k = k + 1

5 range(cells(k + 1, 4), cells(k + 1, 5)) = union(rng, rng.offset(0, -1)).value '

關鍵的語句

6endif7

next

8end sub

效果圖:篩選出了成績大於九十的姓名和成績

給出教程裡的**:

1

sub 條件篩選()2

forif rng > 90

then

4 k = k + 1

5if k = 1

then

6set rn = rng7

else

8set rn = union(rn, rng) '

符合條件的rng就加入(union)到rn

9 aa = rn.address10

endif

11end

if12

next

13for

each rngs in rn '

對於rn裡的每一個元素進行操作得到結果

14 n = n + 1

15 cells(n + 1, "

d") = cells(rngs.row, "

a")16 cells(n + 1, "

e") = rngs17

next

18end sub