2016-04-08 9 views
0

ワークシートには、名前欄に指定されたIDを持つワークシートがすべて10個あります。どのようにしてVBAを使ってIDを配列に格納できますか?フィールド名をエクセル-vbaの配列に格納する

P.S.私は列見出しを保存したくありません。私は各列の名前ボックスに定義されているIDのIDを保存したい。

+1

名前ボックスってどういう意味ですか?スクリーンショットを投稿して詳細を提供できますか? – Will

+1

@ウィル - これは数式バーの左側にある*名前ボックス*を使って名前付き範囲*にすることになります。 – Jeeped

答えて

2

ブックの名前付き範囲をループする。各名前がそのワークシート上の範囲を参照し、名前付き範囲が少なくとも部分的に最初の行にある範囲を参照していることを確認します。ワークブックのスコープと

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の形式になります。

+0

私はあなたの仕事の大ファンです –

関連する問題