本帖最后由 kuaibiao_ 于 2022-8-8 17:20 编辑
锁定单元格的VBA
效果:是否返回的值是"是",返回时间可填内容,反之不可填内容。
设置方法如下:
1.给需要提示的区域添加条件格式
2.在需求提示的区域右侧添加公式
VBA代码:使用方法,把T_238替换成自己的表名,把F_51630_替换成需要判断的字段别名- Imports SpreadsheetGear
- Imports C1.Silverlight
- Imports C1.Silverlight.C1MessageBoxButton
- Imports C1.Silverlight.C1MessageBoxIcon
- Imports Newtonsoft.Json
- Imports Newtonsoft.Json.Linq
- Imports System.Windows.forms
- Namespace ExcelViewVBDotnet
- Public Class StandardInterface
-
- '预留位置1
- '预留位置2
- Dim EV As SpreadsheetGear.Windows.Controls.WorkbookView,AW As SpreadsheetGear.IWorkbook,Range As SpreadsheetGear.IRange
-
- Public Sub Workbook_Open(OldRoot As Object,NewRoot As Object,Excel As SpreadsheetGear.Windows.Controls.WorkbookView)
- EV = Excel
- Excel.GetLock()
- AW = Excel.ActiveWorkbook
- Range = AW.ActiveWorksheet.Range
- Excel.ReleaseLock()
-
- End Sub'打开时执行事件
- Public Sub RangeSelection(sender As Object,e As SpreadsheetGear.Windows.Controls.RangeSelectionChangedEventArgs)
- ActiveWorkbook.WorkbookSet.GetLock()
- Dim Rng As IRange = e.RangeSelection.Intersect(Range("T_238"))
- Dim MyRange As IRange
- If Rng IsNot Nothing Then
- ' C1MessageBox.Show("你好1!", "提示", OK, Information)
- ActiveWorkbook.ActiveWorksheet.ProtectContents = False
- For Each MyRange In Range("F_51630_")
- If MyRange.value = "TRUE" Then '单元格底色是否红色
- MyRange.Cells(0,-1).Locked = True
- ' C1MessageBox.Show("你好2!", "提示", OK, Information)
- Else
- MyRange.Cells(0,-1).Locked = False
- End If
- Next
- End If
- ActiveWorkbook.WorkbookSet.ReleaseLock()
-
- End Sub'单元格选择后执行的事件
- Public Sub RangeChanged(sender As Object,e As SpreadsheetGear.Windows.Controls.RangeChangedEventArgs)
- End Sub'单元格编辑完成后执行事件
- Public Sub ButtunClick(sender As Object,e As SpreadsheetGear.Windows.Controls.ShapeActionEventArgs)
- End Sub'按钮/标签点击事件
- Public Sub FollowHyperlink(sender As Object)
- End Sub '暂不支持
- End Class
- ' 注:除事件字眼下可以自定义代码外的所有代码不允许改动,否则编译将有可能失败。
- End Namespace
复制代码
|
|
kuaibiao_