Copy one sheet to another without the ActiveX buttons and without the vba code behind the worksheet

Jim885

Well-known Member
Joined
Jul 8, 2012
Messages
663
I'm using the following code that works well except that it also transfers the code behind the original Master worksheet with it to the newly added worksheet, as well as all the ActiveX controls (two command buttons).
This is not my desired outcome. I just want the data. I noticed that even using value = value still does not remove the code.

Can someone assist?

VBA Code:
Sub CreateSheetWithName()
Dim ws As Worksheet
Dim sheetName As String
Set ws1 = ThisWorkbook.Sheets("Master")
sheetName = ws1.Range("A2").Value & "-" & ws1("Master").Range("E2")
For i = 1 To Worksheets.Count
    If Worksheets(i).Name = ws1.Range("A2").Value & "-" & ws1.Range("E2").Value Then
    End If
Next i
If Not exists Then
    ws1.Copy after:=Worksheets(Sheets.Count)
    Sheets(i).Name = sheetName
    'ActiveSheet.Name = ws1.Range("A2").Value & "-" & ws1.Range("E2").Value  ' accomplishes the same as the line above
        Sheets(sheetName).Shapes("Rounded Rectangle 3").delete
End If

ws1.Activate

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I note that variable exists is neither declared nor set, unless it's global.

Replace your block of code with this:

VBA Code:
Dim NewSheet As Worksheet '''' can go anywhere but I suggest the top of the Sub
If Not exists Then
   Set NewSheet = Worksheets.Add(after:=Worksheets(Worksheets.Count))
   NewSheet.Name = sheetName
   ws1.UsedRange.Copy NewSheet.Range("A1")
End If
 
Upvote 0
VBA Code:
Sub t()
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets("Master")
SheetsAdd After:=Sheets(Sheets.Count)
ws1.UsedRange.Copy
Sheets(Sheets.Count).Range("A1").PasteSpecial xlPasteValues
Sheets(Sheets.count).Name = ws1.Range("A2").Value & ws1.Range(("E2").value
End Sub
 
Upvote 0
Thank you both.
Both solutions work.

Jazzer,
Good catch. Yes, variable was off because I typed it wrong in the post.
VBA Code:
sheetName = ws1.Range("A2").Value & "-" & ws1("Master").Range("E2")
should have been
VBA Code:
sheetname = ws1.Range("A2").Value & "-" & ws1.Range("E2").Value
 
Upvote 0
Thanks for the feedback,
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top