gpt4 book ai didi

vba - Excel VBA 中的 for 循环

转载 作者:行者123 更新时间:2023-12-03 02:54:07 24 4
gpt4 key购买 nike

我正在尝试在 Excel 列上使用 For 循环。这是我的代码:

   For Each c In Worksheets("sheet1").Range("A1:A5000").Cells
c.Offset(0, 1).Range("A1").Value = Right((Left(c, 13)), 7)
Next

工作正常,问题出在

Range("A1:A5000")

我的工作表少于 1000 行,但它可以增长,并且我希望能够仅在包含数据的行上使用循环。如何将其更改为从 A1 到最后一个非空行?

最佳答案

Dim RowIndex As Long
RowIndex = 1

Dim c

While Not IsEmpty(Worksheets("sheet1").Cells(RowIndex, 1))
Set c = Worksheets("sheet1").Cells(RowIndex, 1)
c.Offset(0, 1).Range("A1").Value = Right((Left(c, 13)), 7)
RowIndex = RowIndex + 1
Wend

关于vba - Excel VBA 中的 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17896655/

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