自动边界单元

时间:2018-07-20 13:59:42

标签: excel excel-vba border

我正在尝试自动将P13:S中的所有填充单元格作为边界。当宏运行时,我还希望它检测透明单元并删除当前存在的任何边界。我目前正在使用它:

Set CD = Workbooks("Savant").Worksheets("Client Details")

Application.ScreenUpdating = False

Dim lngLstCol As Long, lngLstRow As Long

lngLstRow = CD.UsedRange.Rows.Count
lngLstCol = CD.UsedRange.Columns.Count

For Each rngCell In Range(Range("P13:S13"), Cells(lngLstRow, lngLstCol))
    If rngCell.Value <> "" Then
        rngCell.Select
        With Selection.Borders
            .LineStyle = xlNone
        End With
    End If
Next

For Each rngCell In Range(Range("P13:S13"), Cells(lngLstRow, lngLstCol))
    If rngCell.Value > "" Then
        rngCell.Select
        With Selection.Borders
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
    End If
Next

Application.ScreenUpdating = True

它是按需边界的单元格,但是无法删除空单元格的边界。谁能指出我哪里出问题了?

非常感谢

2 个答案:

答案 0 :(得分:1)

您可以通过组合逻辑来进一步简化代码,而不必再次循环。

For Each rngCell In Range(Range("P13:S13"), Cells(lngLstRow, lngLstCol))
    With rngCell
        If Len(Trim(.Value2)) = 0 Then
            With .Borders
                .LineStyle = xlNone
            End With
        Else
            With .Borders
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
        End If
    End With
Next

答案 1 :(得分:0)

我只是很胖。将“ <>”替换为“ =”,即可正常运行。抱歉。