help me revised this vba. copy sheet macro

Dan88

Active Member
Joined
Feb 14, 2008
Messages
262
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.
 

Some videos you may like

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Weaver

Well-known Member
Joined
Sep 10, 2008
Messages
5,196
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")
 

texasalynn

Well-known Member
Joined
May 19, 2002
Messages
8,458
you can't have "/" in a tab name, so you would need to format the date to something
5-15-2009
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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
 

Dan88

Active Member
Joined
Feb 14, 2008
Messages
262

ADVERTISEMENT

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
 

Dan88

Active Member
Joined
Feb 14, 2008
Messages
262
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
 

Dan88

Active Member
Joined
Feb 14, 2008
Messages
262
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
 

texasalynn

Well-known Member
Joined
May 19, 2002
Messages
8,458
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")
 

Watch MrExcel Video

Forum statistics

Threads
1,122,915
Messages
5,598,850
Members
414,263
Latest member
sherrcha

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
Top