2017-12-01 7 views
0

更新:ちょうどfullrngダイナミックVBAレンジの問題 - 設定とダイナミック

全体像を作る方法を学ぶ必要がある: 私は自動的に列のデータに基づいて名前付き範囲を作成しようとしています。

私が投稿したサブプロシージャは、配列の要素を引数として取ります。このロジックはすべて正常に動作します。配列の各要素の名前付き範囲を作成できます。

私は自分の範囲内で値を取得する方法の論理を理解しました。

ROWNUM = 2 次の行を

をfinalrowするため、このロジックを含みます。

問題は、範囲に問題があることです。

私の範囲を設定する=範囲(セル)を設定すると、オブジェクトエラーが発生します。これは意味があり、オブジェクトは宣言されていますが、何も設定されていません。

私はちょうど私も動的にする必要が

fullrngにしたいセルのグループに変更し、その後、これらの範囲を初期化取得する方法を知っている、といけない、その後、2つのセルの高されますです3、4など

Sub Createranges(ByVal TableName As String) 

If TableName <> "" Then 

    Debug.Print TableName 
    Dim fullrng As Range 
    Dim temprng As Range 
    Dim thiscell As Range 
    Dim nextcell As Range 

    Set fullrng = Range("H1") 

    fullrng.Name = TableName 

     For rownum = 2 To finalrow 

      'Checking to see if cell should be in named range 
      If Cells(rownum, ColumnVar).Value = TableName Then 

       'Modify Ranges for union 
       Set thiscell = Range(ColumnVar & Cells.Row, ColumnVar & Cells.Column) 
       Set nextcell = Range(ColumnVar & Cells.Row + 1, ColumnVar & Cells.Column) 

       'union the range to include all past matching values 
       If thiscell.Value2 = nextcell.Value2(2, 1) Then 
        Set temprng = Range(thiscell, nextcell) 
        'Figure out dynamic ranges here 
        Set fullrng = Range(
        fullrng = Union(fullrng, temprng) 
       End If 

      End If 

     Next rownum 
    End If 


End Sub 
+0

トリックはありますか? –

+1

範囲を設定している場合(再割り当てしても)、Setキーワードを使用する必要があります。 – QHarr

答えて

1

範囲オブジェクトに間違った方法で割り当てられているため、あなたとすべてにバグがあります。

この行は

Set temprng = Range(thiscell, nextcell) 

thiscellと​​良くない既に範囲です。あなたがそれらを結合するためにUnionを使用する必要があります。

set temrange=application.union(thiscell, nextcell) 

同じように、この一部変更:改善として

set fullrange=application.union(fullrng,temprange) 

を、あなたは常にあなたが結合している両方の範囲であるUnionをunsingする前に確認する必要がありますis not nothing

たとえば、上記の行が初めて実行されたときには、fullrangeはエラーになることはありません。

if not fullrange is nothing then 
    set fullrange=application.union(fullrng,temprange) 
else 
    set fullrange=temprange 
end if 
0

多くの問題がありました。

あなた自身のオブジェクトを作成しないときに得られることは、オブジェクトモデルを探索して把握する必要があると思います。ここでは、私のColumnVarの代わりに& ROWNUMの変数をCells.Columnを使用して---露骨に間違っているいくつかのもののほかに

は私が変更するものである:

私は名前を付けることができるように私はもともとランダムなセルへの私の範囲を設定しました。私の範囲。フルルグ。Name = TableName私はfullrngを設定した後、この行を右に移動しました。

Sub Createranges(ByVal TableName As String) 

If TableName <> "" Then 

Debug.Print TableName 
Dim fullrng As Range 
Dim temprng As Range 
Dim thiscell As Range 
Dim nextcell As Range 


    For rownum = 2 To finalrow 

     'Checking to see if cell should be in named range 
     If Cells(rownum, ColumnVar).Value = TableName Then 

      'Modify Ranges for union 
      Set thiscell = Range(ColumnVar & rownum, ColumnVar & rownum) 
      Set nextcell = Range(ColumnVar & rownum + 1, ColumnVar & rownum + 1) 

      'union the range to include all past matching values 
      If thiscell.Text = nextcell.Text Then 
       Set temprng = Application.Union(thiscell, nextcell) 
       If Not fullrng Is Nothing Then 
        Set fullrng = Application.Union(fullrng, temprng) 
        fullrng.Name = TableName 
       Else 
        Set fullrng = temprng 
       End If 

      End If 

     End If 

    Next rownum 
End If 


End Sub