将字段名称存储在excel-vba中的数组中

我在工作表中有10列,每个工作表都有一个在namebox中定义的指定ID。 如何使用VBA将ID存储在数组中

ps我不想存储列标题。 我想存储他们在每个列的namebox中定义的ID。

循环遍历工作簿的命名范围; 请检查每个名称是否指向该工作表上的范围,并且指定的范围是指至less部分位于第一行的范围。

Dim n As Long, vColNames As Variant Dim c As Long, twb As Workbook Set twb = ThisWorkbook With Worksheets("Sheet1") With .Cells(1, 1).CurrentRegion 'dim the array one-based to parallel the columns ReDim vColNames(1 To .Columns.Count) 'loop through all names looking for ones that intersect first row For n = 1 To twb.Names.Count 'check the parent worksheet first If twb.Names(n).RefersToRange.Parent.Name = .Parent.Name Then ' next check to ensure first row is part of named range If Not Intersect(twb.Names(n).RefersToRange, .Rows(1), .Cells) Is Nothing Then vColNames(Intersect(twb.Names(n).RefersToRange, .Rows(1)).Cells(1, 1).Column) = _ twb.Names(n).Name End If End If Next n End With End With For c = LBound(vColNames) To UBound(vColNames) Debug.Print vColNames(c) Next c 

具有工作簿范围的名称将作为myNamedRange出现 ; 那些工作表范围将以Sheet1!myNamedRange的forms。