Results 1 to 6 of 6

Thread: [RESOLVED] [EXCEL] Import Sheets from Protected Workbook into New Workbook, Single Sheet

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Resolved [RESOLVED] [EXCEL] Import Sheets from Protected Workbook into New Workbook, Single Sheet

    Basically, I have 52 weeks' worth of data in one workbook, each week in a different worksheet. Each worksheet consists of the same number of columns. Each worksheet has the exact same header row, which is not to be included. I want to take the data from each worksheet and place it in a separate workbook.

    However, the original workbook containing the 52 sheets is protected. I am able to manually select, copy and paste cells into the new workbook without a problem. When I run my script (see below), it gives me an error about how I cannot do the
    Code:
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    without first removing protection from the worksheet. My boss gave me the worksheet, but doesn't remember/know the password.

    I wrote the following to loop through all of the worksheets in the workbook. Each time it starts the loop on a worksheet, it msgbox asks if you want to include the current worksheet. If you say no, it goes to the next worksheet. If you say yes, it selects the desired range (A2 to the lower-right cell), copies, and pastes into the other workbook. Arkansas2009.xls is the original, multi-sheet workbook and Arkansas2009_compiled.xls is the destination workbook.

    Any help on how to work around this error would be greatly appreciated. Here's the code:

    Code:
    Sub GetValues()
    '
    ' GetValues Macro
    '
    
    '
       
       
       Windows("Arkansas2009.xls").Activate
    For Each ws In Worksheets
       Worksheets(ws.Name).Activate
       Dim Msg1, Style1, Title1, Response1, Compiled As Integer, Continue As Integer
       Msg1 = "Do you wish to include this worksheet?"
       Style1 = vbYesNo
       Title1 = "Compilation confirmation"
       Response1 = MsgBox(Msg1, Style1, Title1)
       If Response1 = vbYes Then
            Range("A2").Select
            Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
            'Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
            Selection.Copy
            Windows("Arkansas2009_compiled.xls").Activate
            ActiveCell.SpecialCells(xlLastCell).Select
            Selection.End(xlToLeft).Select
            ActiveSheet.Paste
            Compiled = 1
        
        Else
            Compiled = 0
        End If
        
        'MsgBox ws.Name
        
        If Compiled = 1 Then
            Dim Msg, Style, Title, Response
            Msg = "Worksheet has been compiled. Do you wish to continue?"
            Style = vbYesNo
            Title = "Worksheet Compilation"
            Response = MsgBox(Msg, Style, Title)
            If Response = vbYes Then
                Continue = 1
            ElseIf Compiled = 0 Then
            Dim Msg2, Style2, Title2, Response2
            Msg2 = "Okay, would you like to try the next worksheet?"
            Style2 = vbYesNo
            Title2 = "Continue on or not?"
            Response2 = MsgBox(Msg2, Style2, Title2)
            If Response2 = vbYes Then
                Continue = 1
                Else
                Continue = 0
            End If
                
            End If
        Else
        
        End If
        
        If Continue = 1 Then
        Else
        Exit For
        End If
    Next ws
    
    End Sub
    Last edited by Cristobal16; Oct 13th, 2009 at 03:32 PM. Reason: Change title

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [EXCEL] Import Sheets from Protected Workbook into New Workbook, Single Sheet

    Try this...

    Instead of

    Range("A2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    'Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    replace it with

    vb Code:
    1. Range(Range("A2"), Range("A2").SpecialCells(xlLastCell)).Copy
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: [EXCEL] Import Sheets from Protected Workbook into New Workbook, Single Sheet

    No, that didn't work, it still gave me the error:

    "Run-time error '1004':
    You cannot use this command on a protected sheet. TO use this command, you must first unprotect the sheet (Review tab, Changes group, Unprotect Sheet button). You may be prompted for a password."

    Also, when the MsgBox prompts user to choose if they wish to include the worksheet and the user selects No, instead of continuing on to the next worksheet the process just ends... Not sure why.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: [EXCEL] Import Sheets from Protected Workbook into New Workbook, Single Sheet

    All right, I fixed the continuation prompt issue, so the process now works smoothly when asking if you want to include the worksheet and move on to the next, but I'm still getting this:

    "Run-time error '1004':
    You cannot use this command on a protected sheet. To use this command, you must first unprotect the sheet (Review tab, Changes group, Unprotect Sheet button). You may be prompted for a password."

    When I choose Debug, it sends me straight to:

    Code:
    Range(Range("A2"), Range("A2").SpecialCells(xlLastCell)).Copy
    Again, is there any way that I can do this (or something equivalent) without unprotecting the sheet? I can do it by hand, and when I record the macro by hand, this is the code that it gives me:

    Code:
    Range("A2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Unfortunately, this causes the same error as what Koolsid suggested. Any help is greatly appreciated. Thanks!

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [EXCEL] Import Sheets from Protected Workbook into New Workbook, Single Sheet

    If you would have known the password then it would have been easy...

    anyways try this....

    Replace

    Range(Range("A2"), Range("A2").SpecialCells(xlLastCell)).Copy
    by

    Code:
        Range(Range(Range("A2"), Range("A2").End(xlToRight)), Range(Range("A2"), _
        Range("A2").End(xlToRight)).End(xlDown)).Copy
    Now give it a try...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: [EXCEL] Import Sheets from Protected Workbook into New Workbook, Single Sheet

    Yeah, I know, that's what I told my boss. Anyway, that worked great! Thanks!!!

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width