2017-10-12 10 views
1

findメソッドを使用して、異なるシートの2つの列(AとB)を比較しました。列Aは更新された列であり、列Bはコピーされた列です。コードはループし、列と一致するケースを検索します。列Aに一意の値がある場合は、列Bにコピーされます。ただし、一意の値が列Bに自動的には表示されません。列Aの一意の値のセルをクリックした場合のみ列Bにコピーしてください。Findメソッド固有の値が列に自動的に表示されない

なぜ自動的に更新できないのか分かりますか?

コード列Aに:

Private Sub Worksheet_SelectionChange(ByVal target As Range) 
If target.Column = 9 Then 
fabric = ActiveCell.Value 
Module4.ChkFabric (fabric) 
End If 

End Sub 

私は、列Bにコピーするために、モジュールを使用しています

Sub ChkFabric(ByRef fabric As String) 
Dim Rng, TgtC, ResC As Range 
Dim PrePlan As Worksheet 

Set PrePlan = Worksheets("Pre Master Plan") 

With PrePlan 
Set ResC = .Range("A:A") 
endrow = .Cells(PrePlan.Rows.Count, "A").End(xlUp).Row 
End With 
With ResC 
Set Rng = .Find(what:=Trim(fabric), LookIn:=xlValues, lookat:=xlWhole, 
searchorder:=xlByRows, searchdirection:=xlNext, _ 
      MatchCase:=False) 
If Not Rng Is Nothing Then 


Else 
    PrePlan.Cells(endrow + 1, 1) = fabric 
End If 
End With 


End Sub 

答えて

0

私はユニークな値を取得するためにコードの一部を書いた(項目が含まれていません重複している場合)

Sub uniq() 
Dim cntOccur As Integer 
Dim rowCount As Integer 
Dim Checkcol As Integer, Targetcol As Integer 
Dim currentRowValue As String 
Dim currentRow As Integer, brow As Integer 
Checkcol = 1 'Denotes A column 
Targetcol = 2 'Denotes B column 

rowCount = Cells(Rows.Count, Checkcol).End(xlUp).Row 
brow = 0 
    For currentRow = 1 To rowCount 
     currentRowValue = Cells(currentRow, Checkcol).Value 
' Check the number of occurrence of each and every cell in column A 
     cntOccur = Application.WorksheetFunction.CountIf(Range("A1:A" & rowCount), currentRowValue) 
If cntOccur = 1 Then 
brow = brow + 1 
' If it is unique and has no duplicates then write in to B 
Cells(brow, Targetcol).Value = currentRowValue 
End If 

Next 
End Sub 
+0

このコードでは、2つの列の比較のみが可能ですか?なぜなら、私のワークブックでは、複数の列を列Bと比較する必要があるからです。モジュールにコードを書き込んだのはなぜですか? –

+0

A1:A&rowcountから次の範囲の範囲をデータ範囲に拡張するだけで、 Application.WorksheetFunction.CountIf(範囲(データ範囲)、currentRowValue) – Valli

+0

申し訳ありませんが私の誤りは、複数のワークシートの列が列Bと比較することを意味していました –

関連する問題