软件环境:
EXCEL2019
制作背景:
OneNote自带的表格不能合并单元格,不能垂直居中,买了珍宝插件,发现珍宝插件用的并不是OneNote原生的表格,虽然能合并单元格,但是使用手感远不如OneNote原生的。最后想到EXCEL的表格功能很强大,支持合并单元格、垂直居中,所以尝试使用EXCEL来代替OneNote里的表格,并且通过VBA代码,让EXCEL的界面、手感更像OneNote。
VBA代码:
右击工作表标签,点击查看代码。
输入下边这些代码,保存。
Private Sub Worksheet_Change(ByVal Target As Range)
Cells.EntireColumn.ColumnWidth = 8.11
Cells.EntireColumn.AutoFit
Cells.FormatConditions.Delete
Cells.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEN(TRIM(A1))=0"
Cells.FormatConditions(Cells.FormatConditions.Count).SetFirstPriority
With Cells.FormatConditions(1).Interior
.Pattern = xlPatternLinearGradient
.Gradient.Degree = 90
.Gradient.ColorStops.Clear
End With
With Cells.FormatConditions(1).Interior.Gradient.ColorStops.Add(0)
.Color = 16446444
.TintAndShade = 0
End With
With Cells.FormatConditions(1).Interior.Gradient.ColorStops.Add(1)
.Color = 16446444
.TintAndShade = 0
End With
Cells.FormatConditions(1).StopIfTrue = False
Cells.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEN(TRIM(A1))>0"
Cells.FormatConditions(Cells.FormatConditions.Count).SetFirstPriority
With Cells.FormatConditions(1).Borders(xlLeft)
.LineStyle = xlContinuous
.Color = -7960438
.TintAndShade = 0
.Weight = xlThin
End With
With Cells.FormatConditions(1).Borders(xlRight)
.LineStyle = xlContinuous
.Color = -7960438
.TintAndShade = 0
.Weight = xlThin
End With
With Cells.FormatConditions(1).Borders(xlTop)
.LineStyle = xlContinuous
.Color = -7960438
.TintAndShade = 0
.Weight = xlThin
End With
With Cells.FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.Color = -7960438
.TintAndShade = 0
.Weight = xlThin
End With
With Cells.FormatConditions(1).Interior
.Pattern = xlPatternLinearGradient
.Gradient.Degree = 90
.Gradient.ColorStops.Clear
End With
With Cells.FormatConditions(1).Interior.Gradient.ColorStops.Add(0)
.Color = 16446444
.TintAndShade = 0
End With
With Cells.FormatConditions(1).Interior.Gradient.ColorStops.Add(1)
.Color = 16446444
.TintAndShade = 0
End With
Cells.FormatConditions(1).StopIfTrue = False
End Sub
效果演示:
所有单元格的填充都为淡蓝色。非空单元格宽度自动调整,自动加浅灰色边框;空单元格宽度为默认值,没有边框。