Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Excel red box around text macro

0

Hello! 

I'm trying to add a macro in excel where I can use a shortcut to populate a red box around text in order for me to "Tickmark" evidence in an excel sheet. I'm not sure what code I would need to enter to make this happen. Any help would be much appreciated!

[CODE]

Sub TICKMARK()

'

' TICKMARK Macro

'

' Keyboard Shortcut: Ctrl+Shift+E

'

    Range("D2:F5").Select

    Selection.Borders(xlDiagonalDown).LineStyle = xlNone

    Selection.Borders(xlDiagonalUp).LineStyle = xlNone

    With Selection.Borders(xlEdgeLeft)

        .LineStyle = xlContinuous

        .Color = -16776961

        .TintAndShade = 0

        .Weight = xlThin

    End With

    With Selection.Borders(xlEdgeTop)

        .LineStyle = xlContinuous

        .Color = -16776961

        .TintAndShade = 0

        .Weight = xlThin

    End With

    With Selection.Borders(xlEdgeBottom)

        .LineStyle = xlContinuous

        .Color = -16776961

        .TintAndShade = 0

        .Weight = xlThin

    End With

    With Selection.Borders(xlEdgeRight)

        .LineStyle = xlContinuous

        .Color = -16776961

        .TintAndShade = 0

        .Weight = xlThin

    End With

    Selection.Borders(xlInsideVertical).LineStyle = xlNone

    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

    Range("F1").Select

    Selection.Borders(xlDiagonalDown).LineStyle = xlNone

    Selection.Borders(xlDiagonalUp).LineStyle = xlNone

    Selection.Borders(xlEdgeLeft).LineStyle = xlNone

    Selection.Borders(xlEdgeTop).LineStyle = xlNone

    With Selection.Borders(xlEdgeBottom)

        .LineStyle = xlContinuous

        .Color = -16776961

        .TintAndShade = 0

        .Weight = xlThin

    End With

    Selection.Borders(xlEdgeRight).LineStyle = xlNone

    Selection.Borders(xlInsideVertical).LineStyle = xlNone

    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

    Range("F1").Select

    Selection.Borders(xlDiagonalDown).LineStyle = xlNone

    Selection.Borders(xlDiagonalUp).LineStyle = xlNone

    Selection.Borders(xlEdgeLeft).LineStyle = xlNone

    Selection.Borders(xlEdgeTop).LineStyle = xlNone

    With Selection.Borders(xlEdgeBottom)

        .LineStyle = xlContinuous

        .Color = -16776961

        .TintAndShade = 0

        .Weight = xlThin

    End With

    Selection.Borders(xlEdgeRight).LineStyle = xlNone

    Selection.Borders(xlInsideVertical).LineStyle = xlNone

    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

    Range("E2").Select

End Sub

Answer
Discuss

Discussion

Hi Michael and welcome to the Forum. 

Please post what code you tried so far, using the CODE button when editing your original question (we like users to have made an attempt before we help). 
John_Ru (rep: 6152) Jan 5, '23 at 5:22 pm
Hi John, 

Thank you for the welcoming. I'm new to macros so I'm sure my code isn't as good as it could be but I've posted what i have been attempting to use above. I'm trying to make it so that I can move the red box around a sheet and adjust the sizing of the box as well. Thank you for any help you can provide. 
MichaelMunoz95 (rep: 2) Jan 5, '23 at 7:11 pm
Hello Michael,

First, you can eliminate a lot of the code you have by using the following:
Range("D2:F5").BorderAround _
        ColorIndex:=3, Weight:=xlThin
Range("F1").BorderAround _
        ColorIndex:=3, Weight:=xlThin
 
 Range("E2").Select

It appears that you used Excel's Macro Recorder to get the code you have. Excel, by default, will enter all possible lines. What you are doing with the macro is adding a border. So any line that has xlNone is not necessary.

You go on to mention "I'm trying to make it so that I can move the red box around a sheet and adjust the sizing". At what point (how, what trigger) do you want the borders removed? Do you want any of the borders to stay when you leave the sheet or close the workbook?
These details are needed to be able to come up with a solution.

Cheers   :-)
WillieD24 (rep: 557) Jan 5, '23 at 8:06 pm
Hi Willie,

Thank you for the insight! I will give it a shot when I log on for work tomorrow morning! For some background, I work in audit so I'm constantly adding red "tickmark boxes" around evidence so that I can point and refer to it in a write up document. So essentially what I'm trying to accomplish is being able to streamline the process of making these boxes around screenshots in a sheet (hoping it stays when I exit out the sheet). Apologies for my grade school code :) been watching YouTube videos and they suggested recording a macro as an easier method to create them.

Thank you for the help!
MichaelMunoz95 (rep: 2) Jan 6, '23 at 12:13 am
I think I see where I am going wrong. I was initially adding in a border when I should be adding in a shape to get what im looking for! 
MichaelMunoz95 (rep: 2) Jan 6, '23 at 1:14 am
Michael- please see my revised answer and file. Using shapes makes the removal of previous (or mistaken) "tickmark boxes" much easier!
John_Ru (rep: 6152) Jan 7, '23 at 6:48 am
Add to Discussion

Answers

0
Selected Answer

Michael

In the attached file, I've replaced all your recorded code with this (in Module1), in bold with added comments to explain what is happening:

Sub TICKMARK()
'
' TICKMARK Macro
'
' Keyboard Shortcut: Ctrl+Shift+E
'

Dim Edges() As Variant
'
'declare an array with outer edge types
Edges = Array(xlEdgeBottom, xlEdgeTop, xlEdgeRight, xlEdgeLeft)
'loop through array for edge types
For n = LBound(Edges) To UBound(Edges)
    'setting edges to the same
    With Selection.Borders(Edges(n))
        .LineStyle = xlContinuous
        .Color = vbRed
        .Weight = xlMedium
    End With
Next n

End Sub

To use it, select a range of cells (optionally adding multiple ranges by pressing Ctrl first and selecting) then press Ctrl+Shift+E- it will draw red borders around the outside of what you had selected.

REVISION 07 January 2023:

In view of your discussion comments about using shapes instead, the revised file has this new code (in Module 2) which does that and will first delete all your previous "tickmark" boxes then draw new ones around your new selection(s). It does so by setting the shape names as Tickmark0, Tickmark2 etc. then the first For Each loop used the Instr method to see which shapes are named Tickmark... and deletes them if so (leaving any other shapes in tact). Again the comments tell you what's happening:

Sub AddShapes()
'
' AddShapes Macro
' Removes/ adds shapes around selection(s)
'
' Keyboard Shortcut: Ctrl+Shift+Q
'
    Dim shp As Shape, Slct As Variant

    'delete existing Tickmark shapes
    For Each shp In ActiveSheet.Shapes
        If InStr(shp.Name, "Tickmark") > 0 Then shp.Delete
    Next shp
    ' capture user selection in an array
    Slct = Split(Selection.Address, ",")
    ' loop though selection address(es)
    For n = LBound(Slct) To UBound(Slct)
        'add rectangle with dimensions...
        With Range(Slct(n))
            Set shp = ActiveSheet.Shapes.AddShape(1, .Left, .Top, .Width, .Height)
        End With
        ' ... then set properties and distinctive name(s) for easy later deletion
        With shp
            .Fill.Visible = msoFalse
            .Line.ForeColor.RGB = RGB(255, 0, 0)
            .Line.Weight = 2
            .Name = "Tickmark" & n
        End With
    Next n

End Sub

Pick some cells then use Ctrl+Shift+Q to get new red shapes. Repeat to delete those and add new ones.

Hope this helps- if so, please mark this answer as Selected.

Discuss

Discussion

Hi John,

This is exactly what I was looking to accomplish! Thank you so much for your help :) any tips on where I can learn more about coding macros without using macro recorder?
MichaelMunoz95 (rep: 2) Jan 7, '23 at 8:19 am
Great! Thanks for selecting my answer, Michael. Am I right to assume you prefer the revised solution, based on shapes? (I do!) 

Don has provided some great tutorials on this (his) site- you'll find a section on VBA on the Tutorials page (look above for the hyperlink). Better still he has an excellent structured VBA course taking you from "Beginner to Expert" but you'd need to pay (it may still be discounted, I'm not sure). See ad above.

As you can see, the Macro Recorder often creates overlong code but it is very useful if you don't know or can't remember how to do something in VBA - it gives you great clues (which you can tidy up snd modify).
John_Ru (rep: 6152) Jan 7, '23 at 9:25 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login