具有两个条件的指令函数

时间:2019-06-26 12:19:57

标签: excel vba

我在Sheet2的单元格“ A1”中有一个字符串,而在Sheet2的单元格“ A2”中有另一个字符串,我使用LEFT函数将它们带走。这些在每次导入时都在变化。我试图在Sheet1的“ AP”列中找到第一个字符串,并在Sheet1的“ AA”列中找到第二个字符串,并按这些值对工作表进行排序。然后,我想复制整个Sheet1并将其粘贴到Sheet2中。我的代码没有给我任何帮助。为什么错了?

Sub rc1()
    Dim lastrow As Long
    Dim i As Integer, icount As Integer
    Dim j As Integer, jcount As Integer
    Dim LResult As String

    LResult = Sheets("Sheet2").Range("A1")
    LResult = Left(LResult, 4)
    JResult = Sheets("Sheet2").Range("A2")
    JResult = Left(JResult, 2)

    lastrow = Sheets("Sheet1").Range("A30000").End(xlUp).Row

    Sheets("Sheet2").Activate
    Sheets("Sheet2").Range("B2:AQ" & lastrow).Select
    Selection.ClearContents

    icount = 1
    For i = 2 To lastrow
        For j = 2 To lastrow
            If InStr(1, LCase(Sheets("Sheet1").Range("AP" & i)), LCase(LResult)) <> 0 And InStr(1, LCase(Sheets("Sheet1").Range("AA" & j)), LCase(JResult)) <> 0 Then
                icount = icount + 1
                Sheets("Sheet2").Range("B" & icount & ":AQ" & icount) = Sheets("Sheet1").Range("A" & i & ":AP" & i).Value
            End If
        Next j
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

我认为您的问题在这里:

LResult = Sheets("Sheet2").Range("A1")
LResult = Left(LResult, 4)
JResult = Sheets("Sheet2").Range("A2")
JResult = Left(LResult, 2)

最后一行不是JResult = Left(JResult, 2)吗?

此刻的工作方式,它将覆盖在sheet2单元格A2中分配的值,在sheet2单元格A1中最左边的2个字符。如果此值随后未出现在AA列中,则if语句中的条件将永远不会评估为true。