2016-05-06 11 views
0

私のワークシートには複数のテーブル/リストオブジェクトがあります。私は一度に1つのテーブルを選択しようとしています(ある時点で既存のテーブルの間に挿入されたテーブルが存在する可能性があるため、テーブル番号ではなく連続したビュー順で)。テーブルは選択され、範囲に変換され、その範囲が将来の使用のために宣言/設定される。範囲を変数ではなく定数で宣言しなければならないので、私はそうする方法を理解できません。ここに私のコードは、これまでのところです:整数nを変更して複数の範囲を宣言する

Dim LastRow As Long, c As Range 
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).row 
Dim n As Integer 
n = 1 
Dim tableName As String 
Dim SelectedCell As Range 

For Each c In Range("B6:B" & LastRow) 
    If c.Text = "Column1" Then 
     c.Activate 
     Set SelectedCell = ActiveCell 
     tableName = SelectedCell.ListObject.Name 
     ActiveSheet.ListObjects(tableName).Range.Select 
     ActiveSheet.ListObjects(tableName).Unlist 
     Dim rng(n) As Range 
     Set rng(n) = Selection 
     n = n + 1 
    End If 
Next 

答えて

0

ReDimあなたは(ともPreserve保存されたパラメータを削除しないために)必要なものです。また、それを行うために、それはここで問題にならないであろうVariantである必要があります:

Dim LastRow As Long, c As Range 
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row 
Dim n As Integer, rng() As Variant 
ReDim rng(1 To 1) 
n = 1 
Dim tableName As String 
Dim SelectedCell As Range 

For Each c In Range("B6:B" & LastRow) 
    If c.Text = "Column1" Then 
    c.Activate 
    Set SelectedCell = ActiveCell 
    tableName = SelectedCell.ListObject.Name 
    ActiveSheet.ListObjects(tableName).Range.Select 
    ActiveSheet.ListObjects(tableName).Unlist 
    ReDim Preserve rng(1 To n) 
    Set rng(n) = Selection 
    n = n + 1 
    End If 
Next 
関連する問題