我有一个工作簿,其中包含来自所有分支机构的所有发票的列表,让我们称之为" Everything",基本上我需要搜索是否在包含每个分支的另一个文件中找到发票#39;发票,。它实际上存在于每个分支的文件中,并且每个文件按月分割,我需要检入每个工作表,然后在单元格中插入一个值。让我们称这个为" 0001"等各个分支。
"一切"文件基本上包含一个带有分支编号的列,一个带有发票号,一个带有发行者代码,一个说是否在分支文件中找到它。分支文件包含除分支编号以外的相同文件,最后一列显示发票是否在" Everything"文件与否。有些情况下,发票在分支文件上,并且不在"所有文件"以及它在所有文件上并且不在分支文件上的情况。
我尝试做的是在VBA中插入一个循环,这样它就会在所有文件中的发票后自动开票并打开特定的分支文件,然后在每张表中搜索发票号。我还需要检查发行者是否相同,但首先我尝试了这个代码,当它搜索该值时,它返回了错误的单元格!这是代码:
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中是一个真正的菜鸟,但我没有发现这个代码有什么问题......你能帮助我吗?
答案 0 :(得分:1)
未测试:
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
我在这里看到几个问题:
NFE
已经是一个范围,所以你可以NFE.Offset(,8)
即使第一部分为False,VBA也会始终评估 {/ 1}}的部分,因此在And
为NFE
的情况下第二部分将导致运行时错误(因为您无法从Nothing中取消...)。要处理这个问题,你需要两个不同的If块:
Nothing
应该这样做。