Code error helppp!

Gavin Harrison

New Member
Joined
May 2, 2017
Messages
22
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.
 

mole999

Moderator
Joined
Oct 23, 2004
Messages
9,879
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
 

Gavin Harrison

New Member
Joined
May 2, 2017
Messages
22
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
 

MARK858

Well-known Member
Joined
Nov 12, 2010
Messages
11,345
Office Version
365, 2010
Platform
Windows, Mobile
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
 

Gavin Harrison

New Member
Joined
May 2, 2017
Messages
22
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
 

MARK858

Well-known Member
Joined
Nov 12, 2010
Messages
11,345
Office Version
365, 2010
Platform
Windows, Mobile
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
 

Forum statistics

Threads
1,078,447
Messages
5,340,345
Members
399,370
Latest member
salamon

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top