VBA在Excel 2010上运行,但在Excel 2013上出现错误

VBA在Excel 2010上运行,但在Excel 2013上出现错误,vba,excel,Vba,Excel,我在Excel 2010上创建了这段代码,但当尝试在Excel 2013上运行它时,我得到了一个运行时1004错误,调试器高亮显示了这一行 wbDest.Sheets(1).Name = cell.Value 我认为2010年写的任何东西都会在2013年生效。我的假设是错误的吗?有什么建议吗 Private Sub CommandButton1_Click() Const sColumn As String = "M" Dim wbDest As Workbook Dim rngFil

我在Excel 2010上创建了这段代码,但当尝试在Excel 2013上运行它时,我得到了一个运行时1004错误,调试器高亮显示了这一行

 wbDest.Sheets(1).Name = cell.Value 
我认为2010年写的任何东西都会在2013年生效。我的假设是错误的吗?有什么建议吗

Private Sub CommandButton1_Click()

Const sColumn As String = "M"

Dim wbDest As Workbook
Dim rngFilter As Range, rngUniques As Range
Dim cell As Range

Set rngFilter = Range(sColumn & "1", Range(sColumn & Rows.Count).End(xlUp))

Application.ScreenUpdating = False

With rngFilter
    .AdvancedFilter Action:=xlFilterInPlace, Unique:=True
    Set rngUniques = Range(sColumn & "2", Range(sColumn & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
    On Error Resume Next
    ActiveSheet.ShowAllData
    On Error GoTo 0
End With

For Each cell In rngUniques
    Set wbDest = Workbooks.Add(xlWBATWorksheet)
    rngFilter.AutoFilter Field:=1, Criteria1:=cell.Value
    rngFilter.EntireRow.Copy
    With wbDest.Sheets(1).Range("A1")
        .PasteSpecial xlPasteColumnWidths
        .PasteSpecial xlPasteValuesAndNumberFormats
        .PasteSpecial xlPasteFormats
    End With
    Application.CutCopyMode = True
    wbDest.Sheets(1).Name = cell.Value
    Application.DisplayAlerts = False
    wbDest.SaveAs ThisWorkbook.Path & Application.PathSeparator & cell.Value & " " & Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mm-yy")
    wbDest.Close False
    Application.DisplayAlerts = True
Next cell

rngFilter.Parent.AutoFilterMode = False
Application.ScreenUpdating = True

End Sub
尝试编辑此行:

Set rngUniques = Range(sColumn & "2", Range(sColumn & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)


这两个版本的代码对我来说都很好。
Set rngUniques = Range(sColumn & "2", sColumn & Cells(Rows.Count, "M").End(xlUp).Row).SpecialCells(xlCellTypeVisible)