Excel VBA嵌套数组提供基于节select的用户名称select

我试图(最终)根据用户在第一个(或主要)数据validation框中的select来填充具有特定select的数据validation。 目前我需要得到这个嵌套数组,因为我相信这将是解决这个问题的第一步。 但是,我不能得到这个嵌套的数组填充。 先谢谢你!

Sub Test_NestedArray() Dim ws as Worksheet Set ws = Worksheet("EOS Report") Dim Array_Machine_List_Choices As Variant Dim Array_Fab Dim Array_Paint Dim Array_Sub Dim Array_Asy Dim Array_Facilities Array_Fab = Array(Range(MACHINESFAB)) 'referencing named ranges on the sheet Array_Paint = Array(RANGE(MACHINESPAINT)) Array_Sub = Array(RANGE(MACHINESSUB)) Array_Asy = Array(RANGE(MACHINESASY)) Array_Facilities = Array(RANGE(MACHINESFACILITIES) Array_Machine_List_Choices = Array(Array_Fab, Array_Paint, Array_Sub, Array_Asy, Array_Facilities) MsgBox (Array_Machine_List_Choices(1)) 'see the array choices for Paint End Sub 

取消/评论如有必要。 希望尽pipe这显示了你需要多一点评论

 Sub Test_NestedArray() Dim ws As Worksheet Dim Array_Fab As Variant, Array_Paint As Variant, Array_Sub As Variant Dim Array_Asy As Variant, Array_Facilities As Variant, Array_Machine_List_Choices As Variant Set ws = Worksheets("EOS Report") ' Assuming all your ranges are in this worksheet. If not delete the With and the End With and the .'s in front of Range With ws ' ' If data is in a single row with multiple columns ' Array_Fab = Application.Transpose(Application.Transpose(Range("MACHINESFAB"))) 'referencing named ranges on the sheet ' Array_Paint = Application.Transpose(Application.Transpose(Range("MACHINESPAINT"))) ' Array_Sub = Application.Transpose(Application.Transpose(Range("MACHINESSUB"))) ' Array_Asy = Application.Transpose(Application.Transpose(Range("MACHINESASY"))) ' Array_Facilities = Application.Transpose(Application.Transpose(Range("MACHINESFACILITIES"))) ' If data is in a single column with multiple rows Array_Fab = Application.Transpose(Range("MACHINESFAB")) 'referencing named ranges on the sheet Array_Paint = Application.Transpose(Range("MACHINESPAINT")) Array_Sub = Application.Transpose(Range("MACHINESSUB")) Array_Asy = Application.Transpose(Range("MACHINESASY")) Array_Facilities = Application.Transpose(Range("MACHINESFACILITIES")) End With Array_Machine_List_Choices = Array(Array_Fab, Array_Paint, Array_Sub, Array_Asy, Array_Facilities) MsgBox Join(Array_Machine_List_Choices(1), vbNewLine) 'see the array choices for Paint End Sub 

Application.Transpose是将Range转换为一维数组的一种方法。 如果您将数组设置为排列,则会创build一个二维数组(即使您只指向一列),所以使用下面的示例

 +----+ | A1 | +----+ | A2 | +----+ | A3 | +----+ | A4 | +----+ 

要访问元素A1你必须引用它作为Array(1,1) (不是Array(0,0)设置范围的数组使用Base 1

通过使用Application.Tranpose技巧,Excel创build一维数组,以便您可以将其作为Array(1)引用。 这首先简化了引用,其次(更重要的是)允许您使用很多数组函数,例如JoinFilter ,这些函数在二维数组上不起作用(Excel没有2D的任何内置函数arrays)