簡體   English   中英

在循環 VBA 中查找函數

[英]Find Function in Loop VBA

我有以下 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 個錯字: shtData.Cells(4, c).Value And ? 而不是_

然后,如果是我,我會把這個 find 放在一個變量中,然后評估這個變量......可以更容易地跟蹤任何問題,尤其是當找不到值時。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM