Code error helppp!

Gavin Harrison

New Member
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
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
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
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
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
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
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top