VBA 发现不工作

VBA Find not working

我有一个工作簿,其中包含所有分支机构的所有发票列表,我们称之为 "Everything",基本上我需要搜索是否在另一个包含每个分支机构发票的文件中找到这些发票。它实际上是每个分支都在文件上,每个文件按月划分为 sheet ,我需要在每个 sheet 签入,然后在一个单元格中插入一个值。让我们称这个为“0001”,每个分支依此类推。

"everything" 文件基本上包含一列分支编号,一列发票编号,一列发行人代码和一列说明是否在分支文件中找到。分支文件包含相同的内容,除了分支编号,最后一列表示发票是否在 "Everything" 文件中。在某些情况下,发票在分支文件中而不在 "everything file" 中,还有在所有文件中而不在分支文件中的情况。

我试图做的是在 VBA 中插入一个循环,这样它会在所有文件中自动开具发票并打开特定的分支文件,然后在每个 [=24] 中搜索发票编号=].我还需要检查发行者是否相同,但首先我尝试了这段代码,当它搜索值时返回了错误的单元格!这是代码:

Dim sh As Worksheet 
Dim iLoop As Integer 
For iLoop = 7 To 1719 

 ' this is where the invoices are in an excel sheet

iloopoffset = iLoop - 6 

 ' as you see above, the list of invoices starts at line 7, so I used this to offset

If Range("K6").Offset(iloopoffset).Value = "No" Then 

     ' Column K is the one saying if the invoice was found or not in the branches file

    Set searchedvalue = Range("B6").Offset(iloopoffset, 0) 

     ' I used this so i could use the value in the .find formula

    MsgBox (searchedvalue.Value) 
    Workbooks.Open ("C:\Users\xxxxxx\Documents\xxxxxx\XML " + Range("D6").Offset(iloopoffset).Value) 
    For Each sh In Worksheets 
        If ActiveSheet.Name = "062015" Or "052015" Or "042015" Or "032015" Or "022015" Or "012015" Or "122014" Or "112014" Then 

             ' I needed to do this because on the sheets with the names above, the searched value will be in another column. sheets before 112014 are different.

            Set NFE = Worksheets(sh.Name).Range("B:B").Find(Range("B6").Offset(iloopoffset, 0).Value, lookat:=xlPart) 
        Else 
            Set NFE = Worksheets(sh.Name).Range("A:A").Find(Range("B6").Offset(iloopoffset, 0).Value, lookat:=xlPart) 
        End If 


        If Not NFE Is Nothing Then 
            MsgBox ("Found on sheet " + ActiveSheet.Name + " " + NFE.Address) 
            Range(NFE.Address).Offset(, 12).Value = "YES" 

             ' yes for found

            ActiveWorkbook.Save 
            ActiveWindow.Close 
        End If 
    Next sh 
    ActiveWorkbook.Save 
    ActiveWindow.Close 
End If 
Next iLoop 
End Sub 

这是怎么回事?我是 VBA 的真正菜鸟,但我没有发现这段代码有任何问题......你能帮帮我吗?

未测试:

Sub test()

    Const FILE_ROOT As String = "C:\Users\xxxxxx\Documents\xxxxxx\XML "
    Dim shtAll As Worksheet, rw As Range, searchedvalue
    Dim sh As Worksheet, wb As Workbook
    Dim iLoop As Long, colSrch As Long, NFE As Range
    Dim arrSheets

    Set shtAll = ActiveWorkbook.Sheets("Everything") 'adjust to suit...

    'sheets to watch out for....
    arrSheets = Array("062015", "052015", "042015", "032015", "022015", _
                      "012015", "122014", "112014")

    For iLoop = 7 To 1719

        Set rw = shtAll.Rows(iLoop)

        'if not found...
        If rw.Cells(1, "K").Value = "No" Then

            searchedvalue = rw.Cells(1, "B").Value

            Set wb = Workbooks.Open(FILE_ROOT & rw.Cells(1, "D").Value)

            For Each sh In wb.Worksheets

                'which column to search in? check if sheet name is in arrSheets
                colSrch = IIf(IsError(Application.Match(sh.Name, arrSheets, 0)), 1, 2)

                Set NFE = sh.Columns(colSrch).Find(searchedvalue, lookat:=xlPart)

                If Not NFE Is Nothing Then
                    MsgBox ("Found on sheet " + ActiveSheet.Name + " " + NFE.Address)
                    NFE.Offset(, 12).Value = "YES"
                    wb.Save
                    Exit For
                End If
            Next sh

            wb.Close savechanges:=False
        End If

    Next iLoop
End Sub

编辑

If Not NFE Is Nothing And sh.Range(NFE).Offset(, 8) = cnpj Then

我在这里看到几个问题:

  1. NFE 已经是一个范围,所以你可以只做 NFE.Offset(,8)

  2. VBA 将始终评估 Andboth 部分,即使第一部分为 False,因此在以下情况下NFENothing 第二部分将导致 运行 时间错误(因为您不能从无偏移...)。要处理这个问题,您需要两个不同的 If 块:

    If Not NFE Is Nothing Then
         If NFE.Offset(, 8) = cnpj Then
            'do something
         End If
    End If
    

应该做。