Use VBA to change cell formatting based on existing formatting

Dossertoir

New Member
Joined
Oct 15, 2015
Messages
2
Hi

I've been recording some basic formatting macros to speed up my work. I am wanting some to cycle to different formats depending on the existing format of selected cells. For example, I have the following code for including a top border on the selection:

Private Sub Top_Border()'
' Top_Border Macro
'
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With


End Sub

And the following code for including a top and a bottom border on a cell:

Private Sub Double_Border()
'
' Double_Border Macro
'
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Color = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Color = 0
.TintAndShade = 0
.Weight = xlThin
End With


End Sub


But what I am wanting is for when I run the macro, if there is no top border, then include the top border, if there is already a top border, then include both a top and a bottom border, and if there is already a top and bottom border, then remove both of the borders. I am just looking to utilise similar functions whilst using minimal keyboard short-cuts.

I know this is probably basic and that the above code is possibly very clunky - but I am a VBA novice!

Thanks!
 

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,477
Code:
With Selection 'need to select your own

If .Borders(xlEdgeTop).LineStyle <> xlLineStyleNone And .Borders(xlEdgeBottom).LineStyle <> xlLineStyleNone Then
'delete top and bottom borders
.Borders(xlEdgeTop).LineStyle = xlLineStyleNone
.Borders(xlEdgeBottom).LineStyle = xlLineStyleNone

ElseIf .Borders(xlEdgeTop).LineStyle <> xlLineStyleNone Then
'add bottom border if top border exists
.Borders(xlEdgeBottom).LineStyle = xlContinuous

ElseIf .Borders(xlEdgeTop).LineStyle = xlLineStyleNone Then
'add top border if it doesn't exist
.Borders(xlEdgeTop).LineStyle = xlContinuous

End If

End With
For more info, check out the following page:

https://technet.microsoft.com/en-us/library/ee692886.aspx
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,278
Here I will give you a couple If Statements. And you try and see if you can write the script yourself.
If your not able to complete this task check back in here for more help.

Code:
Sub If_Selection()

If Selection.Borders(xlEdgeTop).LineStyle = xlContinuous Then
MsgBox "Yes Top"
'Your other code here
End If

If Selection.Borders(xlEdgeTop).LineStyle = xlContinuous And Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous Then
MsgBox "Yes Both Top and Bottom"
'Your other code here
End If
End Sub
Instead of giving you a fish. I would like to help you learn to fish.
 
Last edited:

Forum statistics

Threads
1,082,040
Messages
5,362,804
Members
400,693
Latest member
jenlj

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top