简体   繁体   English

比较两列并使用vba复制粘贴

[英]Compare two columns and copy paste using vba

I have two columns in a sheet "test". 我在工作表“测试”中有两列。 Let's assume col C and D. 让我们假设C和D列。

Each rows in C and D might have either "COMPATIBLE" or "NOT DETERMINED" or Blank cell. C和D中的每一行可能具有“兼容”或“未确定”或空白单元格。

I want to compare col C and D,and if C has "COMPATIBLE" and D has "NOT DETERMINED", then "COMPATIBLE" should be paste into D and vice versa. 我想比较C和D列,如果C具有“兼容”并且D具有“未确定”,则应将“ COMPATIBLE”粘贴到D中,反之亦然。

I have below code, But not sure how to complete it: 我有以下代码,但不确定如何完成它:

Sub compare_cols()

'Get the last row
Dim Report As Worksheet
Dim i As Integer, j As Integer
Dim lastRow As Integer

Set Report = Excel.Worksheets("test") 'You could also use Excel.ActiveSheet _
                                        if you always want this to run on the current sheet.

lastRow = Report.UsedRange.Rows.Count

Application.ScreenUpdating = False

For i = 2 To lastRow
    For j = 2 To lastRow
        If Report.Cells(i, 1).Value = "COMPATIBLE" Then 
            If InStr(1, Report.Cells(j, 2).Value, Report.Cells(i, 1).Value, vbTextCompare) > 0

UPDATING THE WORK IN PROGRESS CODE: 以进度代码更新工作:

Option Explicit

Sub compare_cols()
With Worksheets("Latency") '<-.-| reference your worksheet
    With .Range("F1:G" & .UsedRange.Rows(.UsedRange.Rows.count).Row) '<--| reference its columns C:D range from row 1 down to worksheet last used row
        Correct .Cells, "COMPATIBLE", "Not Determind", 2
        Correct .Cells, "Determind", "COMPATIBLE", 1
    End With
    .AutoFilterMode = False
End With
End Sub

Sub Correct(rng As Range, val1 As String, val2 As String, colToChangeIndex As Long)
With rng '<--| reference passed range
    .AutoFilter Field:=1, Criteria1:=val1 '<--| filter referenced range on its 1st column with 'val1'
    .AutoFilter Field:=2, Criteria1:=val2 '<--| filter referenced range on its 2nd column with 'val2'
    If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then '<--| if any filterd cells other than header
        .Resize(.Rows.count - 1, 1).Offset(1, colToChangeIndex - 1).SpecialCells(xlCellTypeVisible).Value = "COMPATIBLE" '<--| write "COMPATIBLE" in column "D"
    End If
End With
End Sub

Try this code 试试这个代码

Sub CvalueAndDvalue()
    Dim cValue As Range, dValue As Range

    Dim Report As Worksheet
    Set Report = Excel.Worksheets("test")

    For i = 2 To Report.Range("C" & Rows.Count).End(xlUp).Row
        Set cValue = Report.Range("C" & i)
        Set dValue = Report.Range("D" & i)

        If (Trim(cValue) = "COMPATIBLE" And Trim(dValue) = "NOT DETERMINED") Then
            dValue = cValue
            ElseIf (Trim(dValue) = "COMPATIBLE" And Trim(cValue) = "NOT DETERMINED") Then
            cValue = dValue
        End If
    Next i
End Sub

you could use AutoFilter() : 您可以使用AutoFilter()

Option Explicit

Sub compare_cols()
    With Worksheets("test") '<-.-| reference your worksheet
        With .Range("C1:D" & .UsedRange.Rows(.UsedRange.Rows.Count).Row) '<--| reference its columns C:D range from row 1 down to worksheet last used row
            Correct .Cells, "COMPATIBLE", "NOT DETERMINED", 2
            Correct .Cells, "NOT DETERMINED", "COMPATIBLE", 1
        End With
        .AutoFilterMode = False
    End With
End Sub

Sub Correct(rng As Range, val1 As String, val2 As String, colToChangeIndex As Long)
    With rng '<--| reference passed range
        .AutoFilter Field:=1, Criteria1:=val1 '<--| filter referenced range on its 1st column with 'val1'
        .AutoFilter Field:=2, Criteria1:=val2 '<--| filter referenced range on its 2nd column with 'val2'
        If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then '<--| if any filterd cells other than header
            .Resize(.Rows.Count - 1, 1).Offset(1, colToChangeIndex - 1).SpecialCells(xlCellTypeVisible).Value = "COMPATIBLE" '<--| write "COMPATIBLE" in column "D"
        End If
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM