在列vba中找到字母

时间:2018-03-22 17:14:58

标签: vba object if-statement

我有if函数应检查excel选项卡中的一些条件,如果列B<> 0和C列有“A”然后它给我“OK”

但它不起作用。我试图添加.text,.value或者其他什么,但它仍然看不到“A”并且给我“运行时错误424对象需要”

你能告诉我吗?

    Sub test()

        Dim varSheetA As Variant
        Dim varSheetB As Variant
        Dim strRangeToCheck As String
        Dim iRow As Long
        Dim iCol As Long
            Dim iRow1 As Long
        Dim iCol1 As Long
            Dim jRow As Long
        Dim jCol As Long
    Dim i As Long

        strRangeToCheck = "A1:V1000"
        ' If you know the data will only be in a smaller range, reduce the size of the ranges above.
        Debug.Print Now
        varSheetA = Worksheets("Sheet1").Range(strRangeToCheck)
        varSheetB = Worksheets("Sheet2").Range(strRangeToCheck) ' or whatever your other sheet is.
        Debug.Print Now

 For iRow1 = LBound(varSheetA, 1) To UBound(varSheetA, 1)
    For iCol1 = LBound(varSheetA, 2) To UBound(varSheetA, 2)

        If varSheetB(iRow1, 2) <> 0 And varSheetB(iRow1, 3)="A" Then
        MsgBox ("OK")

               'Sheets("Sheet2").Select
            'Cells(iRow1, iCol1).EntireRow.Copy
            'Sheets("Sheet4").Select
            'Range("A" & Rows.Count).End(xlUp).Offset(1).Select
            'ActiveSheet.Paste
    End If
        Next iCol1
    Next iRow1


MsgBox ("Done")
End Sub

1 个答案:

答案 0 :(得分:0)

您的代码很难遵循,但您的说明表明您希望在C列中查找字母A,如果找到则检查B列中的值是否为0。

我很确定你不希望每次匹配时都显示'OK' - 这可能是1000美元,这是不行的(除非你正在构建某种酷刑程序)。

此代码将返回一个消息框,列出符合条件的行号。毫无疑问,你会想要改变代码来做一些更有用的事情。

Public Sub Test()

    Dim SearchRange As Range
    Dim FoundValue As Range
    Dim FirstAddress As String
    Dim Message As String

    'Only going to search column C for the letter A.
    Set SearchRange = ThisWorkbook.Worksheets("Sheet1").Range("C1:C1000")

    With SearchRange
        'Find the first value if it exists.
        Set FoundValue = .Find("A", LookIn:=xlValues, LookAt:=xlWhole)

        'Only continue if something was found.
        If Not FoundValue Is Nothing Then
            FirstAddress = FoundValue.Address
            Do
                'Record the row number if the value in column B is not 0.
                If FoundValue.Offset(, -1) <> 0 Then

                    'Copy the row to Sheet2.
                    'Column C will have a value in each row (it will contain the letter A)
                    'so that can be used to find the next available row to copy to.
                    With ThisWorkbook.Worksheets("Sheet2")
                        FoundValue.EntireRow.Copy Destination:= _
                            .Cells(.Rows.Count, 3).End(xlUp).Offset(1, -2)
                    End With

                    Message = Message & FoundValue.Row & vbCrLf
                End If

                'Look for the next letter A.
                Set FoundValue = .FindNext(FoundValue)

            Loop While FoundValue.Address <> FirstAddress
        End If
    End With

    MsgBox "Criteria met on these rows:" & vbCrLf & Message, vbOKOnly + vbInformation

End Sub