How do I get Excel to copy a range of cells into another cell?

DreyFox

Board Regular
Joined
Nov 25, 2020
Messages
61
Office Version
  1. 2016
Platform
  1. Windows
Hello!

This is the functionality I would like. First the user clicks a command button, which will let them name the sheet that they will create, then copy a certain range of the current sheet into the new sheet keeping its formatting. What I have tried (code):
VBA Code:
Private Sub CommandButton1_Click()
Dim val As String

val = InputBox("Enter the configuration name for the current selection")

Sheets.Add(After:=Sheets(Sheets.Count)).Name = val

Worksheets("Feed Configurator").Range("A108:M109").Copy Worksheets(val)

End Sub
It unfortunately does not work. Any help or assistance would be greatly appreciated! Thank you!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Where in the new sheet should the range go?
 
Upvote 0
Try.
VBA Code:
Worksheets("Feed Configurator").Range("A108:M109").Copy Worksheets(val).[a1]
 
Upvote 0
Try.
VBA Code:
Worksheets("Feed Configurator").Range("A108:M109").Copy Worksheets(val).[a1]
It says "Run time error 1004, Application defined or object defined error"
 
Upvote 0
How about
VBA Code:
Private Sub CommandButton1_Click()
Dim ShtName As Variant

ShtName = InputBox("Enter the configuration name for the current selection")
If ShtName = "" Then Exit Sub
Sheets.Add(After:=Sheets(Sheets.count)).Name = ShtName

Worksheets("Feed Configurator").Range("A108:M109").Copy Worksheets(ShtName).Range("a1")

End Sub
You should not use VBA keywords like Val as the names of variables of procedures.
 
Upvote 0
Solution
How about
VBA Code:
Private Sub CommandButton1_Click()
Dim ShtName As Variant

ShtName = InputBox("Enter the configuration name for the current selection")
If ShtName = "" Then Exit Sub
Sheets.Add(After:=Sheets(Sheets.count)).Name = ShtName

Worksheets("Feed Configurator").Range("A108:M109").Copy Worksheets(ShtName).Range("a1")

End Sub
You should not use VBA keywords like Val as the names of variables of procedures.
Thank you! This works!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Glad we could help & thanks for the feedback.
Sorry for opening this thread again, but is there a way to copy the values only (for example copy the cell values, but not the formulas)?
 
Upvote 0
You can use
VBA Code:
Worksheets(ShtName).Range("A1:M2").Value = Worksheets("Feed Configurator").Range("A108:M109").Value
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,869
Members
449,054
Latest member
juliecooper255

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