Code error helppp!

Gavin Harrison

New Member
Joined
May 2, 2017
Messages
34
Hi All.

I have some code below, which always errors on the paste special line. If anyone can help i would appreciate it.

It basically copys data from a hidden sheet and pastes the values into another sheet (which is determinted based on the value of a cell).

Sub Export()
If MsgBox("Are you sure, this will overwrite any existing data?", vbYesNo) = vbNo Then Exit Sub
ActiveWorkbook.Unprotect "aladdin"
Sheets("Calculations").Visible = True
Sheets("Calculations").Select
Range("ac69:af165").Copy
Sheets(Range("aa69").Value).Select
ActiveSheet.Unprotect "aladdin"
Range("A12").PasteSpecial Paste:=xlValues
ActiveSheet.Protect "aladdin"
Sheets("Calculations").Visible = False
ActiveWorkbook.Protect "aladdin", Structure:=True, Windows:=False
Range("A1:G1").Select
End Sub

Thanks in advance for any help.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
try
Code:
Sub Export()
    If MsgBox("Are you sure, this will overwrite any existing data?", vbYesNo) = vbNo Then Exit Sub
    ActiveWorkbook.Unprotect "aladdin"
    With Sheets("Calculations")
        .Visible = True
        .Select
        .Range("ac69:af165").Copy
        .Range("aa69").Value.Select
        .Unprotect "aladdin"
        .Range("A12").PasteSpecial Paste:=xlValues
        .Protect "aladdin"
    End With
    Sheets("Calculations").Visible = False
    ActiveWorkbook.Protect "aladdin", Structure:=True, Windows:=False
    Range("A1:G1").Select
End Sub

I have no idea where the last range select refers to
 
Upvote 0
Hi.

That code errors at the below section, do you think its beacuse if doesnt refer to it being a sheet that needs selecting defined by the contents of cell aa69?

.Range("aa69").Value.Select
 
Upvote 0
What happens with

Code:
Sub Export()
    If MsgBox("Are you sure, this will overwrite any existing data?", vbYesNo) = vbNo Then Exit Sub
    ActiveWorkbook.Unprotect "aladdin"
    
    Sheets("Calculations").Range("ac69:af165").Copy
    With Sheets(Sheets("Calculations").Range("aa69").Value)
        .Unprotect "aladdin"
        .Range("A12").PasteSpecial Paste:=xlValues
        .Protect "aladdin"
    End With
    
    ActiveWorkbook.Protect "aladdin", Structure:=True, Windows:=False
    Range("A1:G1").Select
End Sub
 
Upvote 0
Hi Mark.

it still errors at the paste line. I have got it to run the full sequence with this below code, but its not actually pasting the range?

Thanks

Sub Export()
If MsgBox("Are you sure, this will overwrite any existing data?", vbYesNo) = vbNo Then Exit Sub
ActiveWorkbook.Unprotect "aladdin"
With Sheets("Calculations")
.Visible = True
.Select
.Range("ac69:af165").Copy
Sheets(Range("aa69").Value).Select
.Unprotect "aladdin"
.Range("A12").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
.Protect "aladdin"
End With
Sheets("Calculations").Visible = False
ActiveWorkbook.Protect "aladdin", Structure:=True, Windows:=False
Range("A1:G1").Select
End Sub
 
Upvote 0
it still errors at the paste line.

Strange it doesn't for me.

What does the error state?
What does the code below produce (copy the result to the thread, do not retype it)?

Code:
Sub Export2()
    If MsgBox("Are you sure, this will overwrite any existing data?", vbYesNo) = vbNo Then Exit Sub
    ActiveWorkbook.Unprotect "aladdin"
    
    With Sheets(Sheets("Calculations").Range("aa69").Value)
       Debug.Print Sheets(Sheets("Calculations").Range("aa69").Value).Name
    End With
    
    ActiveWorkbook.Protect "aladdin", Structure:=True, Windows:=False
 
End Sub

and what happens with

Code:
Sub Export3()
    If MsgBox("Are you sure, this will overwrite any existing data?", vbYesNo) = vbNo Then Exit Sub
    ActiveWorkbook.Unprotect "aladdin"

    With Sheets(Sheets("Calculations").Range("aa69").Value)
        .Unprotect "aladdin"
        Sheets("Calculations").Range("ac69:af165").Copy
        .Range("A12").PasteSpecial Paste:=xlValues
        .Protect "aladdin"
    End With

    ActiveWorkbook.Protect "aladdin", Structure:=True, Windows:=False
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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