2017-10-24 5 views
0

私はVBAを初めて使用していますので、最初に値がない場合にシートを別のシートにコピーする必要があります。セルのフォーマットは常に同じです。idが最初に他のシートに欠落している場合にのみシートを別のシートにコピー

Sub test() 
    Dim tohere   As Worksheet 
    Dim fromhere   As Worksheet 
    Dim rngTohere   As Range 
    Dim rngfromHere  As Range 
    Dim rngCelTohere  As Range 
    Dim count    As Integer 
    Dim strArray   As Variant 
    Dim i     As Integer 

    'Set Workbooks 
    Set tohere = ThisWorkbook.Worksheets("Test") 
    Set fromhere = ThisWorkbook.Worksheets("Test 2") 

    'Set Columns 
    Set rngTohere = tohere.Columns("C") 'this is our column of interest 
    Set rngfromHere = fromhere.Columns("C") 
    i = 1 'this is counter to foryou to know which row you need to copy 

    count = rngfromHere.Cells.Count - WorksheetFunction.CountBlank(rngfromHere) 
    strArray = rngfromHere(Cells(1, 1), Cells(count, 1)).Value 

    'Loop through each cell in Column C 
    For Each rngCelTohere In rngTohere.Cells 
     If IsInArray(rngCelTohere.Value, strArray) = False Then 
      'here need to put copy entire row into sheet 
      'use i row to copy into tohere Worksheet 
     End If 
     i = i + 1 ' 
    Next rngCelTohere 


End Sub 

Function IsInArray(stringToBeFound As Integer, arr As Variant) As Boolean 'this functions returns true or false. for our case we need it to be false 
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1) 
End Function 

は誰も私を助けて、これは良いアイデアであれば言って、また、シートの最後にコピー行全体で私を助けることができます:今のところ私はこのコードを持っています。前もって感謝します!あなたのコードは、このためにコンパイルされません

答えて

0

Set i = 1 'this is counter to foryou to know which row you need to copy 
set count = rngfromHere.Cells.Count - WorksheetFunction.CountBlank(rngfromHere) 

は、これらは整数で、彼らはsetではありません。コードを準備したら、Debug>Compileに行き、コンパイルエラーがないかどうか確認してください。 VB Editorは、それらがどこにあるかを表示します。 i行目をコピーに関する

What does the keyword Set actually do in VBA?

、ここを参照してください:あなたのコードで

Copy row values after another row values, copy whole row to another sheet afterwards

Copy and Paste row by index number in Excel Macro

は、あなたのインデックスはrngCelTohere.Rowである可能性があります。

+0

私はそれを変更し、自分のコードをコンパイルしようとします。答えをありがとう、しかし、私はループの私のコードでi番目の行をコピーするための部分がまだ欠けています。私はVBAを使用していないので、このアイデアを試してみてください。今私はそれを必要としています。これを行う簡単な方法があるかどうかはわかりません。 – Chavez

+0

@Chavez - 編集を参照してください。 – Vityata

+0

ありがとうたくさんの男!あなたはロック! – Chavez

関連する問題