|
求帮忙看看怎么解决:想用下面的代码把不同工作簿(销售日报)批量打开,提取其中特定工作表里面产品A的销售额,但问题是,特定工作表的名称不规范,有的工作簿下面叫“营业日报”,有的叫“日报”或者“sheet1”,而且销售额的字段也有两种:销售(收款)和销售(已收款)。
按这段代码的话,黄色部分会报错,“下标越界”,因为有的工作簿下面并没有叫营业日报,日报或者sheet1的工作表,对象不存在,这应该怎么写呢?
奇怪的是,如果只统计叫“营业日报”的,绿色部分并不报错,而且两种名称的销售额都能提取,按理有一样的问题啊,就是对象不存在,会报“下标越界”才对吧?
Sub 日报统计()
Set fso = CreateObject("scripting.filesystemobject")
a = 1
Set sh = ActiveSheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
ThisWorkbook.UpdateLinks = xlUpdateLinksAlways
For Each f In fso.getfolder(ThisWorkbook.Path).Files
If f.Name <> ThisWorkbook.Name Then
sh.Cells(a, 1) = Split(f.Name, ".")(0)
Set wb = Workbooks.Open(f)
Set sheetname1 = wb.Sheets("营业日报")
Set sheetname2 = wb.Sheets("日报")
Set sheetname3 = wb.Sheets("sheet1")
If Not sheetname1 Is Nothing Then
sheetname = sheetname1
Else
If Not sheetname2 Is Nothing Then
sheetname = sheetname2
Else
sheetname = sheetname3
End If
End If
With sheetname
Set Rng = .UsedRange.Find("产品A", lookat:=xlWhole)
If Not Rng Is Nothing Then
r = Rng.Row
Set Rst1 = .UsedRange.Find("销售" & Chr(10) & "(收款)", lookat:=xlWhole)
Set Rst2 = .UsedRange.Find("销售" & Chr(10) & "(已收款)", lookat:=xlWhole)
If Rst1 Is Nothing Then
c = Rst2.Column
Else
c = Rst1.Column
End If
sh.Cells(a, 2) = .Cells(r, c)
End If
End With
wb.Close False
a = a + 1
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
感谢感谢~
|
|