2017-11-02 10 views
0

VBAを使用して、ワークブック「トランザクションファイル」のシートを検索し、3つの基準に基づくブック「ユーティリティ」のシートにコピーしようとしています。最初の1つは、カラムAが「PV」の場合、第2の基準は、カラムMが「ユーティリティー水」「ユーティリティーエレクトリック」または「ユーティリティガス」のいずれかであり、第3のものはカラムAEが3つすべてが満たされている場合は、行をコピーして貼り付けます。3つの条件を使用して1つのシートから別のシートに行をコピーする

私は、このテンプレートを見つけましたが、私の基準にプラグインする場所がわからない:

Sub copyRow() 
Dim ws1 As Worksheet, ws2 As Worksheet 
Dim lastrowDest As Long, currowSrc As Long, currowDest As Long, lastrowSrc 
As Long 
Dim critvalue1 As String 

Set ws1 = Sheets("Sheet2") 
Set ws2 = Sheets("Sheet1") 

lastrowSrc = ws2.Range("AE" & Rows.Count).End(xlUp).Offset(1).Row - 1 
lastrowDest = ws1.Range("AE" & Rows.Count).End(xlUp).Row 

For currowSrc = 2 To lastrowSrc 
critvalue1 = ws2.Range("E" & currowSrc).Value 
ws2.Cells(6, 5).Value = critvalue1 
For currowDest = 2 To lastrowDest 
    If ws1.Range("E" & currowDest).Value = critvalue1 Then 
     ws2.Rows(currowSrc).Copy Destination:=ws1.Range("A" & currowDest) 
    End If 
Next currowDest 
Next currowSrc 

End Sub 

感謝を!

答えて

0

あなたが与えたコード例は、シートごとにあります。 別のワークブック内の1枚のシート、別のワークブック内の別のシートをチェックしたいとします。

だから、あなたはそのブックを開く必要があります。私は私が入力されたかを示すために私の最初の投稿を編集してきましたが、エラーを取得しています

Dim trx as workbook ' the other workbook (i.e. the other excel file). 
' trx is a vba variable that will hold the contents of the transactions excel file 
' a workbook has sheets in it. 
dim sheet1 as worksheet, sheet2 as worksheet 
' these are two vba variables holding the two sheets we'll work on. 
dim criteria1 as boolean, criteria2 as boolean, criteria3 as boolean 
' boolean variables can have the value of True or False only. 

' just for reading 
dim columnAindex as integer, columnMindex as integer, columnAEindex as integer 

columnAindex = 1 
columnMindex = Range("1M").column ' 13 
columnAEindex = Range("AE").column ' yup, that number 
set trx = Workbooks.open("trx.xlsx") 
set sheet2 = trx.Sheets(1) ' or whatever sheet by index or name. 

For Each myrow in Rows 
    criteria1 = (myrow.cells(1,columnAindex) = "PV') ' .cells(1,1) is columnA of current row 
    m = myrow.cells(1, columnMindex) ' value in cell of this current row, at column M 
    criteria2 = ((m = this) or (m = that) or (m = theother)) 
    ' if the value in AE is unique for each row, this will work: 
    ' I got that from another question on stack overflow 
    ae = myrow.cells(1, columnAEindex) ' value in our row at column AE 
    set trxAEcolumnRange = sheet2.Range("AE:AE") 
    criteria3 = IsError(Application.Match(ae, trxAEcolumnRange, 0)) ' true if could not find a match to the value in our row's ae. 


    if criteria1 and criteria2 and criteria3 then copypasterow(myrow) 
next 
trx.close() 
end sub 

sub CopyPasteRow(aRow) 
    aRow.copy 
    sheet2.range.end.paste ' no need for xlup because this is the next empty row after the data 
end sub 
+0

次に、あなたが私たちのシート内の行ごとを見ていきます。私は間違って何かを入力したと確信しています。あなたの助けをありがとう! – kong1802

+0

こんにちは。それを元の状態に戻す。次に、私が書いたコードが動作していないという発言を追加して、それを修正します。 プログラムをデバッグして、正確に何が失敗しているかを確認します。ファイルを開いていますか? コンパイルエラーが発生していますか?正確に何が間違っていますか? いずれにせよ、基準3を正しく理解しましたか?あるいは、あなたは宿題を持っていて、あなた自身が本当にその質問を理解していない学生ですか? – pashute

+0

あなたの質問を元に戻して編集しました。私はコードをチェックし、何が正しく動作しないかを見ていきますが、重大な問題はないと思われます。コードを読んで、私のコメントに返信するか、あなたが理解していないコード行か、プログラムがどこで失敗しているのかを教えてください。 – pashute

関連する問題