简体   繁体   English

如何在一系列合并和未合并的单元格周围放置边框

[英]How to put borders around a range of merged and non-merged cells

I need a macro that puts a border in a range of cells from A to F, based on the formatting in the A column.我需要一个宏,它根据 A 列中的格式在从 A 到 F 的一系列单元格中放置边框。 In the A column, cells can be merged with multiple cells below it, or can be just a single cell.在 A 列中,单元格可以与其下方的多个单元格合并,也可以只是一个单元格。 I have written a VBA code to put a border around the non-blank cells in column A, but dont know how to expand it to the other 5 columns (from B to F).我编写了一个 VBA 代码来在 A 列中的非空白单元格周围放置边框,但不知道如何将其扩展到其他 5 列(从 B 到 F)。 See the pictures to better understand what I have written and what I need.查看图片以更好地了解我所写的内容和我需要的内容。

How the Data looks like: Data数据的样子:数据

What my code does: WhatMyCodeDoes我的代码做什么: WhatMyCodeDoes

What I want it to do: WhatIWantItToDo我想要它做什么: WhatIWantItToDo

My code:我的代码:

Sub Borders()
Dim linestyle As Variant, line   As Variant
Range("A1").Select
Do While ActiveCell.Address <> Range("A65536").End(xlUp).Offset(1, 0).Address
If ActiveCell.Value > 0 Then
 linestyle = Array(xlDiagonalDown, xlDiagonalUp, xlInsideVertical, xlInsideHorizontal)
line = Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight)
For I = 0 To UBound(linestyle)
Selection.Borders(linestyle(I)).linestyle = xlNone
    With Selection.Borders(line(I))
        .linestyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    Next I
Else
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub

You may change only two lines and will be OK.您可以只更改两行,就可以了。

'Range("A1").Select
Range("A1").Resize(1, 6).Select
' ...

'ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(1, 0).Resize(1, 6).Select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM