VBA to sheets

rspalding

Active Member
Joined
Sep 4, 2009
Messages
295
Office Version
  1. 365
Platform
  1. Windows
I'm using the following code but get a runtime 424 error. Also, I would like to the sheet name in c17 to last cell

Private Sub CommandButton1_Click()
lastcell = ThisWorkbook.Worksheets("Master").Cells(Rows.Count, 1).End(xlUp).Row
For i = 17 To lastcell
With YhisWorkbook
newname = ThisWorkbook.Worksheets("Master").Cells(i, 1).Value
.Sheets.Add after:=.Sheets(.Sheets.Count)
ActiveSheet.Name = newname
End With
Next
ThisWorkbook.Worksheets("Master").Activate
ThisWorkbook.Worksheets("Master").Cells(1, 1).Select
End Sub

Thanks for the help...
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
One error here for starters....

Code:
With YhisWorkbook
 
Upvote 0
Michael M,

Thanks for catching the type-o. I looked multiple times and didn't see it. How can I tell the code to look at the corresponding cell in C column and tell it to copy the sheet named in "C"? So A will have the new sheet name but C will tell it which sheet to copy.

Thanks,
 
Upvote 0
If you put this declaration

Code:
Option Explicit

At the top of your code module ala:
Code:
Option Explicit

Private Sub CommandButton1_Click()
    lastcell = ThisWorkbook.Worksheets("Master").Cells(Rows.Count, 1).End(xlUp).Row
    For i = 17 To lastcell
        With YhisWorkbook
            newname = ThisWorkbook.Worksheets("Master").Cells(i, 1).Value
            .Sheets.Add after:=.Sheets(.Sheets.Count)
            ActiveSheet.Name = newname
        End With
    Next
    ThisWorkbook.Worksheets("Master").Activate
    ThisWorkbook.Worksheets("Master").Cells(1, 1).Select
End Sub

Then the compiler will help you catch errors like "YhisWorkbook" and by forcing you to declare all your variables, it will make it easier for us to help you when you post questions about your code.

BTW, I recommend you use code tags for clarity when posting code
attachment.php

attachment.php
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
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