help me revised this vba. copy sheet macro

Dan88

Active Member
Joined
Feb 14, 2008
Messages
275
Office Version
  1. 365
Platform
  1. Windows
hi everyone. I am currently running this macro below that lets me copy a sheet named template to the end and rename it based on a cell value.

Sub End()
Dim ws As Worksheet, wb As Workbook
Dim rngData As Range

Set wb = ActiveWorkbook

With wb.Sheets("Template")
Set rngData = Range(.Range("J2"), .Range("J2").End(xlDown))
End With

For Each cell In rngData
If cell.Value <> "" Then
On Error Resume Next

Set ws = Worksheets(cell.Value)
wb.Sheets("Template").Copy after:=wb.Sheets(wb.Sheets.Count)
wb.Sheets(wb.Sheets.Count).Name = cell.Value
Else
Set ws = Nothing
End If

On Error GoTo 0
End If
Next cell
End If
End Sub


is there something im missing? the code just wouldn't work. the value in J1 is a date.

thanks in advance.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
When you say it won't work, what does it do?

Haven't checked the whole thing but if I was going to name a sheet with a date from a cell, I'd try the following:

Code:
wb.Sheets(wb.Sheets.Count).Name = format(cell.Value,"dd_mm_yyyy")
 
Upvote 0
you can't have "/" in a tab name, so you would need to format the date to something
5-15-2009
 
Upvote 0
You haven't said what's not working, but you can't use End as the name of a procedure and your code doesn't compile. Try:

Code:
Sub Test()
    Dim ws As Worksheet, wb As Workbook
    Dim rngData As Range
    Dim cell As Range
    Set wb = ActiveWorkbook
    With wb.Sheets("Template")
        Set rngData = Range(.Range("J2"), .Range("J2").End(xlDown))
    End With
    For Each cell In rngData
        If cell.Value <> "" Then
            On Error Resume Next
            Set ws = Worksheets(cell.Value)
            If ws Is Nothing Then
                wb.Sheets("Template").Copy after:=wb.Sheets(wb.Sheets.Count)
                wb.Sheets(wb.Sheets.Count).Name = cell.Value
            Else
                Set ws = Nothing
            End If
            On Error GoTo 0
        End If
    Next cell
End Sub
 
Upvote 0
Thanks!
that worked however, it copies 2 sheets now. One named main(2) and one with cell value(date).

how do i fix this?
is there a way i can have the date appear in alpha numeric (ie. March_01_2009?

thanks
 
Upvote 0
excelR8R/texasalynn/Andrew Poulsom,
i modified the code and it works and i figured out how to get the format to show alpha.
But the macro is copying 2 sheets every time it runs. It makes the sheet and names it after cell j2 and then copies another one.

how do i edit this to get it only to copy 1 sheet?

Thanks
 
Upvote 0
Sub copy()
Dim ws As Worksheet, wb As Workbook
Dim rngData As Range
Dim cell As Range
Set wb = ActiveWorkbook
With wb.Sheets("main")
Set rngData = Range(.Range("J2"), .Range("J2").End(xlDown))
End With
For Each cell In rngData
If cell.Value <> "" Then
On Error Resume Next
Set ws = Worksheets(cell.Value)
If ws Is Nothing Then
wb.Sheets("main").copy after:=wb.Sheets(wb.Sheets.Count)
wb.Sheets(wb.Sheets.Count).Name = Format(cell.Value, "MMM-yy")
Else
Set ws = Nothing
End If

End If
Next
End Sub
 
Upvote 0
I think you need to change this
wb.Sheets(wb.Sheets.Count).Name = Format(cell.Value, "MMM-yy")
to this
ActiveSheet.Name = Format(cell.Value, "MMM-yy")
 
Upvote 0

Forum statistics

Threads
1,216,312
Messages
6,130,009
Members
449,550
Latest member
8073662045

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