如果某行上的事件单元格,Excel VBA会添加边框

标签 excel vba border cells

我正在为甘特图电子表格编写一些 VBA。

我在第 5 行有 3 个月的日期,我可以通过在更新整个工作表的单元格中输入日期来设置开始日期。

我的图表 L6:CZ42 有一系列单元格。对于此范围,如果第 5 行中的单元格是该月的 1 号,则该列中的每个单元格都将有一个灰色虚线左边框,而右侧没有任何内容。这可以按我的意愿工作。

问题是它在单元格的顶部和底部添加了一个灰色边框,这对于第 7 到 41 行是可以的,但是对于第 6 行,我想要一个黑色的顶部边框,而对于第 42 行,我想要一个黑色的底部边框。

我添加了这部分代码试图对这个问题进行排序,但语法错误,检查它是否在第 6 行

' If this is the first row (6) in the range then
' add a black continuous border to the top
If Cells(6, i) Then
    With .Borders(xlEdgeTop)
        .ColorIndex = 1
        .Weight = xlThin
        .LineStyle = xlContinuos
    End With
End If

这是我的全部代码
Sub Worksheet_Change(ByVal Target As Range)

Dim i As Long
Dim CuDate As Date


For i = 12 To 104
CuDate = Cells(5, i).Value

' Are we on the 1st day of the month
If Day(CuDate) = 1 Then
    With Range(Cells(6, i), Cells(42, i))
        ' If this is the first row (6) in the range then
        ' add a black continuous border to the top
        If Cells(6, i) Then
            With .Borders(xlEdgeTop)
                .ColorIndex = 1
                .Weight = xlThin
                .LineStyle = xlContinuos
            End With
        End If

        With .Borders(xlEdgeLeft)
            .ColorIndex = 15
            .Weight = xlThin
            .LineStyle = xlDot
        End With
        With .Borders(xlEdgeRight)
            .LineStyle = xlLineStyleNone
        End With
    End With
Else
    With Range(Cells(6, i), Cells(42, i))
        ' If this is the last row (42) in the range then
        ' add a black continuous border to the bottom
        If Cells(42, i) Then
            With .Borders(xlEdgeBottom)
                .ColorIndex = 1
                .Weight = xlThin
                .LineStyle = xlContinuos
            End With
        End If

        With .Borders(xlEdgeLeft)
            .LineStyle = xlLineStyleNone
        End With
        With .Borders(xlEdgeRight)
            .LineStyle = xlLineStyleNone
        End With
    End With
End If

Next
End Sub

最佳答案

这条线并不像你认为的那样:If Cells(6, i) Then
相当于说:If Cells(6, i).Value = True Then ,即“如果第 6 行和第 i 列的单元格内容计算为 True 当隐式强制为 bool 值时,则”,这显然不是您想要的。相反,请尝试:

If ActiveCell.Row = 6 Then
If Cells(42, i) Then 也是同样的道理。在您的代码中进一步向下。

关于如果某行上的事件单元格,Excel VBA会添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10716082/

相关文章:

vba - VBA 项目可以从 Office 2007 访问哪些表单控件?

javascript - 如何禁用Ext JS面板和工具栏的边框线?

css - 使用 Css 将 Tr 边框底部放在 Td 边框上,留在 TABLE 中

python - 合并 Pandas 列(一对多)

excel - VBA错误: "Compile error: Expected End Sub"

Excel VBA 的正则表达式正向回顾问题

vba - 将值输入到另一个宏调用的输入框

android - 在 Android 上的形状上绘制顶部边框

vba - 使用 VBA 从 Excel 运行 bat 文件

vba - Excel VBA代码移动带有图像的工作表添加屏幕更新和错误