VBA for creating a new sheet but staying on previous sheet

Leo55

New Member
Joined
Aug 10, 2022
Messages
18
Office Version
  1. 2010
Platform
  1. Windows
Thought I would be able to do this one, but I over-estimated my abilities.

In my workbook I have one sheet, the title of the sheet changes dependent on the customer reference. -e.g. Cust250885-ref-A485FR etc.

I would like to run a macro for creating a new tab ( when created - it can stay named as sheet1) but I would like the active sheet to stay as the original Customer data as I need the rest of the macro to run on that.

In short how do I create a new sheet but keep the focus on the original sheet but the name of the original sheet can change.

Thank you in advance.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Also if this cannot be done please tell me that I am being too ambitious for my skill level.

Many thanks
 
Upvote 0
In short how do I create a new sheet but keep the focus on the original sheet but the name of the original sheet can change.
the newly created worksheet will automatically become the active sheet but you can turn screen updating off at the beginning of the macro and then back on at the end after making the original sheet active again.

VBA Code:
Sub Test()
    'variable to hold the original active sheet when macro starts
    Dim OrigWS As Worksheet
    Set OrigWS = ActiveSheet
   
    'prevent the screen from updating
    Application.ScreenUpdating = False
    'add the new sheet
    Worksheets.Add After:=Worksheets(Worksheets.Count)
    'name the new sheet
    ActiveSheet.Name = "Whatever"
    'make the original sheet active again
    OrigWS.Activate
    'turn screen updating back on
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
the newly created worksheet will automatically become the active sheet but you can turn screen updating off at the beginning of the macro and then back on at the end after making the original sheet active again.

VBA Code:
Sub Test()
    'variable to hold the original active sheet when macro starts
    Dim OrigWS As Worksheet
    Set OrigWS = ActiveSheet
  
    'prevent the screen from updating
    Application.ScreenUpdating = False
    'add the new sheet
    Worksheets.Add After:=Worksheets(Worksheets.Count)
    'name the new sheet
    ActiveSheet.Name = "Whatever"
    'make the original sheet active again
    OrigWS.Activate
    'turn screen updating back on
    Application.ScreenUpdating = True
End Sub
that has worked perfectly! thank you!
 
Upvote 0

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,089
Latest member
ikke

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