0

我正在尝试将关闭的工作簿中每个工作表中的可变值范围加载到格式为“worksheet.name:[range]”的字典中,以便不必打开所述工作簿并遍历工作表然后遍历指定单元格范围内的单元格(“for each”循环是无止境的!)。我发现很多人推荐一个流行的 Excel4Macro,叫做 GetValues,但是据我所知,这个宏只能获取或返回一个单元格地址,而我需要返回一个变量范围,即 C5:L12 或 C5: K12。我一直在处理 ADOX 和集合,但在那里我没有取得太大进展(知道如何在 Catalog.Table 对象中返回“单元格”条目吗?Access/SQL 键和字段对我来说是陌生的)。有没有人尝试过这样的事情?任何指示或建议?谢谢你们。

这是我目前正在使用的代码。我想在不打开从中提取值的工作簿的情况下完成它。我怎样才能做到这一点?

Set WB = Workbooks.Open(c:\work\book\path.xlsm)
'loop through the worksheets in WB and create a master dictionary with key:value ws.name:[wsvalues]
Set wsdict = CreateObject("Scripting.Dictionary")
For Each ws In WB.Worksheets
    Set wsccoll = New Collection
    With ws
        maxcols = .Cells(5, .Columns.Count).End(xlToLeft).Column    'get the last column in the search area
        basewsc = .Range(.Cells(5, 3), .Cells(12, maxcols)).Value   'get the searchable area on the worksheet, which starts at C5 and goes to maxcols12
        For Each i In basewsc                                       'start getting a list of all non-blank cells with no repeats
            isincollection = False
            If i <> vbNullString Then
                For Each Item In wsccoll
                    If Item = i Then
                        isincollection = True
                        Exit For
                    End If
                Next Item
                If Not isincollection Then
                    wsccoll.Add i
                End If
            End If
        Next i
        wsdict.Add .Name, wsccoll                     'populate the dictionary with the sheetname: [non-blank cells in sheet search area]
    End With
Next ws
'close the workbook because we don't need it anymore
WB.Close SaveChanges:=False
Set WB = Nothing
4

0 回答 0