0

I have turned on the "require variable declare" option to force to declare the variable in the VBA setting. After recording a Macro, the following recorded Macro fails to run with an error message:

compile error: expected function or variable.

I have tried to turned off the "require variable declare" in tools->options, then the code runs successfully. How to run it when the option is on?

Option Explicit

Sub Macro2()
'
' Macro2 Macro
'

'
    Range("A1:E4").Select
    selection.Copy
    Range("A16").Select
    activesheet.Paste
End Sub

1 Answer 1

1

VBA is case sensitive. selection.Copy should be Selection.Copy and activesheet.Paste should be ActiveSheet.Paste. Also, unless there is a need to select the cells, you can simplify your code to:

Range("A1:E4").Copy Range("A16")
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for answering. The code is copied from the recorded Macro. I tried to fix it to CAPITAL letter. After click F5, it returns the same error and fix it to lowercase ''selection"'
@XianhaoZHU do you by chance have another Sub named selection?
Are you using Excel's built-in VBA editor or a 3rd party tool?
@BigBen - Excellent question! Another related one would be: Do you have variables named "selection" and/or "activesheet"?
@BigBen yes i do and i figure it out now. - -! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.