VBA代码具有XLSM文档的运行时错误,但不具有XLS

时间:2017-08-29 20:42:44

标签: excel vba excel-vba excel-2013

这个VBA代码在复制和移动XLS文件时对我有用,但是当我尝试在XLSM文件上运行它时,它告诉我运行时错误并且正在突出显示;

" CurrentWB.Sheets(SheetNumber)。选择'选择工作簿中的所有工作表"

运行时错误1004:选择工作表类失败的方法

有谁知道这个问题可能是什么?

 Sub AutoUpdate()

On Error Resume Next
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.PrintCommunication = False

Dim SystemPath As String
Dim FilePath As String
Dim FileName As String
Dim UtilityType As String
Dim ThisName As String
Dim FlattenedFilePath As String
Dim FlattenedFileFolder As String
Dim CurrentWB As Workbook 'Workbook Stores Workbook

SystemPath = Range("Sys.Path")
UtilityType = Range("Utility.Type")
FlattenedFileFolder = Range("Flattened.Files")
FilePath = SystemPath & UtilityType
FileName = Dir(FilePath & "\*.xls")
FlattenedFilePath = FilePath & "\" & FlattenedFileFolder

Do While FileName <> ""

Set CurrentWB = Workbooks.Open(FileName:=FilePath & "\" & FileName, UpdateLinks:=3) 'Sets CurrentWB = to that long name. This becomes the name of the workbook.
    CurrentWB.RunAutoMacros Which:=xlAutoOpen 'Enables Macros in Workbook
    CurrentWB.Save
    ThisName = CurrentWB.Name
        For SheetNumber = 1 To CurrentWB.Sheets.Count 'Counts Worksheets in Workbook
                If (CurrentWB.Sheets(SheetNumber).Name <> "What If") Then
                CurrentWB.Sheets(SheetNumber).Unprotect ("UMC626") 'Unprotects Workbook
                With CurrentWB.Sheets(SheetNumber).UsedRange
                    .Value = .Value
                End With
                CurrentWB.Sheets(SheetNumber).Protect Password:="UMC626", DrawingObjects:=True, Contents:=True, Scenarios:=True 'Protects Workbook
            End If
        Next SheetNumber 'Runs Through Iteration
        CurrentWB.Cells(1, 1).Select 'Saves each workbook at the top of the page
        CurrentWB.SaveAs FileName:=FlattenedFilePath & "\" & ThisName
        CurrentWB.Close 'Closes Workbook

FileName = Dir
Loop

End Sub

0 个答案:

没有答案