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

Macro for the find function

This macro will return the location (row, column or address) of the cell containing the search value.

Function Find_Row_Num(What_2_Find)

What_2_Find = "Insert row before this line for PAPDR"
Set Which_Worksheet_2_Find = ThisWorkbook.Worksheets("Details")
Set Range_2_Find = Which_Worksheet_2_Find.Range("C30:C65000")
' this is a range.
Value_or_Formula = xlFormulas 'xlVaues - search the text/value in the cell,
'for cells with formula, it will look at the result.
'xlformulas - search the text/value within formula
'xlformulas works even when cell is hidden.
'It is able to look for value too.

Exact_Partial = xlPart
'xlPart - find cells that contains (What_2_Find)
'xlWhole - find cells which contains
'exactly the value placed in (What_2_Find)

Match_Capital_Letters = False
'False - when A and a is treated the same
' True when A and a means different things.



With Range_2_Find
Set c = .Find(What:=What_2_Find, LookIn:=Value_or_Formula, _
LookAt:=Exact_Partial, MatchCase:=Match_Capital_Letters)
'c is the cell that meet your find criteria
If Not c Is Nothing Then
Find_Row_Num = c.Row
' Change this to c.column if you want the column
' where the text is found.
'change it to c.address if you want the address returned.

Else
Find_Row_Num = 1 ' Cannot find, so default the address to A1
End If
End With

End Function