我想更改Worksheet的名称取决于我拥有的文件。它应该有" File1"或" File2"工作表。但我收到错误"超出范围"当我尝试使用工作表的更改名称时,这意味着if语句中的代码不起作用,请帮我解决这个问题。
Dim wsSheet As Worksheet
Dim wsSheet2 As Worksheet
On Error Resume Next
Set wsSheet = Sheets("File1")
Set wsSheet2 = Sheets("File2")
On Error GoTo 0
If Not wsSheet Is Nothing Then
Sheets("Sheet1").Name = "Sheet1ChangedFile1"
Sheets("Sheet2").Name = "Sheet2ChangedFile1"
Else
If Not wsSheet2 Is Nothing Then
Sheets("Sheet1").Name = "Sheet1ChangedFile2"
Sheets("Sheet2").Name = "Sheet2ChangedFile2"
End If
End If
Sheets("Sheet1ChangedFile2").Select 'Here I get an error
答案 0 :(得分:1)
问题是Else声明,如果你完成第一个if语句,你永远不会到达else部分,所以请改用它:
Dim wsSheet As Worksheet
Dim wsSheet2 As Worksheet
On Error Resume Next
Set wsSheet = Sheets("File1")
Set wsSheet2 = Sheets("File2")
On Error GoTo 0
If Not wsSheet Is Nothing Then
Sheets("Sheet1").Name = "Sheet1ChangedFile1"
Sheets("Sheet2").Name = "Sheet2ChangedFile1"
End If 'Changed the Else to End If
If Not wsSheet2 Is Nothing Then
Sheets("Sheet1").Name = "Sheet1ChangedFile2"
Sheets("Sheet2").Name = "Sheet2ChangedFile2"
End If
Sheets("Sheet1ChangedFile2").Select