VBA: Button that create a new worksheets and copy data to the new one

hebbebe

New Member
Joined
Sep 13, 2019
Messages
6
Hello!

Im having a bit of a problem with my code, and it might be very easy for you. Im very new at this.

Problem: I want a commandbutton that create new worksheets. The new worksheets gets its name from the value in CELL B12.
And when it have created the new worksheet, it should copy a range of cells from the mainsheet called "Indata" and paste it in the new sheet.

Code

Private Sub CommandButton1_Click()


Dim a As String
a = ActiveSheet.Cells(12, 2).Value
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = a


Range("A1:J30").copy Worksheet("a").Range("A1")


End Sub

It works with creating the new sheet, but wont paste the data.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi & welcome to MrExcel.
How about
Code:
Sub hebbebe()
   Dim Nme As String
   
   Nme = Range("B12").Value
   Sheets.Add(, Sheets(Sheets.Count)).Name = Nme
   Sheets("Indata").Range("A1:J30").Copy Sheets(Nme).Range("A1")
End Sub
 
Upvote 0
Try this macro
Code:
Option Explicit


Private Sub CommandButton1_Click()


Dim Main_sheet As Worksheet
Set Main_sheet = Sheets("ABCD") ' Change ABCD to your Main sheet's name
Dim a As String                   'i.e The paper we copy from
a = ActiveSheet.Cells(12, 2).Value
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = a
'Main_sheet.Range("A1:J30").Copy  Sheets(a).Range("A1")
  '''''''''''''''''''''  OR
Sheets(a).Range("A1").Resize(30, 10).Value = _
Main_sheet.Range("A1").Resize(30, 10).Value
End Sub
 
Last edited:
Upvote 0
Im still getting the same problem. It creates the worksheet, but it's blank.

Do you get any error messages?
Also is the new sheet named correctly?
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

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