在2列中查找2个值,将该行粘贴到新电子表格,然后根据特定条件在下一列中输入数据

时间:2015-07-01 18:07:22

标签: excel excel-vba vba

我尝试在2列中找到2个不同的值,然后将整行粘贴到新的电子表格中。然后根据原始工作表中的行号,我将不同的类别输入到新电子表格中的下一列。这是我的代码,但不知怎的,我无法正确输入部分。

Sub CommandButton1_Click()

    Dim strFind As String
    Dim strFind2 As String
    Dim i As Long, j As Long
    Dim wsFind As Worksheet
    Dim wsPaste As Worksheet

    'Get value to search for
    strFind = InputBox("Please enter the Base Origin:", "Enter Value")
    strFind2 = InputBox("Please enter the Base Destination:", "Enter Value")

    'Create object reference to the current worksheet
    Set wsFind = ActiveSheet

    'Create a new worksheet with object reference and then rename it
    Set wsPaste = Worksheets.Add
    wsPaste.Name = strFind
    wsPaste.Range("A1") = "Base Origin"
    wsPaste.Range("B1") = "Base Destination"
    wsPaste.Range("C1") = "20'"
    wsPaste.Range("D1") = "40'"
    wsPaste.Range("E1") = "40'HQ"

    'Paste starting at row 2 in wsPaste
    j = 2

    'Start searching from row 2 of wsFind, continue to end of worksheet
    For i = 2 To wsFind.UsedRange.Rows.Count
        If wsFind.Range("A" & i) = strFind Then
        If wsFind.Range("B" & i) = strFind2 Then
            'Copy row i of wsFind to row j of wsPaste then increment j
            wsFind.Range(i & ":" & i).Copy
            Worksheets(wsPaste.Name).Range(j & ":" & j).PasteSpecial xlPasteValues
            If i < 194 Then
            wsPaste.Range("F" & i) = "KN Fixed Rates"
                Else
                If i < 386 Then
                wsPaste.Range("F" & i) = "KN Market Rates June 2"
                    Else
                    If i < 574 Then
                    wsPaste.Range("F" & i) = "Shapiro 6.30.15"
                        Else
                        If i < 825 Then
                        wsPaste.Range("F" & i) = "Mode 6.30.15 Revised 3"
                        End If
                    End If
                End If
            End If
            j = j + 1
        End If
        End If
    Next i

End Sub

当我的i大于194且小于386时,我的下一列是空白而不是“KN Market Rates June 2

0 个答案:

没有答案