Hello All--
I am having an issue with this VBA code (please keep in mind I am a novice). When the VBA is activated on a standard cell (or a range of cells), I would like it to go to a dots format, and then a double diagonal format, and then back to the original standard format. However, when I activate the VBA I am getting a combination/cumulative result of dots and diagonal lines at the same time. It is working correctly when an individual cell is selected, however, when a range of cells the macro is incorrectly formatting. I've included a gif to show what I mean. Any help would be appreciated, thank you in advance.
The code is below:
I am having an issue with this VBA code (please keep in mind I am a novice). When the VBA is activated on a standard cell (or a range of cells), I would like it to go to a dots format, and then a double diagonal format, and then back to the original standard format. However, when I activate the VBA I am getting a combination/cumulative result of dots and diagonal lines at the same time. It is working correctly when an individual cell is selected, however, when a range of cells the macro is incorrectly formatting. I've included a gif to show what I mean. Any help would be appreciated, thank you in advance.
The code is below:
VBA Code:
Sub Blank_Format()
'
' Blank Format Macro
'
' Keyboard Shortcut: Ctrl+Shift+Q
'
Application.ScreenUpdating = False
Dim cell As Range
For Each cell In Selection
On Error GoTo Error
'\\Dots format
If cell.Interior.Color = "16777215" And cell.Interior.Pattern = "-4142" Then
cell.Interior.Pattern = 18
cell.Font.Color = "0"
cell.Interior.Color = "16777215"
cell.Font.Bold = False
With Selection.Interior
.Pattern = xlGray8
.PatternThemeColor = xlThemeColorLight1
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'\\Slash format
ElseIf cell.Interior.Pattern = 18 Then
cell.Interior.Pattern = xlSolid
With Selection.Borders(xlDiagonalUp)
.LineStyle = xlContinuous
.Weight = xlThin
.Color = RGB(192, 192, 192)
End With
With Selection.Borders(xlDiagonalDown)
.LineStyle = xlContinuous
.Weight = xlThin
.Color = RGB(192, 192, 192)
End With
Else
'\\Base format
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
cell.Interior.Pattern = xlSolid
cell.Font.Color = "0"
cell.Interior.Color = "16777215"
cell.Font.Bold = False
cell.Interior.ColorIndex = "0"
End If
Error:
If Err.Description <> "" Then
cell.Interior.Pattern = xlSolid
cell.Font.Color = "0"
cell.Interior.Color = "16777215"
cell.Font.Bold = False
cell.Interior.ColorIndex = "0"
End If
Next cell
Application.ScreenUpdating = True
End Sub