我的公司当前正在从Office 2013(32位)升级到Office 365和Excel(64位)。我已经“有机会”解决了一些VBA问题。
我经验不足,花了几个小时来解决我在下面描述的问题。我确信这是对比我有更多知识的人的简单修复。我知道从Excel 2016 32位到Excel 2016 64位现在正在使用VBA 7。
有些VBA创建了一个临时工作表,当它试图删除它时,它会抛出“运行时错误91。对象变量或未设置块变量”错误。
清理:
Application.DisplayAlerts = False
Cws.Delete 'this is line that fails
Application.DisplayAlerts = True
'Remove formula
Range("V3", Cells(Fill_Row, 34)).ClearContents
知道我需要更改什么吗?
完整的代码如下:
'''' 子CreateFile()
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim mailAddress As String
Dim ccAddress As String
Dim NewWB As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Fill_Row As Long
Dim strDir As String
strDir = Worksheets("Variables").Range("B26")
'Find last row of pivot
Fill_Row = Range("A2")
ccAddress = Range("B3")
If Range("B2") <> "" Then
If MsgBox(Range("B2") & " Send anyway?", vbYesNo) = vbNo Then Exit Sub
End If
On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
'Autofill Formula
Range("V2:AH2").AutoFill Destination:=Range("V2", Cells(Fill_Row, 34)), Type:=xlFillDefault
'Set filter sheet, you can also use Sheets("MySheet")
Set Ash = ActiveSheet
'Set filter range and filter column (column with names)
Set FilterRange = Ash.Range("F1:U" & Ash.Rows.Count)
FieldNum = 1 'Filter column = A because the filter range start in column A
'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Cws.Range("A1"), _
CriteriaRange:="", Unique:=True
'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))
'If there are unique values start the loop
If Rcount >= 2 Then
For Rnum = 2 To Rcount
'Look for the mail address in the MailInfo worksheet
mailAddress = ""
On Error Resume Next
mailAddress = Application.WorksheetFunction. _
VLookup(Cws.Cells(Rnum, 1).Value, _
Worksheets("Commission").Range("A5:B" & _
Worksheets("Commission").Rows.Count), 2, False)
On Error GoTo 0
If mailAddress <> "" Then
'Filter the FilterRange on the FieldNum column
FilterRange.AutoFilter Field:=FieldNum, _
Criteria1:=Cws.Cells(Rnum, 1).Value
'Copy the visible data in a new workbook
With Ash.AutoFilter.Range
On Error Resume Next
Set rng = .SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With
Set NewWB = Workbooks.Add(xlWBATWorksheet)
rng.Copy
With NewWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial Paste:=xlPasteValues
.Cells(1).PasteSpecial Paste:=xlPasteFormats
.Cells(1).Select
Application.CutCopyMode = False
End With
'Create a file name
TempFilePath = strDir & "\"
TempFileName = "Sales Installed Report for " & Replace(Range("A2"), "/", "-") _
& " " & Range("P1")
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2016
FileExtStr = ".xlsx": FileFormatNum = 51
End If
'Save, Mail, Close and Delete the file
With NewWB
.SaveAs TempFilePath & TempFileName _
& FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
On Error GoTo 0
.Close savechanges:=False
End With
End If
'Close AutoFilter
Ash.AutoFilterMode = False
Next Rnum
End If
cleanup:
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True
'Remove formula
Range("V3", Cells(Fill_Row, 34)).ClearContents
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
''Send email if agent has no sales
'Call No_Sale_Email
''Display pop up window
MsgBox "Report Complete."
End Sub
''''
答案 0 :(得分:0)
我认为可能是因为您尚未为变量分配值
(例如,设置cws = Thisworkbook.sheets(“工作表名称”)) 请再次检查cws变量
答案 1 :(得分:0)
在将cws设置为新工作表之前,存在错误。您的错误与删除工作表无关。发生错误是因为您拥有:
Ruby stdout -------> stdin ./db
stdin <-------- stdout
(blocked because empty)
pipe.puts("a") "a\n" -------> empty <read from stdin>
empty <-------- empty
(still blocked)
pipe.puts("b") "a\nb\n" -----> empty <read from stdin>
empty <-------- empty
(flushing stdout) (continues consuming "a\nb\n")
pipe.close_write empty ----/---> "a\n\b" <read from stdin>
empty <-------- empty
(blocks because empty)
pipe.gets(nil) empty ----/---> empty <send data to stdout>
empty <-------- "data\n"
(continues consuming "data\n") (flushing stdout)
pipe.gets(nil) empty ----/---> empty <close stdout>
"data\n" <--/-- empty
将您带到:
On Error GoTo cleanup
之前:
cleanup:
Cws.Delete
摆脱“出现错误...”,检查该错误,并在必要时创建新帖子。