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.
 
Try:

Code:
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(Format(cell.Value, "MMM-yy"))
            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 cell
End Sub
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Andrew Poulsom, this is still copying 2 sheets? what giveS?
i spoke to fast, texasalynn code didnt work...

Thanks for your time.
 
Upvote 0
Andrew Poulsom, this is still copying 2 sheets? what giveS?
i spoke to fast, texasalynn code didnt work...

Thanks for your time.

I don't know what gives. To test my code I entered 1 Jan 2009, I Feb 2009 and 1 Mar 2009 (as serial dates) in J2:J5. The code created 3 sheets, Jan-09, Feb-09 and Mar-09 (if they didn't already exist).

What do you have in J2 onwards?
 
Upvote 0
oh. I only need the code to generate one sheet for the month.
J2 onwards are blank.
So next month, i will enter April in J2 and the bba will create a copy of the "main" and rename it april based on value in J2.

No need for multiple sheet like you tested....
hope this is clear.

Thanks
 
Upvote 0
With 1 Jan 2009 in J2 I only get one sheet named Jan-09 (if it didn't already exist). If you only want to use J2 why?

Set rngData = Range(.Range("J2"), .Range("J2").End(xlDown))

and the For Each ... Next loop?
 
Upvote 0
i was able to play around with the code and came up with this:

Sub Copy2End()
Dim ws As Worksheet, wb As Workbook
Dim rngData 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)
wb.Copy after:=wb.Sheets(wb.Sheets.Count)
wb.Sheets(wb.Sheets.Count).Name = Format(cell.Value, "MMM-yy")
Else
Set ws = Nothing
End If

On Error GoTo 0

Next cell
End Sub


The vba only adds one sheet now, however, as i want to add more it replaces the one that was jsut added.
ie. i add Mar09, the go back to main and put Apr09, the macro adds the new sheet but replaces the Mar09 sheet.

driving me insane.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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