Excel VBA怎么判断 一个sheet是否存在 如果不存在则创建他

2024-12-20 17:53:46
推荐回答(3个)
回答1:

Sub run()
On Error GoTo 999
Sheets("sheet1").Select
Exit Sub
999
Sheets.Add
ActiveSheet.Name = "sheet1"
End Sub

回答2:

On Error Resume Next
MkDir "D:\xxx" ' 不存在就建一个,已存在则跳到下一句

回答3:

function CheckSheet(sName as string) as boolean
dim ws as worksheet
on error goto TT
set ws=thisworkbook.worksheets(sName)
checksheet=ture 'worksheet exists
exit function
TT:
checksheet=false 'no sheet found
end function
function AddSheet()
dim ws as worksheet,sKey as string
skey="kk"
if checksheet(skey)=false then
set ws=thisworkbook.worksheet.add
ws.name=skey
end if
end function