在 Excel 2003 中创建要循环的可变大小的组的好方法是什么?

发布于 2024-07-10 02:39:09 字数 539 浏览 7 评论 0原文

我有一个针对很多项目运行的程序,跳过某些不符合标准的项目。 然而,然后我会返回并为一些在第一次传递中错过的人运行它。 目前,我通过为每个人手动重新运行该过程来实现此目的,但理想情况下希望有一个更方便的解决方案。

我老板建议的可能有效的方法是创建一个列表(如数据 -> 列表),其中包含相关项目的名称,然后迭代该列表。 可悲的是,我的帮助文件 fu 似乎让我失望了 - 我不知道我是否只是不知道要寻找什么,或者什么。

运行“生成宏”命令显示,首先创建列表的 VBA 的结构如下: ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1"), , xlYes).Name = "List1"

不幸的是,我似乎不知道如何处理结果列表。 我希望按照“

For Each ListItem in List 
    Run the procedure on the text in ListItem.Value
Next ListItem

有什么建议吗?”的方式进行循环?

I have a procedure that is run for a lot of items, skipping over certain items who don't meet a criterion. However, I then go back and run it for some of the individuals who were missed in the first pass. I currently do this by manually re-running the procedure for each individual person, but would ideally like a solution a little more hands off.

Something my boss suggested might be effective would be creating a List (as in Data -> Lists) that contains the names of the items in question, and then iterating over the list. Sadly, my help-file fu seems to be failing me - I don't know whether I just don't know what to look for, or what.

Running the "Generate Macro" command shows that the VBA to create a list in the first place is along the lines of
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1"), , xlYes).Name = "List1"

Unfortunately, I can't seem to figure out how to then do stuff with the resulting list. I'm looking to making a loop along the lines of

For Each ListItem in List 
    Run the procedure on the text in ListItem.Value
Next ListItem

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七堇年 2024-07-17 02:39:09

也许是这样的:

Dim Counter 'module level '

Sub RunSomeProc()
    Counter = 0
    '1st test '
    SomeProc

    '2nd Test skipped items'
    For Each c In Range("c1:c" & Counter)
        SomeProc
    Next

End Sub

Sub SomeProc()
For Each c In Range("NamedRange1")
    If SomeTest=SomeVal Then
        'Write to 2nd test range '
        Range("C1").Offset(Counter, 0) = c 'Value of cell'
        Counter = Counter + 1
    End If
Next
End Sub

Perhaps something on these lines:

Dim Counter 'module level '

Sub RunSomeProc()
    Counter = 0
    '1st test '
    SomeProc

    '2nd Test skipped items'
    For Each c In Range("c1:c" & Counter)
        SomeProc
    Next

End Sub

Sub SomeProc()
For Each c In Range("NamedRange1")
    If SomeTest=SomeVal Then
        'Write to 2nd test range '
        Range("C1").Offset(Counter, 0) = c 'Value of cell'
        Counter = Counter + 1
    End If
Next
End Sub
如果没有 2024-07-17 02:39:09

您可以通过以下方式迭代它:

set rgList = Range("name_of_range")    
For i = 1 To rgList.Rows.Count
  ' Do something using rgList.Cells(i, 1)
  RunProcedure(rgList.Cells(i, 1))
Next i

我在这里假设范围位于列上; 如果它是连续的,您应该对第二个索引进行迭代。
当然,可能有更好的方法来迭代范围; 我在一个小脚本上使用这个,并且工作得很好。

You can iterate on it in this way:

set rgList = Range("name_of_range")    
For i = 1 To rgList.Rows.Count
  ' Do something using rgList.Cells(i, 1)
  RunProcedure(rgList.Cells(i, 1))
Next i

I assumed here that the range is on a column; was it on a row, you should have done the iteration on the second index.
Of course, there may be better ways to iterate on a range; I am using this one on a small script, and is working quite fine.

梦年海沫深 2024-07-17 02:39:09

我最终的解决方案是将列表作为外部数据查询导入,然后我很好地命名并作为范围引用。 所以:

For each item in Sheets("Sheet1").Range("Range1")
    Do stuff
Next item

My eventual solution was to import the list as an external data query that I then named nicely and referenced as a range. So:

For each item in Sheets("Sheet1").Range("Range1")
    Do stuff
Next item
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文