SOLVED

Use a macro to achieve conditional formating.

%3CLINGO-SUB%20id%3D%22lingo-sub-1519312%22%20slang%3D%22en-US%22%3EUse%20a%20macro%20to%20achieve%20conditional%20formating.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1519312%22%20slang%3D%22en-US%22%3E%3CP%3EI%20need%20to%20format%20a%20generated%20document%20by%20adding%20a%20blank%20line%20and%20formatting%203%20cells%20with%20a%20top%20line%20boarder%20all%20based%20on%20the%20text%20of%20cells%20in%20a%20separate%20column.%26nbsp%3B%20I%20want%20to%20use%20a%20macro%20that%20is%20stored%20in%20the%20personal%20macro%20file%20as%26nbsp%3Ba%20new%26nbsp%3Breport%20is%20generated%20daily.%26nbsp%3B%20I%20have%20a%20routine%20that%20will%20add%20the%20blank%20line%20but%20can't%20get%20the%20boarder%20included.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20a%20cell%20in%20column%20D%20%3D%20%22Department%20Totals%22%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20format%20the%20same%20cells%20in%20column%20E%3AG%20with%20top%20boarder%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20insert%20blank%20row%20after%20that%20row.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1519312%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1522497%22%20slang%3D%22en-US%22%3ERe%3A%20Use%20a%20macro%20to%20achieve%20conditional%20formating.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1522497%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F281238%22%20target%3D%22_blank%22%3E%40BYahr1512%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20about%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20FormatTotalCell()%0ADim%20rng%20As%20Range%0ADim%20r%20%20%20As%20Long%0AWith%20ActiveSheet%0A%20%20%20%20Set%20rng%20%3D%20.Columns(%22D%22).Find(what%3A%3D%22Department%20Totals%22%2C%20lookat%3A%3DxlWhole)%0A%20%20%20%20If%20Not%20rng%20Is%20Nothing%20Then%0A%20%20%20%20%20%20%20%20r%20%3D%20rng.Row%0A%20%20%20%20%20%20%20%20.Rows(r%20%2B%201).Insert%0A%20%20%20%20%20%20%20%20With%20.Range(%22E%22%20%26amp%3B%20r%20%26amp%3B%20%22%3AG%22%20%26amp%3B%20r).Borders(xlEdgeTop)%0A%20%20%20%20%20%20%20%20%20%20%20%20.LineStyle%20%3D%20xlContinuous%0A%20%20%20%20%20%20%20%20%20%20%20%20.ColorIndex%20%3D%20xlAutomatic%0A%20%20%20%20%20%20%20%20%20%20%20%20.Weight%20%3D%20xlMedium%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20Else%0A%20%20%20%20%20%20%20%20MsgBox%20%22No%20cell%20in%20column%20D%20contains%20'Department%20Totals'%20in%20it.%22%2C%20vbExclamation%0A%20%20%20%20End%20If%0AEnd%20With%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1524124%22%20slang%3D%22en-US%22%3ERe%3A%20Use%20a%20macro%20to%20achieve%20conditional%20formating.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1524124%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20was%20a%20step%20in%20the%20right%20direction.%26nbsp%3B%20I%20experienced%20two%20problems.%26nbsp%3B%20First%2C%20only%20the%20first%20time%20%22department%20total%22%20was%20in%20column%20D%20were%20the%20adjustments%20made.%26nbsp%3B%20This%20happens%20300%20to%20400%20times%20in%20this%20report.%26nbsp%3B%20Also%20when%20I%20copied%20it%20into%20the%20personal%20macro%20file%20it%20somehow%20messed%20with%20the%20regular%20replace%20command%20in%20Excel.%26nbsp%3B%20I%20would%20also%20like%20to%20assign%20a%20short%20cut%20key%20for%20this%20macro.%26nbsp%3B%20Perhaps%20Ctrl%2BShift%2BQ.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1524225%22%20slang%3D%22en-US%22%3ERe%3A%20Use%20a%20macro%20to%20achieve%20conditional%20formating.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1524225%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F281238%22%20target%3D%22_blank%22%3E%40BYahr1512%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOkay%2C%20give%20this%20a%20try%20and%20I%20am%20sure%20this%20will%20take%20care%20of%20both%20the%20issue%20you%20reported.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20FormatTotalCell()%0ADim%20rng%20%20%20%20%20%20%20%20%20As%20Range%0ADim%20r%20%20%20%20%20%20%20%20%20%20%20As%20Long%0ADim%20fAddress%20%20%20%20As%20String%0A%0AApplication.ScreenUpdating%20%3D%20False%0A%0AWith%20ActiveSheet%0A%20%20%20%20Set%20rng%20%3D%20.Columns(%22D%22).Find(what%3A%3D%22Department%20Totals%22%2C%20LookAt%3A%3DxlWhole)%0A%20%20%20%20If%20Not%20rng%20Is%20Nothing%20Then%0A%20%20%20%20%20%20%20%20fAddress%20%3D%20rng.Address%0A%20%20%20%20%20%20%20%20Do%0A%20%20%20%20%20%20%20%20r%20%3D%20rng.Row%0A%20%20%20%20%20%20%20%20.Rows(r%20%2B%201).Insert%0A%20%20%20%20%20%20%20%20With%20.Range(%22E%22%20%26amp%3B%20r%20%26amp%3B%20%22%3AG%22%20%26amp%3B%20r).Borders(xlEdgeTop)%0A%20%20%20%20%20%20%20%20%20%20%20%20.LineStyle%20%3D%20xlContinuous%0A%20%20%20%20%20%20%20%20%20%20%20%20.ColorIndex%20%3D%20xlAutomatic%0A%20%20%20%20%20%20%20%20%20%20%20%20.Weight%20%3D%20xlMedium%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20%20%20%20%20Set%20rng%20%3D%20.Columns(%22D%22).FindNext(rng)%0A%20%20%20%20%20%20%20%20Loop%20While%20Not%20rng%20Is%20Nothing%20And%20fAddress%20%26lt%3B%26gt%3B%20rng.Address%0A%20%20%20%20Else%0A%20%20%20%20%20%20%20%20MsgBox%20%22No%20cell%20in%20column%20D%20contains%20'Department%20Totals'%20in%20it.%22%2C%20vbExclamation%0A%20%20%20%20End%20If%0AEnd%20With%0AResetFindAndReplace%0AApplication.ScreenUpdating%20%3D%20True%0AEnd%20Sub%0A%0ASub%20ResetFindAndReplace()%0A%20%20%20%20Dim%20r%20As%20Range%0A%0A%20%20%20%20On%20Error%20Resume%20Next%0A%20%20%20%20Set%20r%20%3D%20ActiveCell%0A%20%20%20%20On%20Error%20GoTo%200%0A%0A%20%20%20%20Cells.Find%20what%3A%3D%22%22%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20After%3A%3DActiveCell%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20LookIn%3A%3DxlFormulas%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20LookAt%3A%3DxlPart%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20SearchOrder%3A%3DxlByRows%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20SearchDirection%3A%3DxlNext%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20MatchCase%3A%3DFalse%2C%20_%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20SearchFormat%3A%3DFalse%0A%20%20%20%20Cells.Replace%20what%3A%3D%22%22%2C%20Replacement%3A%3D%22%22%2C%20ReplaceFormat%3A%3DFalse%0A%0A%20%20%20%20If%20Not%20r%20Is%20Nothing%20Then%20r.Select%0A%20%20%20%20Set%20r%20%3D%20Nothing%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20will%20need%20to%20assign%20the%20shortcut%20key%20to%20run%20this%20macro%20on%20your%20end%20and%20to%20do%20so%2C%20press%20Alt%2BF8%20to%20open%20Macro%20list%2C%20choose%20the%20desired%20macro%20from%20the%20list%2C%20click%20on%20Options%20and%20in%20there%20set%20the%20desired%20shortcut%20key.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20that%20takes%20care%20of%20your%20original%20question%2C%20please%20don't%20forget%20to%20accept%20the%20post%20with%20the%20proposed%20solution%20as%20a%20Best%20Response%20to%20mark%20your%20question%20as%20Solved.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1527064%22%20slang%3D%22en-US%22%3ERe%3A%20Use%20a%20macro%20to%20achieve%20conditional%20formating.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1527064%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%26nbsp%3Btaking%20the%20time%20to%20provide%20information.%26nbsp%3B%20%26nbsp%3BOn%20the%20first%20attempt%20this%20did%20not%20give%20the%20desired%20results.%26nbsp%3B%20It%20appears%20that%20when%20copied%20into%20the%20personal%20file%20it%20is%20two%20separate%20sets%20of%20instructions%20and%20the%20results%20returned%20were%20%22No%20Department%20Total%20found%20in%20Column%20D%22.%26nbsp%3B%20I'll%20get%20a%20chance%20to%20work%20with%20this%20again%20Fri%20and%20Sat.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1527374%22%20slang%3D%22en-US%22%3ERe%3A%20Use%20a%20macro%20to%20achieve%20conditional%20formating.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1527374%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F281238%22%20target%3D%22_blank%22%3E%40BYahr1512%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20the%20macro%20doesn't%20find%20any%20cell%20in%20column%20D%20is%20not%20equal%20to%20the%20text%20%22Department%20Totals%22%20it%20will%20prompt%20you%20with%20the%20following%20message...%3C%2FP%3E%3CP%3E%3CSTRONG%3E%22No%20cell%20in%20column%20D%20contains%20'Department%20Totals'%20in%20it.%22%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20remember%20it's%20not%20a%20partial%20match%20but%20the%20whole%20cell%20content%20is%20to%20be%20matched.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1530793%22%20slang%3D%22en-US%22%3ERe%3A%20Use%20a%20macro%20to%20achieve%20conditional%20formating.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1530793%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYAY!%26nbsp%3B%20Its%20working.%26nbsp%3B%20Thanks%20for%20the%20assist.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1530806%22%20slang%3D%22en-US%22%3ERe%3A%20Use%20a%20macro%20to%20achieve%20conditional%20formating.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1530806%22%20slang%3D%22en-US%22%3E%3CP%3EYou're%20welcome%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F281238%22%20target%3D%22_blank%22%3E%40BYahr1512%3C%2FA%3E!%20Glad%20it%20worked%20as%20desired.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I need to format a generated document by adding a blank line and formatting 3 cells with a top line boarder all based on the text of cells in a separate column.  I want to use a macro that is stored in the personal macro file as a new report is generated daily.  I have a routine that will add the blank line but can't get the boarder included.

 

If a cell in column D = "Department Totals"

    format the same cells in column E:G with top boarder

    insert blank row after that row.

7 Replies
Highlighted

@BYahr1512 

How about this?

 

Sub FormatTotalCell()
Dim rng As Range
Dim r   As Long
With ActiveSheet
    Set rng = .Columns("D").Find(what:="Department Totals", lookat:=xlWhole)
    If Not rng Is Nothing Then
        r = rng.Row
        .Rows(r + 1).Insert
        With .Range("E" & r & ":G" & r).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .Weight = xlMedium
        End With
    Else
        MsgBox "No cell in column D contains 'Department Totals' in it.", vbExclamation
    End If
End With
End Sub

 

Highlighted

This was a step in the right direction.  I experienced two problems.  First, only the first time "department total" was in column D were the adjustments made.  This happens 300 to 400 times in this report.  Also when I copied it into the personal macro file it somehow messed with the regular replace command in Excel.  I would also like to assign a short cut key for this macro.  Perhaps Ctrl+Shift+Q.

Highlighted
Best Response confirmed by BYahr1512 (Occasional Contributor)
Solution

@BYahr1512 

Okay, give this a try and I am sure this will take care of both the issue you reported.

 

Sub FormatTotalCell()
Dim rng         As Range
Dim r           As Long
Dim fAddress    As String

Application.ScreenUpdating = False

With ActiveSheet
    Set rng = .Columns("D").Find(what:="Department Totals", LookAt:=xlWhole)
    If Not rng Is Nothing Then
        fAddress = rng.Address
        Do
        r = rng.Row
        .Rows(r + 1).Insert
        With .Range("E" & r & ":G" & r).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .Weight = xlMedium
        End With
        Set rng = .Columns("D").FindNext(rng)
        Loop While Not rng Is Nothing And fAddress <> rng.Address
    Else
        MsgBox "No cell in column D contains 'Department Totals' in it.", vbExclamation
    End If
End With
ResetFindAndReplace
Application.ScreenUpdating = True
End Sub

Sub ResetFindAndReplace()
    Dim r As Range

    On Error Resume Next
    Set r = ActiveCell
    On Error GoTo 0

    Cells.Find what:="", _
               After:=ActiveCell, _
               LookIn:=xlFormulas, _
               LookAt:=xlPart, _
               SearchOrder:=xlByRows, _
               SearchDirection:=xlNext, _
               MatchCase:=False, _
               SearchFormat:=False
    Cells.Replace what:="", Replacement:="", ReplaceFormat:=False

    If Not r Is Nothing Then r.Select
    Set r = Nothing
End Sub

 

 

You will need to assign the shortcut key to run this macro on your end and to do so, press Alt+F8 to open Macro list, choose the desired macro from the list, click on Options and in there set the desired shortcut key.

 

If that takes care of your original question, please don't forget to accept the post with the proposed solution as a Best Response to mark your question as Solved.

 

Highlighted

@Subodh_Tiwari_sktneer 

Thank you for taking the time to provide information.   On the first attempt this did not give the desired results.  It appears that when copied into the personal file it is two separate sets of instructions and the results returned were "No Department Total found in Column D".  I'll get a chance to work with this again Fri and Sat.

Highlighted

@BYahr1512 

If the macro doesn't find any cell in column D is not equal to the text "Department Totals" it will prompt you with the following message...

"No cell in column D contains 'Department Totals' in it."

 

Please remember it's not a partial match but the whole cell content is to be matched.

Highlighted

@Subodh_Tiwari_sktneer 

YAY!  Its working.  Thanks for the assist.

Highlighted

You're welcome @BYahr1512! Glad it worked as desired.