首页 > 解决方案 > 如何防止在 Do While 循环中合并的单元格将我选定的单元格从 VBA 中的一列移动到另一列?

问题描述

我从银行收到 excel 文件,其中包含我在特定列中需要的信息。所以我想遍历特定的列并获取这些单元格的值。假设我选择 B 列。我从 B1 开始并遍历列单元格。但是,一旦我到达一个合并的单元格,其中有很多,当我试图移过它时,合并的单元格会让我离开 B 列。我正在使用 Offset(1, 0) 向下列。

'Here is a quick example of how the selected cell will move
'I'm using an index to move down 15 cells
'Merge cell A2 and B2 before running the macro

Sub test()

Dim index As Integer
index = 0

Range("B1").Select

Do While index < 15

    Selection.Offset(1, 0).Select
    index = index + 1

Loop

End Sub

选择从 B1 移动到 B2,与 A2 合并,然后继续到 A3 而不是 B3。

标签: excelvba

解决方案



推荐阅读