• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

VBA to copy a new sheet based and then access new sheet with a variable |VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / VBA to copy a new sheet based and then access new sheet with a variable |VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosVBA to copy a new sheet based and t…
sp_PrintTopic sp_TopicIcon
VBA to copy a new sheet based and then access new sheet with a variable
Avatar
Kenneth McMillan
USA
Member
Members

Dashboards

Power Query
Level 0
Forum Posts: 29
Member Since:
November 15, 2017
sp_UserOfflineSmall Offline
1
February 6, 2018 - 1:53 pm
sp_Permalink sp_Print sp_EditHistory

I am trying to copy a new sheet based on the active sheet and rename it using an input box. I then want to copy a sheet named "January" and copy and paste that data into the worksheet I just created, but it needs to be a variable named worksheet as I add a new sheet each month.
In the code below, "March" in this case will be named to April for the following month.
I hope this explanation makes sense. I can provide the workbook if needed.

Can someone please help. Here is what I have so far.
Thanks in advance,
Ken Mc

Sub InsertDuplicateSheet() 
    ActiveSheet.Copy After:=ActiveSheet 
    ActiveSheet.Name = InputBox("Please insert the name of the new sheet.", "Rename Sheet") 
    Sheets("January").Select 
    Range("A1").Select 
    Range(Selection, Selection.End(xlToRight)).Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Selection.Copy 
    Sheets("March").Select 
    Range("A1").Select 
    ActiveSheet.Paste 
    Range("B4").Select 
     
End Sub 

Avatar
SunnyKow
Puchong, Malaysia

VIP
Members


Trusted Members
Level 8
Forum Posts: 1432
Member Since:
June 25, 2016
sp_UserOfflineSmall Offline
2
February 7, 2018 - 10:36 am
sp_Permalink sp_Print

Hi Kenneth

Try this. I added the code in red.

Sub InsertDuplicateSheet()
ActiveSheet.Copy After:=ActiveSheet
ActiveSheet.Name = InputBox("Please insert the name of the new sheet.", "Rename Sheet")
'Save the newly created sheet name to a variable
NewShtName = ActiveSheet.Name
Sheets("January").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Copy to the newly created sheet
Sheets(NewShtName).Select
Range("A1").Select
ActiveSheet.Paste
Range("B4").Select
End Sub

Hope this helps.

Sunny

Avatar
David_Ng
Member
Members
Level 0
Forum Posts: 306
Member Since:
December 5, 2016
sp_UserOfflineSmall Offline
3
February 7, 2018 - 3:49 pm
sp_Permalink sp_Print

Cannot run the VB, prompt syntax error

Pls try store to a worksheet and run to see if problem can be fixed

Avatar
SunnyKow
Puchong, Malaysia

VIP
Members


Trusted Members
Level 8
Forum Posts: 1432
Member Since:
June 25, 2016
sp_UserOfflineSmall Offline
4
February 7, 2018 - 4:07 pm
sp_Permalink sp_Print sp_EditHistory

Hi David

You need 1 default sheet and another sheet named January.

The modified code is based on my understanding on what the OP wanted and also what the original code is doing.

Without the actual file with some sample data, this is all that I can do (making a guess and hope for the best Kiss)

Sunny

Avatar
Kenneth McMillan
USA
Member
Members

Dashboards

Power Query
Level 0
Forum Posts: 29
Member Since:
November 15, 2017
sp_UserOfflineSmall Offline
5
February 8, 2018 - 1:03 am
sp_Permalink sp_Print

Thanks David and Sunny!!

I really appreciate your help. I added your variable as a string and changed:

 NewShtName = ActiveSheet.Name

and everything worked perfectly.

Thanks again,

Ken Mc

Avatar
SunnyKow
Puchong, Malaysia

VIP
Members


Trusted Members
Level 8
Forum Posts: 1432
Member Since:
June 25, 2016
sp_UserOfflineSmall Offline
6
February 8, 2018 - 9:25 am
sp_Permalink sp_Print

Thanks for the feedback Kenneth.

Glad we are able to help.

Sunny

Avatar
David_Ng
Member
Members
Level 0
Forum Posts: 306
Member Since:
December 5, 2016
sp_UserOfflineSmall Offline
7
February 8, 2018 - 12:27 pm
sp_Permalink sp_Print

Ken,

If you don't mind can you upload the Worksheet with the VB  for our reference , of course all sensitive or cret data to be 

covered-up or twisted as purely for testing application objective.

Avatar
Kenneth McMillan
USA
Member
Members

Dashboards

Power Query
Level 0
Forum Posts: 29
Member Since:
November 15, 2017
sp_UserOfflineSmall Offline
8
February 9, 2018 - 3:50 am
sp_Permalink sp_Print

Of course not David. It's just chart I made to track the problem I was having with my feet and hands. I'm happy to say since the doc put me on a steroid, I've made great progress since late November. 🙂Laugh

Avatar
David_Ng
Member
Members
Level 0
Forum Posts: 306
Member Since:
December 5, 2016
sp_UserOfflineSmall Offline
9
February 9, 2018 - 9:47 am
sp_Permalink sp_Print

Thanks millions Ken!

Honestly we learn a lot from this forum!

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Philip Treacy, Ngoc Tinh, Riny van Eekelen, Alexandra Radu
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
michael serna
mashal sana
Tiffany Kang
Leah Gillmore
Sopi Yuniarti
LAFONSO HERNANDEZ
Hayden Hao
Angela chen
Sean Moore
John Chisholm
Forum Stats:
Groups: 3
Forums: 24
Topics: 6215
Posts: 27248

 

Member Stats:
Guest Posters: 49
Members: 31897
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.