• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

IF statement Through VBA

HSA1

New Member
Converting this below code into an if statement.

For example: If Sheet2 is not exist, code will copy data from Sheet1 and create sheet2 then will paste data in Sheet2 and will return to Sheet1.

If Sheet2 is exist then Nothing.3

Any help you could provide would be much appreciated

When i run this code again it gives error If Sheet2 is already exist then it should return nothing
72536
Code:
Sub sheet()
Dim Destination As Worksheet
Set Destination = Worksheets.Add(After:=Worksheets("Sheet1"))
Destination.Name = "Sheet2"
Sheets("sheet1").Range("A:D").Copy Sheets("sheet2").Range("A:D")
Sheets("Sheet1").Select
   Range("A1").Select
End Sub
 
Last edited:
Use ISREF worksheet function like in this sample :​
Another one :​
 
Sir thank you that you replied i would like to say in this regard i have tried according to your given examples but failed.

My question is simple that if i press or run the code again why this error is appear.

72537

How to eliminate this from code.

If Sheet has created and then again i run the code multiple times it should be like nothing is happening. I want this and i do not know how to make this code as i said.
 
I have spent more than 4 hours constant to make this work.

But i do not know what is wrong with my statement.

If sheet is <> "Sheet2" then add sheet otherwise go to ThisWorkbook.Sheets(Sheet1).Range(a1).Select.

Someone can please make this correct.

Code:
Sub shtnew()

Dim wb As Workbook
Dim ws As Worksheet

Set wb = ActiveWorkbook

For Each ws In wb.Worksheets

    If ws.Name <> "Sheet2" Then
    Set ws = Worksheets.Add(after:=Worksheets("Sheet1"))
    ws.Name = "Sheet2"
 
    Else
 ThisWorkbook.Sheets(Sheet1).Range(a1).Select
    End If
    

Next
End Sub
 
Back
Top