AdR*_*ock 1 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
Run Code Online (Sandbox Code Playgroud)
这是我的整个代码
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
Run Code Online (Sandbox Code Playgroud)
这行不符合你的想法: If Cells(6, i) Then
它等同于:If Cells(6, i).Value = True Then,即"如果第6行和列上的单元格内容的i计算结果是True隐式强制转换为布尔值,那么",这显然不是你想要的.相反,尝试:
If ActiveCell.Row = 6 Then
Run Code Online (Sandbox Code Playgroud)
同样的推理If Cells(42, i) Then在你的代码中进一步说明.
| 归档时间: |
|
| 查看次数: |
10955 次 |
| 最近记录: |