PC Review


Reply
Thread Tools Rate Thread

Copy a Cell with a macro

 
 
Otter
Guest
Posts: n/a
 
      11th Mar 2008
I am trying to copy a formula from Cell M18 to Cells M19 thru M58 if the
value in Cell F3 is "Y". I get a "Autofill Method of Range class failed"
error. Sometimes it works on column "M" but then gives me an
"Application-defined or object-defined error" error when it tries to do the
same thing in column "U". Here is the code CPY_FORMULA

Thank you in advance for your help.

Application.EnableEvents = False

If (Target.Column = 11 And (Target.Row = 2 Or Target.Row = 3 Or Target.Row =
4)) Then

Worksheets("Data").Unprotect PWORD
Call Analysis_Sales
Worksheets("Data").Unprotect PWORD

End If

If (Target.Column = 6 And Target.Row = 3) Then

Worksheets("Analysis").Unprotect PWORD
Range("F3").Value = UCase(Range("F3").Value)
If Range("F3") <> "Y" Then
Range("F3") = "N"
End If

Call Cpy_Formula

Worksheets("Analysis").Protect PWORD

End If


Application.EnableEvents = True


End Sub

Private Sub Cpy_Formula()


If Range("F3").Value = "Y" Then

Range("M19:M58").Interior.ColorIndex = 4
Selection.AutoFill Destination:=Range("M18:M58"), Type:=xlFillDefault
Range("M18:M58").Select

Range("U19:U58").Interior.ColorIndex = 8
Selection.AutoFill Destination:=Range("U18:U58"), Type:=xlFillDefault
Range("U18:U58").Select

Range("AC19:AC58").Interior.ColorIndex = 39
Selection.AutoFill Destination:=Range("AC18:AC58"),
Type:=xlFillDefault
Range("AC18:AC58").Select

Range("AK19:AK58").Interior.ColorIndex = 3
Selection.AutoFill Destination:=Range("AK18:AK58"),
Type:=xlFillDefault
Range("AK18:AK58").Select

Range("AS19:AS58").Interior.ColorIndex = 7
Selection.AutoFill Destination:=Range("AS18:AS58"),
Type:=xlFillDefault
Range("AS18:AS58").Select

Else

Range("M19:M58").Interior.ColorIndex = 36
Range("M19:M58").Value = 0

Range("U19:U58").Interior.ColorIndex = 36
Range("U19:U58").Value = 0

Range("AC19:AC58").Interior.ColorIndex = 36
Range("AC19:AC58").Value = 0

Range("AK19:AK58").Interior.ColorIndex = 36
Range("AK19:AK58").Value = 0

Range("AS19:AS58").Interior.ColorIndex = 36
Range("AS19:AS58").Value = 0

End If


End Sub
 
Reply With Quote
 
 
 
 
michael.beckinsale
Guest
Posts: n/a
 
      11th Mar 2008
Hi Otto,

Try replacing each bit of your code that looks like this:

Range("M19:M58").Interior.ColorIndex = 4
Selection.AutoFill Destination:=Range("M18:M58"), Type:=xlFillDefault
Range("M18:M58").Select

With this

Range("M18").Copy Destination:= Range("M19:M58")

This will copy all of the properties of 'M18' to 'M19:M58' and the
because you are not selecting ranges the code is more efficient.

HTH

Michael
 
Reply With Quote
 
Otter
Guest
Posts: n/a
 
      11th Mar 2008
Thanks Michael. That works perfectly

"michael.beckinsale" wrote:

> Hi Otto,
>
> Try replacing each bit of your code that looks like this:
>
> Range("M19:M58").Interior.ColorIndex = 4
> Selection.AutoFill Destination:=Range("M18:M58"), Type:=xlFillDefault
> Range("M18:M58").Select
>
> With this
>
> Range("M18").Copy Destination:= Range("M19:M58")
>
> This will copy all of the properties of 'M18' to 'M19:M58' and the
> because you are not selecting ranges the code is more efficient.
>
> HTH
>
> Michael
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating Macro to copy information from cell into another cell using Add Comments pmipalma Microsoft Excel Programming 2 6th Oct 2006 07:46 PM
Using macro to copy a part of a cell content to next cell =?Utf-8?B?Q2hhcmxlcw==?= Microsoft Excel Misc 6 31st May 2006 05:57 AM
macro to copy & paste cell data into another cell =?Utf-8?B?ZGFubnlib3k=?= Microsoft Excel Programming 2 23rd Dec 2005 05:01 PM
Writing a macro to copy first cell down until next cell has data Gerald Microsoft Excel Programming 1 10th Aug 2005 10:06 PM
Copy cell macro abzpowr Microsoft Excel Programming 4 7th Jul 2005 06:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:17 AM.