我只是想找到以D开头的工作簿,并包含 "Bob "这个表。
这是我的代码,但是当我调用函数时,它不喜欢wb.Name。我应该在那里加什么才行?
Sub View_Email()
'Select report workbook D*
For Each wb In Application.Workbooks
If wb1.Name Like "D*" And WorksheetExists("Bob", wb.Name) Then
Ct = Ct + 1
wb1.Activate
Set WB_rep = ActiveWorkbook
Exit For
End If
Next wb1
If Ct = 0 Then
MsgBox "Could not find D* file (report). Code will end."
Exit Sub
End If
'Copy Burn-Down chart from report
End Sub
Function WorksheetExists(shtName As String, Optional wb As Workbook) As Boolean
Dim sht As Worksheet
If wb Is Nothing Then Set wb = ThisWorkbook
On Error Resume Next
Set sht = wb.Sheets(shtName)
On Error GoTo 0
WorksheetExists = Not sht Is Nothing
End Function
谢谢你的帮助。下面的代码现在可以用了。
Sub View_Email()
Dim wb As Workbook
'Select report workbook D*
For Each wb In Application.Workbooks
If wb.Name Like "D*" And WorksheetExists("Bob", wb) Then
Ct = Ct + 1
wb.Activate
Set WB_rep = ActiveWorkbook
Exit For
End If
Next wb
If Ct = 0 Then
MsgBox "Could not find D* file (report). Code will end."
Exit Sub
End If
'Copy Burn-Down chart from report
End Sub
Function WorksheetExists(shtName As String, Optional wb As Workbook) As Boolean
Dim sht As Worksheet
If wb Is Nothing Then Set wb = ThisWorkbook
On Error Resume Next
Set sht = wb.Sheets(shtName)
On Error GoTo 0
WorksheetExists = Not sht Is Nothing
End Function