Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Copy Cell Formatting to activecell with VBA

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 154
Default Copy Cell Formatting to activecell with VBA

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 CopyPaste
SpecialFormats.

--
--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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Activecell range copy Allan Excel Programming 3 October 28th 07 01:28 AM
Copy File from activecell FIRSTROUNDKO via OfficeKB.com Excel Discussion (Misc queries) 1 December 13th 06 09:21 PM
Copy content of ActiveCell in a variable Ulrik loves horses Excel Programming 2 October 8th 06 11:05 PM
Copy format of row above my activecell al007 Excel Programming 4 December 9th 05 09:58 PM
ActiveCell.Copy Range("R3C27") Stuart Grant New Users to Excel 2 September 30th 05 09:00 AM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"