VBA发现不工作

我有一个工作簿,其中包含所有分支机构的所有发票清单,我们称之为“全部”,基本上我需要search是否在包含每个分支机构发票的另一个文件中find发票。 它实际上是在每个分支的文件中,每个文件按月分割,我需要在每张表格中检查,然后在单元格中插入一个值。 我们把这个叫做“0001”等等。

“所有”文件基本上包含一个分行号码的列,一个是发票号码,一个是发行人代码,另一个是在分行文件中是否发现。 分支文件除了分支号以外都包含相同的内容,最后一列说明发票是否在“全部”文件中。 有些情况下,发票在分支文件上,而不是在“所有文件”上,也可能出现在所有文件上,而不在分支文件上。

我试图做的是在VBA中插入一个循环,这样它将在所有文件的发票之后自动开票并打开特定分支文件,然后在每张表中search发票号码。 我还需要检查发行者是否是相同的,但是首先我尝试了这个代码,当它search它返回错误的单元格的值! 这里是代码:

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的一个真正的noob,但是我没有发现这个代码有什么问题…你能帮我吗?

未经testing:

 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已经是一个Range了,所以你可以做NFE.Offset(,8)

  2. VBA将始终评估两个部分,即使第一个部分是False,所以在NFENothing的情况下,第二个部分将导致运行时错误(因为您无法从Nothing …中删除)。 要处理这个问题,你需要两个不同的If块:

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

应该这样做。