VBA Code to add a sheet after another errors out...

bbbb1234

Board Regular
Joined
Aug 8, 2002
Messages
150
Office Version
  1. 365
Platform
  1. Windows
I am attempting to add a new sheet after a specific sheet. Best I can remember (it has been some time back), it worked in standalone mode in a short test macro but now that I have integrated the code below with the rest of the macro code, it is not working. It compiles fine but at runtime, it errors out - error screenshot below. To debug, I put the code in a test macro, as noted below. Again, it still errors out on the same NOTED line of code. I cannot for the life of me figure this one out.

I am also wide open to any code cleanup that is offered to improve flow, make it run faster, or anything else of the sort.

PLEASE HELP!!!
-----------------------------------------------------------------------------------------------------------------------------
VBA Code:
Sub test()

Dim LastName As String
Dim FirstName As String
Dim FullName As String

Sheets("Patient").Select
LastName = "ALast1"        'this is a dummy line to set the variable in the test macro
FirstName = "AFirst1"'       this is a dummy line to set the variable in the test macro
FullName = LastName & ", " & FirstName
Sheets.Add After:=Sheets("DON'T REMOVE THIS TAB2").Name = FullName       '<-----------------------THIS IS THE LINE THAT THROWS THE SCREENSHOTTED ERROR BELOW
Worksheets("LOG TAB MASTER").Range("a1:XFD1048576").Copy
ActiveSheet.Paste Destination:=Worksheets(LastName & ", " & FirstName) _
    .Range("a1:XFD1048576")
    Sheets(LastName & ", " & FirstName).Select
With ActiveWorkbook.Sheets(LastName & ", " & FirstName).Tab _
    .ColorIndex = xlColorIndexNone
    End With

End Sub

Error Screenshot
Error.png
VBA Code:
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try adding brackets to that line.

VBA Code:
Sheets.Add(After:=Sheets("DON'T REMOVE THIS TAB2")).Name = FullName
 
Upvote 0
I've taken the liberty to rewrite your procedure.

VBA Code:
Sub test()
Dim Wb As Workbook
Dim WsName As Worksheet

    Set Wb = ActiveWorkbook
            
    Wb.Worksheets("LOG TAB MASTER").Copy after:=Wb.Worksheets("DON'T REMOVE THIS TAB2")
    
    Set WsName = ActiveSheet
    
    With WsName
        .Name = "ALast1," & "AFirst1"
        .Tab.ColorIndex = xlColorIndexNone
    End With
    
End Sub
 
Upvote 0
@bbbb1234
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,124
Members
453,021
Latest member
Justyna P

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