我正在为甘特图电子表格编写一些 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