+ Reply to Thread
Results 1 to 6 of 6

Simple VBA code to move, copy, paste, and protect

Hybrid View

clifton1230 Simple VBA code to move,... 03-11-2013, 06:56 PM
arlu1201 Re: Simple VBA code to move,... 03-12-2013, 03:31 AM
clifton1230 Re: Simple VBA code to move,... 03-12-2013, 10:31 AM
arlu1201 Re: Simple VBA code to move,... 03-12-2013, 01:28 PM
clifton1230 Re: Simple VBA code to move,... 03-12-2013, 05:28 PM
arlu1201 Re: Simple VBA code to move,... 03-13-2013, 11:35 AM
  1. #1
    Registered User
    Join Date
    02-18-2013
    Location
    Dallas
    MS-Off Ver
    Excel 2010
    Posts
    17

    Simple VBA code to move, copy, paste, and protect

    I'm new to visual basic so I would assume that this is fairly simple.

    I need a macro that will unlock my current workbook (just the workbook, not the sheet), then move, copy, and paste values (only) the first two tabs to a new workbook. Then I need it to go back to the original workbook and re-lock it.

    I was able to get the unprotect, move and copy to new sheet (still need the paste values bit), but when I use the ActiveWorkbook.protect function, it locks the new workbook. I need to new workbook unproteted and the old one to lock back.

    I'm assuming I need to reference the file name in order to re-lock the workbook. How can I do that if my file name changes weekly?

    ActiveWorkbook.Unprotect "password"
        Sheets("#1 Weekly Report").Select
        Sheets("#1 Weekly Report").Copy
        ActiveWorkbook.Protect "password", Structure:=True, Windows:=False
    Thank you -

    Cliff

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Simple VBA code to move, copy, paste, and protect

    Do you open the file through the macro? The file that needs to be protected again?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    02-18-2013
    Location
    Dallas
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Simple VBA code to move, copy, paste, and protect

    So while looking at this I realized that the workbook doesn't have to be unlocked to copy and paste the values. Eventually I will lock it down so they cannot see locked cells, but that will be later.

    When recording the macro here's the code that generates:

    Sub Copy2()
    '
    ' Copy2 Macro
    ' Copy and paste values into new workbook
    '
    
    '
        Workbooks.Add
        Windows("Copy of WR_CPF_3-10-2013.xlsm").Activate
        Cells.Select
        Selection.Copy
        Windows("Book5").Activate
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Range("A6:K6").Select
        Windows("Copy of WR_CPF_3-10-2013.xlsm").Activate
        Sheets("#2 Market Survey").Select
        Cells.Select
        Range("A2").Activate
        Application.CutCopyMode = False
        Selection.Copy
        Windows("Book5").Activate
        Sheets("Sheet2").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Windows("Copy of WR_CPF_3-10-2013.xlsm").Activate
        Sheets("Comp Sheet").Select
        Cells.Select
        Application.CutCopyMode = False
        Selection.Copy
        Windows("Book5").Activate
        Sheets("Sheet3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    End Sub
    The problem is it is referencing a specific workbook ("Copy of WR_CPF_3-10-2013.xlsm" and "Book5.xlsx"). This is a weekly report so the file name changes week to week. I need it to recognize the current file name and use that as the "active workbook" and I need to lose the "Book 5" name because it will not always be "Book 5."

    Does all this make sense?

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Simple VBA code to move, copy, paste, and protect

    So is this workbook Copy of WR_CPF_3-10-2013.xlsm already open at the time of running the macro?

    Is this macro in this workbook or in another workbook?

  5. #5
    Registered User
    Join Date
    02-18-2013
    Location
    Dallas
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Simple VBA code to move, copy, paste, and protect

    The macro will be built into the "Copy of WR_CPF_3-10-2013.xlsm" workbook. But that file extension will change week to week. For example, when our employees update this workbook next week, they will save it as "WR_CPF_3-17-2013.xlsm." So I need the macro to reference the active workbook file extension each time it is run.

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Simple VBA code to move, copy, paste, and protect

    In that case, it will be better if you keep the code in one central workbook and then have the workbook open the file required.

    This way, if you need to make changes to the macro in the future, you do not have to edit the codes in all the files.

+ 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