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:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try
Code:
Set NewSheet = Sheets(tabname)
 
Upvote 0
Ok, how about
Code:
Set NewSheet = Sheets(TabName.Value)
 
Upvote 0
I get subscript out of range on my Worksheets("NewSheet").Range("D2:R34").Paste line
 
Upvote 0
Hi try to change to
Code:
Dim TabName As String
  TabName = Worksheets("ROI - Post Visit").Range("S2")
 
Upvote 0
Hi Mohadin,

I get invalid qualifier. Using Fluff's method it's picked up but i just can't call the tab when it comes to pasting the data into it.
 
Upvote 0
Replace
Code:
'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
with
Code:
NewSheet.Range("D2:R34").Value = Worksheets("ROI - Post Visit").Range("D2:R34").Value
 
Upvote 0
Bingo, it works! How would I replicate that to copy the column sizes and formatting?

Also side note is it possible to check all current tabs to see if the tab name is already taken. If so how would I do this?

Ie if cell s2 says "tab1" then i try to create a new tab called "tab1" could i have a msgbox pop up informing using this is invalid.

Ie before this line "Sheets.Add(After:=Sheets(Sheets.Count)).Name = TabName" could I call a sub to do the above mentioned validation else do the copy+paste bit.
 
Upvote 0
How about
Code:
Sub PGD15()
   Dim TabName As String
   Dim NewSheet As Worksheet

   With Worksheets("ROI - Post Visit")
      .Range("S2").Value = .Range("J30").Value
      TabName = .Range("S2").Value
   End With
   If Evaluate("isref('" & TabName & "'!A1)") Then
      MsgBox "Sheets exists"
      Exit Sub
   End If
   Sheets.Add(, Sheets(Sheets.Count)).Name = TabName
   Set NewSheet = Sheets(TabName)
   Worksheets("ROI - Post Visit").Range("D2:R34").Copy
   With NewSheet.Range("D2:R34")
      .PasteSpecial xlPasteFormats
      .PasteSpecial xlPasteValues
      .PasteSpecial xlPasteColumnWidths
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,217
Members
448,554
Latest member
Gleisner2

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