I have this Var1="0001"
I need to check if in active workbook exists the sheet named "0001" if not exists copy the sheet SERVICE and rename it with the value of var, in this case "0001"
Printable View
I have this Var1="0001"
I need to check if in active workbook exists the sheet named "0001" if not exists copy the sheet SERVICE and rename it with the value of var, in this case "0001"
try like
Code:fnd = false
for each sht in thisworkbook.sheets
if sht.name = var1 then fnd = true: exit for
next
if not fnd then
sheets("service").copy before:=sheets(1)
sheets(1).name = var1
end if
A slightly faster method.
Code:Sub Sample()
Dim Var1 As String
Dim ws As Worksheet
Var1 = "0001"
On Error Resume Next
Set ws = Sheets(Var1)
On Error GoTo 0
If ws Is Nothing Then
Sheets("service").Copy Before:=Sheets(1)
ActiveSheet.Name = Var1
End If
End Sub