刷新仪表盘的一部分包括筛选ODBC查询表以仅显示某些项目,然后将这些项目ID复制到同一工作簿中的另一个选项卡。因为项目的顺序和过滤的结果随着查询的每次刷新而变化,所以我很难定义要复制的项目ID的范围。
我尝试了下面的代码,该代码导致只复制查询表中的第一个值,并将其复制到显示表的每一行中。我还尝试通过单元格引用而不是表引用来引用范围,这导致了相同的结果。
Public Sub EPC_test()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = wb.Sheets("projects_master")
'filter query table to show qualifying projects
Set ws = wb.Sheets("projects_master")
Dim i As Integer, lo As ListObject
Set lo = wb.Sheets("projects_master").ListObjects(1) 'lo = query table
i = Application.WorksheetFunction.Match("Display Tab?", wb.Sheets("projects_master").Range("A1:ZZ1"), 0)
lo_PM.Range.AutoFilter Field:=i, Criteria1:="1"
'update range with qualifying project IDs
Set ws_m = wb.Sheets("projects_master")
Set ws_BP = wb.Sheets("Display")
Set range1 = ws_m.Range("projects_master[ID]").Rows.SpecialCells(xlCellTypeVisible)
Set range2 = ws_BP.Range("Display[ID]")
'copy values from query to display
range2.Value = range1.Value