Excel Macro loading Arrays

后端 未结 2 951
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-06 20:42

I am not sure how to phrase the question, but I have come up against an issue where I need to load a list of names from a group of sheets into seperate arrays.

For e

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 21:24

    All credit to @Tony Dallimore for Array or Array's idea. This is offered only as a supliment to his answer.

    To load all sheets data into an array use

    Dim TArray() as Variant
    Dim sh as Worksheet
    
    ReDim TArray(1 To ActiveWorkbook.Worksheets.Count)
    
    For each sh in ActiveWorkbook.Worksheets
        TArray(sh.Index) = sh.UsedRange ' or to get just Column A: sh.UsedRange.Columns(1)
    Next
    

    Or to load a subset of sheets whose names match a pattern

    i = 1
    For each sh in ActiveWorkbook.Worksheets
        If sh.Name Like "Region *" Then
             TArray(i) = sh.UsedRange ' or to get just Column A: sh.UsedRange.Columns(1)
             i = i + 1
        End If
    Next
    

    Again, credit to Tony: if OP feels inclined to accept this, please accept Tony's answer instead

提交回复
热议问题