VBA:合并对于偏移范围的行为如何

问题描述 投票:0回答:1

以下代码中的问题是,被合并的

range
始终是
Range("A1:B2")

'how the range are set (they're dim'ed as Range)

Set Range1 = Sheet1.Range("A1:B2")
Set Range2 = Sheet2.Range("A1:B2")

For i = 1 To col1.Count 'collection with data which will be used further in the code
    If i <> 1 Then 'checking that we're not at the first item
        Sheet1.Rows(range_row + 2 & ":" & range_row + 3).Insert 'range row = 1 at start
        Sheet2.Rows(range_row + 2 & ":" & range_row + 3).Insert
        Sheet1.Rows("61:62").Delete
        Sheet2.Rows("61:62").Delete
        Set Range1 = Range1.Offset(1, 0) 'I've tried with Offset 2 it changes nothing
        Set Range2 = Range1.Offset(1, 0)
        Range1.Merge
        Range2.Merge
        range_row = range_row + 2
    End If
    'other operations not affecting the size of the range
Next i

目标是合并

Range("A1:B2")
然后
Range("A3:B4")
然后
Range("A5:B6")
依此类推...

我有什么:

目标

Offset
方法应将范围向下移动,以便通过
.Merge
方法可以合并 2 行。显然,经过一些调试后,问题似乎是
Range("A1:B2")
总是被合并。但在我的代码中,我在此范围中存储一个值,并将其放入右侧的单元格中......

Merge
不喜欢偏移范围吗?我应该为我的两个范围做不同的声明吗?
请注意,每个变量都已变暗并具有正确的值,期望范围。

excel vba loops range
1个回答
0
投票

As New

© www.soinside.com 2019 - 2024. All rights reserved.