2016-08-25 4 views
0

誰かが正しい方向に私を指し示すことができれば、私のコードは完全な行のデータを返しています。私の現在のコード。単語は「OVER」私はそれが例えばサイドバーに情報を返す必要が表示された場合、私は、各行および各列に調べる必要があり別のタブにvba固有のテキストコピー

Sub BUTTONtest_Click() 
Dim c As Range 
Dim j As Integer 
Dim Source As Worksheet 
Dim Target As Worksheet 

' Change worksheet designations as needed 
Set Source = ActiveWorkbook.Worksheets("Two Years") 
Set Target = ActiveWorkbook.Worksheets("Two Years League") 

j = 3  ' Start copying to row 1 in target sheet 
For Each c In Source.Range("G6:K6") ' Do 50 rows 
    If c.Text = "OVER" Then 
     Source.Rows(c.Row).Copy Target.Rows(j) 
     j = j + 1 
    End If 
Next c 
End Sub 

列B各小区分に適用するには、これを必要とします。カラムC- FはG等

enter image description here

答えて

1

これを返す必要があり、列B及びH-Kの数を返す必要がありますか?

Sub BUTTONtest_Click() 
Dim c As Range 
Dim j As Integer 
Dim Source As Worksheet 
Dim Target As Worksheet 

' Change worksheet designations as needed 
Set Source = ActiveWorkbook.Worksheets("Two Years") 
Set Target = ActiveWorkbook.Worksheets("Two Years League") 

j = 3  ' Start copying to row 1 in target sheet 

For i = 1 To 3 'Number of ¿wees? 
    For Each c In Source.Range(Cells(6, 5 * i - 2), Cells(50, 5 * i + 1)) ' Do 50 rows 
     If c.Text = "OVER" Then 
      Target.Cells(j, 1) = Source.Cells(c.Row, 5 * i - 3) 
      j = j + 1 
     End If 
    Next c 
Next i 

End Sub 

EDIT 、繰り返し行をしたいこれを試してみてくださいしていない場合:

Sub BUTTONtest_Click() 
Dim c As Range 
Dim j As Integer 
Dim Source As Worksheet 
Dim Target As Worksheet 

' Change worksheet designations as needed 
Set Source = ActiveWorkbook.Worksheets("Two Years") 
Set Target = ActiveWorkbook.Worksheets("Two Years League") 

j = 3  ' Start copying to row 1 in target sheet 
a = 1 
For i = 1 To 3 'Number of ¿wees? 
    For Each c In Source.Range(Cells(6, 5 * i - 2), Cells(50, 5 * i + 1)) ' Do 50 rows 
     If c.Text = "OVER" Then 
      If a <> c.Row Then 
       Target.Cells(j, 1) = Source.Cells(c.Row, 5 * i - 3) 
       j = j + 1 
       a = c.Row 
      End If 
     End If 
    Next c 
Next i 

End Sub 
+0

はそれがない。このためにありがとうございました(コメント)このコードを試みることができます私が必要とするのは、この部分が何のためにSource.Range(Cells(6、5 * i-2)、Cells(50,5 * i + 1)) 'と' Target。セル(j、1)= Source.Cells(c.Row 、5 * i - 3) 'それとも私にとってこれをより良く説明するサイトを知っていますか? – Paula

+0

範囲は 'Range(TopLeftCell、BottomRightCell)'として定義します。セルは「セル(行、列)」として表示されます。 2つを組み合わせて、 'Range(Cells(row1、column1)、Cells(row2、column2))'です。しかし、あなたのケースでは、3つのwees( 'i = 1〜3')があり、各範囲は' 5 * i - 2'列から始まり '5 * i + 1'列で終わります。あなたのニーズに合っている場合は、受け入れられたものとしてマークしてください.... – CMArg

+0

詳細:ちょうどその場合:3列目が始まり、8列目が2列目、13列目が3列目です。値(1,3)、(2,8)、(3,13) - >線形回帰、 - >方程式を表示します。 – CMArg

1

あなたは

Option Explicit 

Sub BUTTONtest_Click() 
    Dim Source As Worksheet 
    Dim Target As Worksheet 
    Dim iSection As Long 
    Dim sectionIniCol As Long, sectionEndCol As Long 

    ' Change worksheet designations as needed 
    Set Source = ActiveWorkbook.Worksheets("Two Years") 
    Set Target = ActiveWorkbook.Worksheets("Two Years League") 

    With Source '<--| reference 'Source' sheet 
     With .Range("B6:F" & .Cells(.Rows.Count, "B").End(xlUp).row) '<--| reference its columns "B:F" range from row 6 down to last non empty cell in column "B" 
      With .Offset(, -1).Resize(, 1) '<--| reference corresponding cells in column "A" (which is an empty column) 
       For iSection = 1 To 3 '<-- loop over all your three 5-columns sections 
        sectionIniCol = (iSection - 1) * 5 + 2 '<-- evaluate current section initial col 
        sectionEndCol = sectionIniCol + 4 '<-- evaluate current section ending col 
        .FormulaR1C1 = "=if(countif(RC" & sectionIniCol + 1 & ":RC" & sectionEndCol & ",""OVER"")>0,1,"""")" '<-- write (temporary) formulas in column "A" cells to result "1" should at least one "OVER" occurrence be in corresponding cells of current section columns 
        If WorksheetFunction.Sum(.Cells) > 1 Then Intersect(.Columns(sectionIniCol), .SpecialCells(xlCellTypeFormulas, xlNumbers).EntireRow).Copy Target.Cells(Target.Rows.Count, 1).End(xlUp).Offset(1) '<-- if any occurrence of "OVER" has been found then copy section initial column cells corresponding to column "A" cells marked with "1" and paste them in from first empty row of 'Target' sheet... 
       Next iSection 
       .ClearContents '<--| delete (temporary) formulas in target column "A" 
      End With 
     End With 
    End With 
End Sub 
+0

あなたのデータがあなたが投稿したもの(50行)のような小さな行範囲に広がっていれば、このアプローチも十分に速いことが証明されます。データが行数(数千プラス)でかなり大きくなると、異なるアプローチ(フィルタリング、配列)がより効果的になるでしょう。 – user3598756

+0

@Paula:フィードバックはまったくありませんか?それは私がおそらく人々を助けてくれると思うのです。 – user3598756

+0

お詫び申し上げます私はあなたのコードがうまくいくことを発見しましたが、静的な50行しか残さないので、調節不能なコードを使ってください。また、私の非常に基本的な知識は、あなたのコードを完全に理解していないことを意味しましたが、私はその中で私の頭を掴み、それをどうやって変更するかを考えています。私はそれがあなたの助けを全面的にお手伝いしてくれた別の作品に役立つかもしれないと思う – Paula

関連する問題