How to use loop to delete border from empty cells

Ahmed786

New Member
Joined
Jun 2, 2022
Messages
26
Office Version
  1. 2016
Platform
  1. Windows
Hello dear,
My code is to remove border from empty cell which is working fine.
Dim ismycellempty As Boolean
ismycellempty = IsEmpty(Range("BG11"))
If ismycellempty = True Then
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Else
MsgBox ("no cell empty")
End If
How can i use same code in loop. I wish to use it from Range "BG11 to BG49"
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi Ahmend786,

please use code-tags in the future to post your procedures like described in How to Post Your VBA Code.

Maybe

VBA Code:
Public Sub MrE_1230342_1703212()
' https://www.mrexcel.com/board/threads/how-to-use-loop-to-delete-border-from-empty-cells.1230342/

Dim blnEmpty As Boolean
Dim rngCell As Range
Dim positions As Variant
Dim lngCnt As Long

positions = Array(xlDiagonalDown, xlDiagonalUp, xlDiagonalDown, xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight, xlInsideVertical, xlInsideHorizontal)

For Each rngCell In Range("BG11:BG49")
  blnEmpty = IsEmpty(rngCell)
  If blnEmpty Then
    For lngCnt = LBound(positions) To UBound(positions)
      rngCell.Borders(positions(lngCnt)).LineStyle = xlNone
    Next lngCnt
  Else
    MsgBox rngCell.Address(0, 0) & "is not empty", vbInformation, "No empty cell"
  End If
Next rngCell
End Sub

Ciao,
Holger
 
Upvote 0
A short but sweet method:

VBA Code:
    If WorksheetFunction.CountBlank(Range("BG11:BG49")) > 0 Then
        Range("BG11:BG49").SpecialCells(xlCellTypeBlanks).Borders.LineStyle = xlNone
    Else
        MsgBox ("no cell empty")
    End If
 
Upvote 0
Hi johnnyL,

nice one but if in the cells the xlDiagonalDown and xlDiagonalUp remained - any tip on how to get rid of these as well?

Holger
 
Upvote 0
Hi johnnyL,

nice one but if in the cells the xlDiagonalDown and xlDiagonalUp remained - any tip on how to get rid of these as well?

Holger
Hmm. I thought the line of code I provided removed all of the borders. After testing, I see the diagonals remain. :(

I guess you would have to add the additional 2 lines of code to remove those diagonals if they are a possibility of existing.
 
Upvote 0
I guess you would have to add the additional 2 lines of code to remove those diagonals if they are a possibility of existing.
Correct
Hmm. I thought the line of code I provided removed all of the borders. After testing, I see the diagonals remain.
From Borders object (Excel)

"A collection of four Border objects that represent the four borders of a Range object or Style object."

"Use the Borders property to return the Borders collection, which contains all four borders. You can apply different borders to each side of a cell or range."

and from Border object (Excel)

"Range and Style objects have four discrete borders—left, right, top, and bottom—which can be returned individually or as a group. Use the Borders property to return the Borders collection, which contains all four borders and treats the borders as a unit."

I've never seen MS state a reason why diagonals aren't included (or specifically state that they aren't for that matter)
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top