Excel 当单元格值更改时,如何向一系列行添加厚底边框?

Excel 当单元格值更改时,如何向一系列行添加厚底边框?,excel,vba,border,Excel,Vba,Border,我有一个代码,当单元格值更改时,它将插入一个空行 我如何修改它以添加厚底边框?每当列B中的值更改时,边框将从列A转到列AB Sub InsertRows() Dim lastRow As Long Dim rowPtr As Long lastRow = Range("B" & Rows.Count).End(xlUp).Row For rowPtr = lastRow To 2 Step -1 If Not IsEmpty(Range("B" & rowPtr)) T

我有一个代码,当单元格值更改时,它将插入一个空行

我如何修改它以添加厚底边框?每当列
B
中的值更改时,边框将从列
A
转到列
AB

Sub InsertRows()
Dim lastRow As Long
Dim rowPtr As Long

lastRow = Range("B" & Rows.Count).End(xlUp).Row
For rowPtr = lastRow To 2 Step -1
    If Not IsEmpty(Range("B" & rowPtr)) Then
        If Range("B" & rowPtr) <> Range("B" & rowPtr - 1) Then
            Range("B" & rowPtr).EntireRow.Insert
        End If
    End If
Next

End Sub
子插入行()
最后一排一样长
昏暗的划船和长的划船一样
lastRow=范围(“B”和Rows.Count).End(xlUp).Row
对于rowPtr=lastRow至2步骤-1
如果不是IsEmpty(范围(“B”和rowPtr)),则
如果范围(“B”和rowPtr)范围(“B”和rowPtr-1),则
范围(“B”和行ptr).EntireRow.Insert
如果结束
如果结束
下一个
端接头
试试:


基本上,用代码替换
范围(“B”&rowPtr).EntireRow.Insert
代码以添加边框

你的代码被重构了,再加上一些其他的东西

Sub BorderRows()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim rowPtr As Long
    Dim columnsToBorder As Long

    Set ws = ActiveSheet
    columnsToBorder = 28
    With ws
        lastRow = .Range("B" & .Rows.Count).End(xlUp).Row
        'Optional, clear existing borders
        With .UsedRange.Resize(, columnsToBorder)
            .Borders(xlEdgeTop).LineStyle = xlNone
            .Borders(xlEdgeBottom).LineStyle = xlNone
            .Borders(xlInsideHorizontal).LineStyle = xlNone
        End With

        For rowPtr = 2 To lastRow
            If Not IsEmpty(.Cells(rowPtr, 2)) Then
                If .Cells(rowPtr, 2).Value <> .Cells(rowPtr - 1, 2).Value Then
                    With .Cells(rowPtr, 1).Resize(1, columnsToBorder).Borders(xlEdgeTop)
                        .LineStyle = xlContinuous
                        .ColorIndex = xlAutomatic
                        .TintAndShade = 0
                        .Weight = xlThick
                    End With
                End If
            End If
        Next

        'Optional, add borber to last row
        With .Cells(lastRow, 1).Resize(1, columnsToBorder).Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Weight = xlThick
        End With

    End With
End Sub
子边界行()
将ws设置为工作表
最后一排一样长
昏暗的划船和长的划船一样
暗淡的柱状物与边缘一样长
设置ws=ActiveSheet
columnsToBorder=28
与ws
lastRow=.Range(“B”和.Rows.Count).End(xlUp).Row
'可选,清除现有边界
使用.UsedRange.Resize(,columnsToBorder)
.Borders(xlEdgeTop).LineStyle=xlNone
.Borders(xlEdgeBottom).LineStyle=xlNone
.Borders(xlInsideHorizontal).LineStyle=xlNone
以
对于行ptr=2到最后一行
如果不是空的(.Cells(rowPtr,2)),则
If.Cells(rowPtr,2).Value.Cells(rowPtr-1,2).Value然后
使用.Cells(rowPtr,1)。调整大小(1,columnsToBorder)。边框(xlEdgeTop)
.LineStyle=xlContinuous
.ColorIndex=xlAutomatic
.TintAndShade=0
.重量=xl厚
以
如果结束
如果结束
下一个
'可选,将borber添加到最后一行
使用.Cells(lastRow,1)。调整大小(1,columnsToBorder)。边框(xlEdgeBottom)
.LineStyle=xlContinuous
.ColorIndex=xlAutomatic
.TintAndShade=0
.重量=xl厚
以
以
端接头

我想这是Excel。它看起来更像VBA宏,而不是vb.net代码。它是Excel vb。Excel vb可能是VBA。你在哪里写代码?在Visual Studio或Excel中?在Excel中。它作为宏执行。只是不知道如何写,一旦列B中的值更改为在列B中的值已更改的行中从列a向列AB添加边框,您将获得有关不同标记的更多帮助。vb.net仅适用于通常在Visual Studio中使用vb.net语言编写的代码,该语言与VBA不同。它仅选择值更改的B列。当B中的值发生变化时,如何将其从A更改为AB?抱歉,错过了该部分。使范围函数为:Range(“A”&rowPtr&“:AB”&rowPtr)。选择
Sub BorderRows()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim rowPtr As Long
    Dim columnsToBorder As Long

    Set ws = ActiveSheet
    columnsToBorder = 28
    With ws
        lastRow = .Range("B" & .Rows.Count).End(xlUp).Row
        'Optional, clear existing borders
        With .UsedRange.Resize(, columnsToBorder)
            .Borders(xlEdgeTop).LineStyle = xlNone
            .Borders(xlEdgeBottom).LineStyle = xlNone
            .Borders(xlInsideHorizontal).LineStyle = xlNone
        End With

        For rowPtr = 2 To lastRow
            If Not IsEmpty(.Cells(rowPtr, 2)) Then
                If .Cells(rowPtr, 2).Value <> .Cells(rowPtr - 1, 2).Value Then
                    With .Cells(rowPtr, 1).Resize(1, columnsToBorder).Borders(xlEdgeTop)
                        .LineStyle = xlContinuous
                        .ColorIndex = xlAutomatic
                        .TintAndShade = 0
                        .Weight = xlThick
                    End With
                End If
            End If
        Next

        'Optional, add borber to last row
        With .Cells(lastRow, 1).Resize(1, columnsToBorder).Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Weight = xlThick
        End With

    End With
End Sub