简体   繁体   English

通过ActiveCell嵌入式值的VBA宏循环

[英]VBA Macro Loop through ActiveCell Embedded Values

I am trying to find a better solution to more dynamically loop through a cell with a Dynamic list of values from a Database Connection. 我试图找到一个更好的解决方案,以动态地遍历具有来自数据库连接的值的动态列表的单元格。 The cell "B3" has a drop-down list of the items I want to loop through, and when a particular item is selected it refreshes the worksheet to reflect that items values (Select * from Table where Item = X). 单元格“ B3”具有要循环浏览的项目的下拉列表,并且当选择了特定项目时,它将刷新工作表以反映该项目的值(从表中选择*,其中Item = X)。 The number of items changes per quarter, as the items are the list of properties we appraise. 项目数量每季度更改一次,因为项目是我们评估的属性列表。 Sometimes there are 50, sometimes 5 items. 有时有50个项目,有时有5个项目。 How do I modify this code so I do not have to go into the Macro and update the For i = 1 to 49 loop? 如何修改此代码,这样就不必进入宏并更新For i = 1至49循环? I want is so it just can either query the item list and find the number of items or does a Do While loop and loops through all the values until the last one. 我想要的是,它既可以查询项目列表并找到项目数,也可以执行Do While循环并遍历所有值直到最后一个。

Dim strfindrow As String
For i = 1 To 49
    Range("B3").Select
    strfindrow = "=DATA_PROPERTY!R[" + Trim(Str(i - 2)) + "]C[-1]"
    ActiveCell.FormulaR1C1 = strfindrow
    Range("B3").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1
Next
End Sub

To find last row, also at this link: Error in finding last used cell in VBA 要查找最后一行,也在此链接上: 在VBA中查找最后使用的单元格时出错

With ThisWorkbook.Worksheets("sheet name")
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' if col is "A"
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM