效果圖:
gif好像不動(dòng)
使用方法:選中需要添加復(fù)選框的單元格,之后運(yùn)行宏命令。
原理:使用輔助列來(lái)存儲(chǔ)復(fù)選框的狀態(tài)(true/false)趋箩,同時(shí)給單元格添加條件格式畦娄,輔助列為true時(shí),當(dāng)前單元格變?yōu)榫G色熟尉。
進(jìn)階:
.Width / 2
把復(fù)選框的寬度縮小了一半归露,不然會(huì)導(dǎo)致復(fù)選框擋住單元格導(dǎo)致無(wú)法選中單元格,可以自行修改寬度數(shù)值斤儿。
rng.Cells.Offset(0,5)
表示了輔助列偏移5列靶擦,方便把輔助列放在靠右位置進(jìn)行隱藏,可以更改括號(hào)內(nèi)數(shù)字進(jìn)行行雇毫、列的偏移玄捕。
.Color
表示單元格變色的顏色,可以自行修改棚放。
Application.ScreenUpdating = False
Dim rng As Range
For Each rng In Selection
With rng
With Sheet1.CheckBoxes.Add(.Left, .Top, .Width / 2, .Height) '.Select
.Value = xlOff
.Caption = ""
.LinkedCell = rng.Cells.Offset(0, 5).Address
xAddr = "=" & rng.Cells.Offset(0, 5).Address
Application.CutCopyMode = False
rng.FormatConditions.Add Type:=xlExpression, Formula1:=xAddr
rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
With rng.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
End With
rng.FormatConditions(1).StopIfTrue = False
End With
End With
Next
Application.ScreenUpdating = True
End Sub```