EXCEL使用VBA做一款多功能笔记模板

软件环境:

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

效果演示:

所有单元格的填充都为淡蓝色。非空单元格宽度自动调整,自动加浅灰色边框;空单元格宽度为默认值,没有边框。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值