I am writing some VBA for a Gantt Chart spreadsheet.
I have 3 months worth of dates on row 5 and i can set the start date by entering a date in a cell which updates the whole sheet.
I have a range of cells for my chart L6:CZ42. For this range, if the cell in row 5 is the 1st of the month, every cell in that column will have a grey dotted left border and nothing on the right. This works how i want it to.
The problem is that it adds a grey border to the top and bottom of the cell which is OK for rows 7 to 41, but with row 6 i want a black top border and for row 42 I want a black bottom border.
I added this section of code trying to sort this problem but the syntax is wrong checking if it's on row 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
This is my whole code
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
This line does not do what you think it does: If Cells(6, i) Then
It is equivalent to saying: If Cells(6, i).Value = True Then
, ie "if the content of cell on row 6 and column i
evaluates to True
when implicitly coerced to a Boolean, then", which clearly is not what you want at all. Instead, try:
If ActiveCell.Row = 6 Then
The same reasoning goes for If Cells(42, i) Then
further down in your code.
[ Update: Jean-François Corbett has corrected your code logic re: checking if the active cell is in row 6. But with the typo the code doesn't produce the top and bottom borders.]
Your code does not compile. Please consider using Option Explicit
at the top of your code modules. The only way I could duplicate your issue was by removing Option Explicit
. I set up the VBA Editor so it automatically places Option Explicit
at the top of new modules.
The LineStyle Property must be one of the XlLineStyle constants:
In your code, you wrote xlContinuos not xlContinuous . Once you make this correction the code should work.
Also, this is a minor point but technically the Worksheet_Change Event should be declared as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
May I kindly suggest you take advantage of the built-in features of the VBA Editor as well as the help documentation?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.