2016-12-12 6 views
0

1つのシートの3つの列から別の行の1つの列にデータを移動しようとしています(このデータを取得するためにフィルタリングする必要があります)。ソースシートからフィルターをかけて、宛先シートにプラグインできるすべてのインシデントを取り出す必要があります。vba:フィルタ処理されたデータの3つの列を別のシートの1つの行にコピーする

フィルタリングされたデータに問題があります。私は変数(ソーター)を作成しようとしています、その変数でフィルターをかけて、ソーター変数を取得した行の宛先シートにフィルターされたデータをコピーし、次のソーター変数に移動します。これまでのところ、それは次のようになります。

'Set up my loop based on id in cell from destination page 
    For i = lrow To 1 Step -1                 'need to double check the i and j - one should be from s and the other should be from d 
     For j = 2 To lrow2 Step 1 
     sorter = s.Cells(j, 1).Value 
       'Sorting the source sheet 
       With srange 
        .AutoFilter Field:=1, Criteria1:=sorter 
        'find the first and last row of the visible range 
        firstrow = .AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Row 
        lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row 
         For Row = firstrow To lastrow 
          'select header row of source info 
          s.Range("c1").Select 
           'select copy the cells of the first filtered section 
           ActiveCell.Offset(1, 0).Select 
             If Not cell.EntireRow.Hidden Then 
              'ActiveCell.Copy d.Range("b2") 
              'Copying and pasting first row 
              ActiveCell.Offset(0, 1).Copy d.Cells(j, 2) 
              ActiveCell.Offset(0, 2).Copy d.Cells(j, 3) 
              ActiveCell.Offset(0, 3).Copy d.Cells(j, 4) 
              'Copying and pasting second row 
              ActiveCell.Offset(1, 1).Copy d.Cells(j, 5) 
              ActiveCell.Offset(1, 2).Copy d.Cells(j, 6) 
              ActiveCell.Offset(1, 3).Copy d.Cells(j, 7) 
              'Copying and pasting third row 
              ActiveCell.Offset(2, 1).Copy d.Cells(j, 8) 
              ActiveCell.Offset(2, 2).Copy d.Cells(j, 9) 
              ActiveCell.Offset(2, 3).Copy d.Cells(j, 10) 
              'etc - I have 8 rows, but that should get me started 
             End If 
         Next Row 
        End With 
      Next j 
    Next i 

ライン

firstrow = .AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Row 

とラインの両方が、私はそれらを回避しようとすると、必要なエラー424オブジェクトを与える

if not cell.entirerow.hidden then 

。この問題(VBA: Select the first filtered cell then move onto the next one down)から解決策を実装しようとしましたが、この時点ではかなり失われています。すべての助けをありがとう。

以下は、Scottが修正した問題です。ありがとうございました! - 後世

問題1については、以下を保持することは、私の範囲は動的であるということである、と私は私のコードのこれらの行でエラー1004を取得しておいてください。

dim lrow as string 
    dim lrow2 as string 

    lrow = s.Range("a" & Rows.Count).End(xlUp).Row 
    lrow2 = d.Range("a" & Rows.Count).End(xlUp).Row 

    set name1 = s.Range(Cells(2, 1), Cells(lrow, 1)) 
    set name2 = d.Range(Cells(2, 1), Cells(lrow2, 1)) 
    set srange = s.Range(Cells(1, 1), Cells(lrow, 5))        
    set drange = d.Range(Cells(1, 1), Cells(lrow2, 10)) 

私が働く鉱山の別のマクロからこれをコピーし、サイト上の他の回答(具体的にはVBA: Selecting range by variables)と同じように見えます。変数の上にカーソルを置くと、必要な番号が表示されます。数字で置き換えると変数は変わりません。変数は問題ではありません。 a1表記法に変更すると、drange = d.range( "a1:j10")を設定するとうまくいきますので、見えないシンプルなものがありません。

+1

最初の問題:Range()内のRangeオブジェクト 'Cells()は、Range自体と同じ親で修飾する必要があります。例: 'set name1 = s.Range(s.Cells(2,1)、s.Cells(lrow、1))' –

+0

これを入手しました。スコットに感謝します! – uttuck

答えて

0

私はちょうど両方のページと情報を貼り付けるループを作成しました。私が望むよりもはるかに時間がかかりますが、私は手動でやっていましたので、それよりはるかに高速です。私はそれがいくつかの方法をいくつかスピードアップできることを知っていますが、私は私の悪い解決策を投稿すると思っていました。私はこの問題に答えないので、これを解決した印をつけません、そして私はまだそれが答えるのが好きです。

'Loop through source 
'Loop starts with setting up i for source sheet 
For i = 2 To lrow2 Step 1       'i stays on destination sheet 
'Setting up the loop for the destination sheet 
For j = 2 To lrow Step 1      'j stays on source sheet 

    'sorting by range in the destination sheet 
    sorter = d.Cells(i, 2).Value 
     'Look in cell on source page 
     lcol = d.Cells(i, d.Columns.Count).End(xlToLeft).Column 
     If s.Cells(j, 2).Value = sorter Then _ 
      s.Cells(j, 15).Copy d.Cells(i, lcol + 1) 
     If s.Cells(j, 2).Value = sorter Then _ 
      s.Cells(j, 16).Copy d.Cells(i, lcol + 2) 
     If s.Cells(j, 2).Value = sorter Then _ 
      s.Cells(j, 17).Copy d.Cells(i, lcol + 3) 

Next j 
Next i 
関連する問題