Vba code for creating and renaming a new sheet

CROY1985

Well-known Member
Joined
Sep 21, 2009
Messages
501
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I need some help in tweaking the code below. I have changed the text colour to red for the parts which i believe are the problem.


Code:
Sub MSvone()
'
' MSvone Macro
    Sheets("Sheet1").Select
    ActiveSheet.ListObjects("Table_Query_from_Sage_Accounts_2011_1").Range. _
        AutoFilter Field:=1, Criteria1:="=*00", Operator:=xlOr, Criteria2:= _
        "=*all*"
    Cells.Select
    Selection.Copy
    [COLOR=red]Sheets.Add After:=Sheets(Sheets.Count)[/COLOR]
    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
        , SkipBlanks:=False, Transpose:=False
[COLOR=red]    Sheets("Sheet7").Select[/COLOR]
[COLOR=red]    Sheets("Sheet7").Name = "Marshall Street"[/COLOR]
    Columns("A:A").Select
    Selection.EntireColumn.Hidden = True
End Sub

The second time the macro is run, the new sheet that is created is sheet8. So the part of this code that refers to sheet 7 to rename it, doesnt work.

How can i have the new sheet renamed whichever sheet number excel happens to assign it?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Code:
Sub Test3()
    Dim a As Worksheet
    Set a = Sheets.Add(After:=Sheets(Sheets.count))
    a.Name = "Hi"
End Sub
 
Upvote 0
do i replace the first line i have highlighted in red with what you put and delete the other 2 lines?

Thanks
 
Upvote 0
Here is your whole code
Rich (BB code):
Sub MSvone()
'
' MSvone Macro
    Dim newWS As Worksheet
    Sheets("Sheet1").Select
    ActiveSheet.ListObjects("Table_Query_from_Sage_Accounts_2011_1").Range. _
        AutoFilter Field:=1, Criteria1:="=*00", Operator:=xlOr, Criteria2:= _
        "=*all*"
    Cells.Select
    Selection.Copy
    Set newWS = Sheets.Add(After:=Sheets(Sheets.Count))
    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
        , SkipBlanks:=False, Transpose:=False
    newWS.Name = "Marshall Street"
    Columns("A:A").Select
    Selection.EntireColumn.Hidden = True
End Sub

Btw, you can further reduce your code by removing select.
Rich (BB code):
ActiveSheet.Select
Selection.Blarg

to
Rich (BB code):
ActiveSheet.Blarg

It makes the code more readable and faster.
 
Upvote 0
I have finised with this. I works very well and quickly too. Thank you for your help - it would have took me weeks if i didnt have it.

Code:
Sub createmh()
    
    Selection.AutoFilter
       
        ActiveSheet.ListObjects("Table_Query_from_Sage_Accounts_2011_1").Range. _
        AutoFilter Field:=1, Criteria1:="=*00", Operator:=xlOr, Criteria2:= _
        "=*all*"
    Cells.Select
    Selection.Copy
    Dim a As Worksheet
    Set a = Sheets.Add(After:=Sheets(Sheets.Count))
    a.Name = "Marshall Street"
    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
        , SkipBlanks:=False, Transpose:=False
    Columns("A:A").Select
    Selection.EntireColumn.Hidden = True
         
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,219
Members
452,895
Latest member
BILLING GUY

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