VBA - How to call a new tab with variable name

PGD15

Board Regular
Joined
Aug 5, 2016
Messages
137
Hi,

I've written some code in VBA which creates a new tab based on a cell value. As you can imagine this cell value can change.

After creating this tab i wanted to paste some information into it. However, I cannot call the tab; i'm getting error 9 subscript out of range. Or Object required depending on how i tweak the code

Code:
Sub Save_data2()
'Copy A Range of Data
  Worksheets("ROI - Post Visit").Range("J30").Copy
  
'PasteSpecial Values Only
  Worksheets("ROI - Post Visit").Range("S2").PasteSpecial Paste:=xlPasteValues
 
Dim TabName As Range
Set TabName = Worksheets("ROI - Post Visit").Range("S2")


'Creat New Tab with name of property
Sheets.Add(After:=Sheets(Sheets.Count)).Name = TabName


Dim NewSheet As Worksheet
Set NewSheet = Worksheets("ROI - Post Visit").Range("S12").Value


'copy worksheet
Worksheets("ROI - Post Visit").Range("D2:R34").Copy


'Paste
Worksheets("NewSheet").Range("D2:R34").Paste
Worksheets("NewSheet").Range("D2:R34").Copy
Worksheets("NewSheet").Range("D2:R34").PasteSpecial Paste:=xlPasteValues
 
Last edited:
Bingo x2!

I had to swap the paste values and formats due to merged cells but working like a treat :) many thanks.
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You're welcome & thanks for the feedback.

I would also recommend getting rid of merged cells, they will only cause you a lot of grief & hassle.
 
Upvote 0
1 last thing sorry, is it possible to combo and past the values of a combo box.

IE cell K2 is a combo box with 2 options. Can i paste this box with both option into the new tab?
 
Upvote 0
I would suggest that you start a new thread for this.
I never use comboboxes on sheets
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
for anyone reading this thread, i just had to add .PasteSpecial xlPasteValidation along with the other paste options....
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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