PC Review


Reply
Thread Tools Rate Thread

Copy Cell Formatting to activecell with VBA

 
 
upendra
Guest
Posts: n/a
 
      3rd Oct 2008
Hi,
I wrote the below code to copy formatting of the selected cell to
current cell. But its not working. Kindly let me know whats the fault
with the code

Public Function CopyFormat(myrng As Range)
If myrng.Interior.ColorIndex = xlNone Then
Exit Function
Else
ActiveCell.Interior.ColorIndex = myrng.Interior.ColorIndex
ActiveCell.Interior.Pattern = myrng.Interior.Pattern
End Function


Regards,
Upendra
 
Reply With Quote
 
 
 
 
Thomas [PBD]
Guest
Posts: n/a
 
      3rd Oct 2008
upendra,

First things first, you were missing the End If before the End Function.
Secondly, I believe with what you are attempting, you should probably run
this as a Subroutine. Like so:

Public Sub CopyFormat()

Dim myrng As String
myrng = InputBox("Enter Range")

If Range(myrng).Columns.Count = Selection.Columns.Count Then
If Range(myrng).Rows.Count = Selection.Rows.Count Then
Range(myrng).Copy
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Else
MsgBox "Ranges must be the same size", , "Error"
Exit Sub
End If
Else
MsgBox "Ranges must be the same size", , "Error"
Exit Sub
End If
End Sub

Basically, what I have amended it to do is require a pop-up box for the
input range, place something like B2 in the box. This will only do one cell
at a time, or multiple cells, however your range has to be the same size.

Just a note: This could probably be done just as quickly with Copy>Paste
Special>Formats.

--
--Thomas [PBD]
Working hard to make working easy.


"upendra" wrote:

> Hi,
> I wrote the below code to copy formatting of the selected cell to
> current cell. But its not working. Kindly let me know whats the fault
> with the code
>
> Public Function CopyFormat(myrng As Range)
> If myrng.Interior.ColorIndex = xlNone Then
> Exit Function
> Else
> ActiveCell.Interior.ColorIndex = myrng.Interior.ColorIndex
> ActiveCell.Interior.Pattern = myrng.Interior.Pattern
> End Function
>
>
> Regards,
> Upendra
>

 
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
In a formula, copy the value and formatting of another cell =?Utf-8?B?cGFwYWtmbA==?= Microsoft Excel Worksheet Functions 4 29th Apr 2007 02:22 PM
Copy Cell Formatting =?Utf-8?B?Q2hyaXN0bWFzIE1heQ==?= Microsoft Excel Programming 2 20th Oct 2006 07:04 PM
How to copy formatting when moving from cell to variable to another cell on another worksheet kls Microsoft Excel Programming 1 11th Sep 2004 10:42 PM
VBA? Activecell formatting click4mrh Microsoft Excel Misc 4 28th Aug 2004 06:55 AM
Re: Copy Formatting of Cell Frank Kabel Microsoft Excel Programming 3 14th May 2004 10:35 PM


Features
 

Advertising
 

Newsgroups
 


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