Wednesday, March 05, 2008

Macro to find cell format

This macro will search through the range and identify the cell format (in our example, it is the cell color) and copy and paste the formulas from the first row to the last row which is 100


Sub Find_Cell_Format()

Set Which_Worksheet_2_Find = ThisWorkbook.Worksheets("Details")
Set Range_2_Find = Which_Worksheet_2_Find.Range("C28:BZ28")
' this is a range to find.
last_row_in_the_template = 100
' Change the number of the last
' accordingly


Paste_Type = xlPasteFormulas
'xlPasteAll 'default
'xlPasteAllExceptBorders
'xlPasteColumnWidths
'xlPasteComments
'xlPasteFormats
'xlPasteFormulas
'xlPasteFormulasAndNumberFormats
'xlPasteValidation
'xlPasteValues
'xlPasteValuesAndNumberFormats

Plus_Minus_Times_Divide = xlPasteSpecialOperationNone
'xlPasteSpecialOperationAdd
'xlPasteSpecialOperationDivide
'xlPasteSpecialOperationMultiply
'xlPasteSpecialOperationNone Default
'xlPasteSpecialOperationSubtract


For Each c In Range_2_Find '[A1:C5]
If c.Interior.ColorIndex = 37 Then
'37 refers to color
'orange in grid chart

c_col = c.Column
Which_Worksheet_2_Find.Cells(29, c_col).Copy 'copy the content in row 29
Which_Worksheet_2_Find.Range( _
Which_Worksheet_2_Find.Cells(30, c_col), _
Which_Worksheet_2_Find.Cells(last_row_in_the_template, c_col)) _
.PasteSpecial Paste:=Paste_Type, Operation:=Plus_Minus_Times_Divide
CutCopyMode = False
Range("A1").Select
End If

Next

End Sub

2 comments:

BigGreenZero said...

I am trying to do this same thing but wish to only search for accounting format and set decimal to 0 and symbol to none. I have 25 worksheets with numerous tables and the daunting task of changing all accounting formats to the above is incredible. Not all the column are accounting some are %, text, general, date. I need this to start in a1 and perform through "last used cell in workbook"

can you help me?

JK said...

You can record a macro for all the actions in one worksheet and try to run the macro in all the other worksheets. You will have to activate the macro 24 more times. THis is not the best solution but it would save you significant amount of time.