+ Reply to Thread
Results 1 to 2 of 2

problem with macro.. help needed

Hybrid View

  1. #1
    Registered User
    Join Date
    02-01-2012
    Location
    Switzerland
    MS-Off Ver
    Excel 2003
    Posts
    1

    problem with macro.. help needed

    Hi I'm new here. I hope someone can help me with my strange problem..

    I have a macro that creates a statistic at the end of the month by sorting out the relevant sheets and putting them into a new workbook. Now when I run this macro it doesn't save a new file and it also deletes all defined names in the workbook. I have no idea why this happens.

    I'm using the following code:






    Sub CreateStat()

    Dim NewName As String
    Dim nm As Name
    Dim ws As Worksheet

    If ThisWorkbook.Worksheets(1).ProtectContents = True Then
    For Each ws In ThisWorkbook.Worksheets
    ws.Unprotect
    Next 'wks
    End If


    If MsgBox("Copy specific sheets to a new workbook" & vbCr & _
    "New sheets will be pasted as values, named ranges removed" _
    , vbYesNo, "NewCopy") = vbNo Then Exit Sub

    With Application
    .ScreenUpdating = False


    ' Copy specific sheets
    ' *SET THE SHEET NAMES TO COPY BELOW*
    ' Array("Sheet Name", "Another sheet name", "And Another"))
    ' Sheet names go inside quotes, seperated by commas

    On Error GoTo ErrCatcher
    For Each wsname In Array("DECKBLATT", "MGB STATS", "HRI STATS", "SRE STATS", "RCO STATS", "JUH AAM STATS", "KST STATS", "DRE STATS", "JCH STATS", "SLO STATS", "MSG STATS", "SVS STATS", "RSB STATS", "USO STATS", "PKR STATS", "SVS STATS", "PBO STATS", "CLG STATS", "ANE STATS", "MAK STATS", "RWA STATS", "YBO STATS", "HPA STATS", "KTZ STATS", "TOTAL")
    Worksheets(wsname).Visible = True
    Next

    Sheets(Array("DECKBLATT", "MGB STATS", "HRI STATS", "SRE STATS", "RCO STATS", "JUH AAM STATS", "KST STATS", "DRE STATS", "JCH STATS", "SLO STATS", "MSG STATS", "SVS STATS", "RSB STATS", "USO STATS", "PKR STATS", "SVS STATS", "PBO STATS", "CLG STATS", "ANE STATS", "MAK STATS", "RWA STATS", "YBO STATS", "HPA STATS", "KTZ STATS", "TOTAL")).Copy

    Sheets(Array("DECKBLATT", "MGB STATS", "HRI STATS", "SRE STATS", "RCO STATS", "JUH AAM STATS", "KST STATS", "DRE STATS", "JCH STATS", "SLO STATS", "MSG STATS", "SVS STATS", "RSB STATS", "USO STATS", "PKR STATS", "SVS STATS", "PBO STATS", "CLG STATS", "ANE STATS", "MAK STATS", "RWA STATS", "YBO STATS", "HPA STATS", "KTZ STATS", "TOTAL")).Visible = False
    On Error GoTo 0



    ' Paste sheets as values
    ' Remove External Links, Hperlinks and hard-code formulas
    ' Make sure A1 is selected on all sheets
    For Each ws In ActiveWorkbook.Worksheets
    ws.Cells.Copy
    ws.[A1].PasteSpecial Paste:=xlValues
    ws.Cells.Hyperlinks.Delete
    Application.CutCopyMode = False
    Cells(1, 1).Select
    ws.Activate
    Next ws
    Cells(1, 1).Select

    ' Remove named ranges
    For Each nm In ActiveWorkbook.Names
    nm.Delete
    Next nm

    ' Input box to name new file
    NewName = InputBox("Please Specify the name of your new workbook", "New Copy")

    ' Save it with the NewName and in the same directory as original
    'Prompt for SaveAs name

    If ThisWorkbook.Worksheets(1).ProtectContents = False Then
    For Each ws In ThisWorkbook.Worksheets
    ws.Protect
    Next 'wks
    End If


    ActiveWorkbook.SaveCopyAs ThisWorkbook.Path & "\" & NewName & ".xls"
    ActiveWorkbook.Close SaveChanges:=False

    .ScreenUpdating = True
    End With
    Exit Sub



    ErrCatcher:
    MsgBox "Specified sheets do not exist within this workbook"

    End Sub

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,429

    Re: problem with macro.. help needed

    Hello, and welcome to the forum. Unfortunately ...

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    PLEASE PM WHEN YOU HAVE DONE THIS AND I WILL DELETE THIS POST

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here


    And I would suggest ...

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook. Don't upload a picture when you have a workbook question. None of us is inclined to recreate your data. Upload the workbook and manually add an 'after' situation so that we can see what you expect. In addition clearly explain how you get the results..
    To attach a file to your post, you need to be using the main 'New Post' or 'New Thread' page and not 'Quick Reply'.
    To use the main 'New Post' page, click the 'Post Reply' button in the relevant thread.

    On this page, below the message box, you will find a button labelled 'Manage Attachments'.
    Clicking this button will open a new window for uploading attachments.

    You can upload an attachment either from your computer or from another URL by using the appropriate box on this page.
    Alternatively you can click the Attachment Icon to open this page.

    To upload a file from your computer, click the 'Browse' button and locate the file.

    To upload a file from another URL, enter the full URL for the file in the second box on this page.
    Once you have completed one of the boxes, click 'Upload'.

    Once the upload is completed the file name will appear below the input boxes in this window.
    You can then close the window to return to the new post screen.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1