gpt4 book ai didi

excel - UsedRange 循环比预期大

转载 作者:行者123 更新时间:2023-12-04 21:11:16 25 4
gpt4 key购买 nike

我有一个 22 行的表,我只想通过 B 列。由于某种原因,我的循环超出了 22 行并达到了 259。

代码

Debug.Print "Starting..."
Dim tempRange As Range
Set tempRange = Sheet18.UsedRange.Columns("B")

Dim i As Integer
i = 0
For Each Cell In tempRange.Cells
i = i + 1
Next Cell
Debug.Print "Count: " & i

中间窗口
Starting...
Count: 259

显示有 22 行的 Sheet18 的屏幕截图

Table with 22 rows

显示 22 行的表引用的屏幕截图

Table reference showing 22 rows

最佳答案

UsedRange有问题,如 here 所述.

既然你有一张 table ,或者ListObject ,只需遍历 ListColumn 中的单元格.

Dim myTbl as ListObject
Set myTbl = Sheet18.ListObjects("Table24")

Dim rng as Range
For Each rng in myTbl.ListColumns(2).DataBodyRange
...
Next rng

关于excel - UsedRange 循环比预期大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59178203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com