excel vba如果找不到Sheets页就创建并跳转
答案:5 悬赏:20 手机版
解决时间 2021-04-14 23:05
- 提问者网友:原来太熟悉了会陌生
- 2021-04-14 05:08
excel vba如果找不到Sheets页就创建并跳转
最佳答案
- 五星知识达人网友:行雁书
- 2021-04-14 06:03
使用如下代码将实现,输入一个名称,如果存在名为该输入值的sheet则跳转,不存在则创立后跳转。
Sub SheetSelect()
On Error GoTo 1
Dim mySheet As String
mySheet = Application.InputBox("输入要寻找的sheet", Type:=1)
Worksheets(mySheet).Select
Exit Sub
1:
Sheets.Add
Worksheets(Worksheets.Count).Name = mySheet
Worksheets(mySheet).Select
End Sub
Sub SheetSelect()
On Error GoTo 1
Dim mySheet As String
mySheet = Application.InputBox("输入要寻找的sheet", Type:=1)
Worksheets(mySheet).Select
Exit Sub
1:
Sheets.Add
Worksheets(Worksheets.Count).Name = mySheet
Worksheets(mySheet).Select
End Sub
全部回答
- 1楼网友:想偏头吻你
- 2021-04-14 10:07
Sub test()
Dim sh As Worksheet, sheetExists As Boolean
'是否已经存在
For Each sh In ThisWorkbook.Worksheets
If sh.Name = "aa" Then
sheetExists = True
Exit For
End If
Next
If sheetExists Then '如果存在
Sheets("aa").Activate
Else
Sheets("bb").Copy , Sheets("bb")
ActiveSheet.Name = "aa"
End If
End Sub
祝你成功!
Dim sh As Worksheet, sheetExists As Boolean
'是否已经存在
For Each sh In ThisWorkbook.Worksheets
If sh.Name = "aa" Then
sheetExists = True
Exit For
End If
Next
If sheetExists Then '如果存在
Sheets("aa").Activate
Else
Sheets("bb").Copy , Sheets("bb")
ActiveSheet.Name = "aa"
End If
End Sub
祝你成功!
- 2楼网友:独行浪子会拥风
- 2021-04-14 09:21
Sub aaAdd()
Dim i As Integer
For i = 1 To Sheets.Count
If Sheets(i).Name = "aa" Then
MsgBox ("aa has been here")
Exit Sub
End If
Next i
Sheets("bb").Copy After:=Sheets("bb")
ActiveSheet.Name = "aa"
End Sub
Dim i As Integer
For i = 1 To Sheets.Count
If Sheets(i).Name = "aa" Then
MsgBox ("aa has been here")
Exit Sub
End If
Next i
Sheets("bb").Copy After:=Sheets("bb")
ActiveSheet.Name = "aa"
End Sub
- 3楼网友:罪歌
- 2021-04-14 07:51
Sub yyTEST()
Dim yoSHT As Worksheet, ysNME As String
' [yoSHT]: 设置为[工作表]对象变量。
' [ysNME]: 设置为[工作表名称]。
Application.ScreenUpdating = False
On Error Resume Next
ysNME = "aa"
Set yoSHT = ActiveWorkbook.Sheets(ysNME)
If yoSHT Is Nothing Then
Sheets("bb").Copy after:=Sheets("bb")
ActiveSheet.Name = "aa"
Else
MsgBox ysNME & " 工作表已存在!"
End If
End Sub
(其他三位的答案均有异曲同工之效)
Dim yoSHT As Worksheet, ysNME As String
' [yoSHT]: 设置为[工作表]对象变量。
' [ysNME]: 设置为[工作表名称]。
Application.ScreenUpdating = False
On Error Resume Next
ysNME = "aa"
Set yoSHT = ActiveWorkbook.Sheets(ysNME)
If yoSHT Is Nothing Then
Sheets("bb").Copy after:=Sheets("bb")
ActiveSheet.Name = "aa"
Else
MsgBox ysNME & " 工作表已存在!"
End If
End Sub
(其他三位的答案均有异曲同工之效)
- 4楼网友:何以畏孤独
- 2021-04-14 06:49
Sub tx()
Dim x As Integer
For x = 1 To Sheets.Count
If Sheets(x).Name = "aa" Then
MsgBox ("aa表已存在")
End
End If
Next x
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Select
Sheets(Sheets.Count).Name = "aa"
End Sub追问谢谢,我光想着怎么判断一个sheet是否存在了,没想到可以全部筛查一遍……新思路,感谢。同时期待其他答案,是否可以直接判断某sheet是否存在,比如Is Nothing之类的。追答
可以用错误来判断Sub tx1()
On Error Resume Next
Sheets("aa").Select
If Err <> Empty Then
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Select
Sheets(Sheets.Count).Name = "aa"
Else
MsgBox ("aa表已存在")
End If
On Error GoTo 0
End Sub
Dim x As Integer
For x = 1 To Sheets.Count
If Sheets(x).Name = "aa" Then
MsgBox ("aa表已存在")
End
End If
Next x
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Select
Sheets(Sheets.Count).Name = "aa"
End Sub追问谢谢,我光想着怎么判断一个sheet是否存在了,没想到可以全部筛查一遍……新思路,感谢。同时期待其他答案,是否可以直接判断某sheet是否存在,比如Is Nothing之类的。追答
可以用错误来判断Sub tx1()
On Error Resume Next
Sheets("aa").Select
If Err <> Empty Then
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Select
Sheets(Sheets.Count).Name = "aa"
Else
MsgBox ("aa表已存在")
End If
On Error GoTo 0
End Sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯