在循环VBA中查找功能

时间:2018-11-23 14:59:20

标签: excel vba excel-vba loops find

我有以下VBA代码。我想进行计算,但这只能在满足IF语句的情况下进行。这意味着在一个特定的单元格中,5个不同工作表中的日期应该相同,而在另一个单元格中,客户名称应该相同。日期和名称在工作表中以不同方式显示。 如果我要使用第一个代码,则该代码有效。但是if语句代码中使用的前两行找不到正确的值,因为在所使用的不同工作表中,它们的格式/显示方式不同。 因此,我认为有必要使用find函数来找到计算所需的正确值。提供的第二个宏中显示了使用find函数的代码。当我尝试运行该代码时,出现错误91。因此,假设我做的find函数出了点问题,但我找不到我犯的错误。 有人知道第二个代码出了什么问题吗?

Option Explicit

Sub DoSomething1()


    'Declare Variables
    Dim i As Long, r As Long, c As Long
    Dim count as Double, lastColData as Double, LastRowData as Double, lastRowInput as Double, StartSearch2 As Double
    Dim shtData As Worksheet, shtInput As Worksheet, shtInputI As Worksheet, shtInputII As Worksheet, shtInputIII As Worksheet, shtInputIV As Worksheet, shtInputV As Worksheet, shtInputVI As Worksheet, shtCopy As Worksheet
    Set shtData = Sheets("Data")
    Set shtInput = Sheets("INPUT")
    Set shtInputI = Sheets("INPUTP")
    Set shtInputII = Sheets("INPUTR")
    Set shtInputIII = Sheets("INPUTF")
    Set shtInputIV = Sheets("INPUTQ")
    Set shtInputV = Sheets("INPUTPF")
    LastColData = shtData.Cells(4, shtData.Columns.count).End(xlToLeft).Column
    LastRowData = shtData.Cells(shtData.Rows.count, "A").End(xlUp).row
    LastRowInput = shtInput.Cells(shtInput.Rows.count, "A").End(xlUp).row


    'Search starting from which row?
    StartSearch2 = InputBox("Vanaf welke rij wil je zoeken?", "Start", Default:=6)


    'Loop Code
    For r = StartSearch2 To LastRowData
        For c = 2 To LastColData
            count = 0
            For i = 2 To LastRowInput


                If shtInput.Cells(i,10).Value = shtData.Cells(r,1).Value And _
            shtInputI.Cells(r, 1).Value = shtData.Cells(r, 1).Value And _
                    shtInputIV.Cells(r, 1).Value = shtData.Cells(r, 1).Value And _
                    shtInputIII.Cells(r, 1).Value = shtData.Cells(r, 1).Value And _
                    shtInputII.Cells(r, 1).Value = shtData.Cells(r, 1).Value Then   'Dates are equal in different sheets

                            If shtInput.Cells(i,32).Value = shtData.Cells(4,c).Value And _
                shtInputI.Cells(4, c).Value = shtData.Cells(4, c).Value And _
                                shtInputV.Cells(4, c).Value = shtData.Cells(4, c).Value And _
                                shtInputIV.Cells(4, c).Value = shtData.Cells(4, c).Value And _
                                shtInputIII.Cells(4, c).Value = shtData.Cells(4, c).Value And _
                                shtInputII.Cells(4, c).Value = shtData.Cells(4, c).Value Then   'Names are equal in different sheets


                            count = count + (shtInputI.Cells(r, c).Value + shtInputII.Cells(r, c)) * shtInputV.Cells(5, c).Value * shtInputIV.Cells(r, c).Value * shtInputIII.Cells(r, c).Value

                            End If
                End If



            Next i

            shtData.Cells(r, c).Value = count
        Next c
    Next r

End Sub

Option Explicit

Sub DoSomething2()


    'Declare Variables
    Dim i As Long, r As Long, c As Long
    Dim count as Double, lastColData as Double, LastRowData as Double, lastRowInput as Double, StartSearch2 As Double
    Dim shtData As Worksheet, shtInput As Worksheet, shtInputI As Worksheet, shtInputII As Worksheet, shtInputIII As Worksheet, shtInputIV As Worksheet, shtInputV As Worksheet, shtInputVI As Worksheet, shtCopy As Worksheet
    Set shtData = Sheets("Data")
    Set shtInput = Sheets("INPUT")
    Set shtInputI = Sheets("INPUTP")
    Set shtInputII = Sheets("INPUTR")
    Set shtInputIII = Sheets("INPUTF")
    Set shtInputIV = Sheets("INPUTQ")
    Set shtInputV = Sheets("INPUTPF")
    LastColData = shtData.Cells(4, shtData.Columns.count).End(xlToLeft).Column
    LastRowData = shtData.Cells(shtData.Rows.count, "A").End(xlUp).row
    LastRowInput = shtInput.Cells(shtInput.Rows.count, "A").End(xlUp).row


    'Search starting from which row?
    StartSearch2 = InputBox("Vanaf welke rij wil je zoeken?", "Start", Default:=6)


    'Loop Code
    For r = StartSearch2 To LastRowData
        For c = 2 To LastColData
            count = 0
            For i = 2 To LastRowInput



        If shtInput.Cells.Find(What:=shtInput.Cells(i, 10).Value, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext) = shtData.Cells(r, 1).Value And _
            shtInputI.Cells.Find(What:=shtInputI.Cells(r, 1).Value, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext) = shtData.Cells(r, 1).Value And _
                    shtInputIV.Cells(r, 1).Value = shtData.Cells(r, 1).Value And _
                    shtInputIII.Cells(r, 1).Value = shtData.Cells(r, 1).Value And _
                    shtInputII.Cells(r, 1).Value = shtData.Cells(r, 1).Value Then

                            If shtInput.Cells.Find(What:=shtInput.Cells(i, 32).Value, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext) = shtData.Cells(4, c).Value And _
                            shtInputI.Cells.Find(What:=shtInputI.Cells(4, c).Value, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext) = shtData.Cells(4, c).Value And ?
                                shtInputV.Cells(4, c).Value = shtData.Cells(4, c).Value And _
                                shtInputIV.Cells(4, c).Value = shtData.Cells(4, c).Value And _
                                shtInputIII.Cells(4, c).Value = shtData.Cells(4, c).Value And _
                                shtInputII.Cells(4, c).Value = shtData.Cells(4, c).Value Then

                            count = count + (shtInputI.Cells(r, c).Value + shtInputII.Cells(r, c)) * shtInputV.Cells(5, c).Value * shtInputIV.Cells(r, c).Value * shtInputIII.Cells(r, c).Value

                            End If
                End If



            Next i

            shtData.Cells(r, c).Value = count
        Next c
    Next r

End Sub

1 个答案:

答案 0 :(得分:0)

首先,至少要输入1个错字:shtData.Cells(4, c).Value And ,而不是 _

然后,如果是我,我会将发现的内容放入变量中,然后对变量进行求值...,这样可以更轻松地跟踪任何问题,尤其是在找不到值时。

Sub DoSomething2()


    'Declare Variables
    Dim i As Long, r As Long, c As Long
    Dim count As Double, lastColData As Double, LastRowData As Double, lastRowInput As Double, StartSearch2 As Double
    Dim shtData As Worksheet, shtInput As Worksheet, shtInputI As Worksheet, shtInputII As Worksheet, shtInputIII As Worksheet, shtInputIV As Worksheet, shtInputV As Worksheet, shtInputVI As Worksheet, shtCopy As Worksheet
    Set shtData = Sheets("Data")
    Set shtInput = Sheets("INPUT")
    Set shtInputI = Sheets("INPUTP")
    Set shtInputII = Sheets("INPUTR")
    Set shtInputIII = Sheets("INPUTF")
    Set shtInputIV = Sheets("INPUTQ")
    Set shtInputV = Sheets("INPUTPF")
    lastColData = shtData.Cells(4, shtData.Columns.count).End(xlToLeft).Column
    LastRowData = shtData.Cells(shtData.Rows.count, "A").End(xlUp).Row
    lastRowInput = shtInput.Cells(shtInput.Rows.count, "A").End(xlUp).Row


    'Search starting from which row?
    StartSearch2 = InputBox("Vanaf welke rij wil je zoeken?", "Start", Default:=6)


    'Loop Code
    For r = StartSearch2 To LastRowData
        For c = 2 To lastColData
            count = 0
            For i = 2 To lastRowInput



        If shtInput.Cells.Find(What:=shtInput.Cells(i, 10).Value, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext) = shtData.Cells(r, 1).Value And _
            shtInputI.Cells.Find(What:=shtInputI.Cells(r, 1).Value, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext) = shtData.Cells(r, 1).Value And _
                    shtInputIV.Cells(r, 1).Value = shtData.Cells(r, 1).Value And _
                    shtInputIII.Cells(r, 1).Value = shtData.Cells(r, 1).Value And _
                    shtInputII.Cells(r, 1).Value = shtData.Cells(r, 1).Value Then

                            If shtInput.Cells.Find(What:=shtInput.Cells(i, 32).Value, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext) = shtData.Cells(4, c).Value And _
                            shtInputI.Cells.Find(What:=shtInputI.Cells(4, c).Value, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext) = shtData.Cells(4, c).Value And _
                                shtInputV.Cells(4, c).Value = shtData.Cells(4, c).Value And _
                                shtInputIV.Cells(4, c).Value = shtData.Cells(4, c).Value And _
                                shtInputIII.Cells(4, c).Value = shtData.Cells(4, c).Value And _
                                shtInputII.Cells(4, c).Value = shtData.Cells(4, c).Value Then

                            count = count + (shtInputI.Cells(r, c).Value + shtInputII.Cells(r, c)) * shtInputV.Cells(5, c).Value * shtInputIV.Cells(r, c).Value * shtInputIII.Cells(r, c).Value

                            End If
                End If



            Next i

            shtData.Cells(r, c).Value = count
        Next c
    Next r