周一至周五 : 08:30 - 17:30 客服专员电话/微信:17301649370 QQ:2902102332
微信咨询

扫码微信咨询

关注公众号

关注微信公众号

电话: 021 5161 9370
返回顶部
快表VBA标记单元格字体颜色为红色以及取消
显示全部楼层 倒序浏览 发表日期 2025-07-02 16:59:03 28次阅读 0次回复
通过快表VBA功能,对明细表单元格的字体颜色进行标记红。

图示效果

图示效果

红色=Button 1  ,全部取消=Button 2  ,选中取消=Button 3  ,保存=Button 4  ,F_9269为存储标记为红色字体的单元格地址
  1. Imports SpreadsheetGear
  2. Imports C1.Silverlight
  3. Imports C1.Silverlight.C1MessageBoxButton
  4. Imports C1.Silverlight.C1MessageBoxIcon
  5. Imports Newtonsoft.Json
  6. Imports Newtonsoft.Json.Linq
  7. Imports Microsoft.VisualBasic
  8. Imports System.IO
  9. Imports System.Text
  10. Imports System.Net
  11. Imports System.Windows.forms
  12. Namespace ExcelViewVBDotnet
  13.         Public Class StandardInterface
  14.                 '预留位置1
  15.                 '预留位置2
  16.                 Dim EV As SpreadsheetGear.Windows.Controls.WorkbookView, AW As SpreadsheetGear.IWorkbook, Range As SpreadsheetGear.IRange
  17.                 Dim CF As TextBox
  18.                 Public Sub Workbook_Open(OldRoot As Object, NewRoot As Object, Excel As SpreadsheetGear.Windows.Controls.WorkbookView)
  19.                         EV = Excel
  20.                         Excel.GetLock()
  21.                         AW = Excel.ActiveWorkbook
  22.                         Range = AW.ActiveWorksheet.Range
  23.                         Excel.ReleaseLock()
  24.                         CF = CType(OldRoot.Children(1), TextBox)
  25.                         EV.getlock()
  26.                         ActiveWorkBook.Worksheets("sheet1").ProtectContents = False '查阅时解锁
  27.                        
  28.                         If App.Type= 3 Then
  29.                                 '填写符合条件后执行的代码
  30.                                 AW.ActiveWorksheet.Shapes("Button 4").Visible = False
  31.                                 Else
  32.                                         AW.ActiveWorksheet.Shapes("Button 4").Visible = True
  33.                         End If
  34.            
  35.                         If Range("F_9269").Value <> "" Then '打开时遍历指定地址
  36.                                 Dim bb As String = Range("F_9269").Value.ToString()
  37.                                 Dim cc() As String = bb.split(",")
  38.                                 Dim cell As String

  39.                                 For Each cell In cc
  40.                                         If cell <> "" Then
  41.                                                 AW.ActiveWorksheet.Range(cell).Font.ColorIndex = 2
  42.                                         End If
  43.                                 Next cell
  44.                         End If
  45.              ActiveWorkBook.Worksheets("sheet1").ProtectContents = True '查阅时执行完成后锁定
  46.                         EV.ReleaseLock()
  47.             
  48.                 End Sub'打开时执行事件

  49.                 Public Sub RangeSelection(sender As Object, e As SpreadsheetGear.Windows.Controls.RangeSelectionChangedEventArgs)

  50.                 End Sub'单元格选择后执行的事件

  51.                 Public Sub RangeChanged(sender As Object, e As SpreadsheetGear.Windows.Controls.RangeChangedEventArgs)

  52.                 End Sub'单元格编辑完成后执行事件

  53.                 Public Sub ButtunClick(sender As Object, e As SpreadsheetGear.Windows.Controls.ShapeActionEventArgs)
  54.                         EV.GetLock()
  55.             
  56.                         If e.Shape.Name = "Button 1" Then '对选择单元格标记颜色
  57.                                 Dim addr As String = EV.RangeSelection.Address
  58.                                 Dim address As String
  59.                                 Dim aa As String = Range("F_9269").Text
  60.                                 AW.ActiveWorksheet.Range(addr).Font.ColorIndex = 2
  61.                                 ' address =aa + addr +","
  62.                                 ' Range("F_9269").Value = address
  63.                         End If


  64.                         If e.Shape.Name = "Button 2" Then '全部取消颜色

  65.                                 range("T_855").Font.ColorIndex = 0
  66.                         End If

  67.                         If e.Shape.Name = "Button 3" Then '单个取消颜色
  68.                                 Dim addr As String = EV.RangeSelection.Address
  69.                                 Dim address As String
  70.                                 Dim aa As String = Range("F_9269").Text
  71.                                 AW.ActiveWorksheet.Range(addr).Font.ColorIndex = 0
  72.                                 'address = addr -aa +","
  73.                                 'Range("F_9269").Value = address
  74.                         End If

  75.                         ' ActiveWorkbook.WorkbookSet.GetLock()
  76.                         If e.Shape.Name = "Button 4" Then
  77.                                 CF.Text = "#保存"
  78.                                 '填写符合条件后执行的代码
  79.                                 Dim address As String
  80.                                 Dim cell As IRange

  81.                                 For Each cell In AW.ActiveWorksheet.UsedRange
  82.                                         If cell.Font.ColorIndex = 2 Then

  83.                                                 address = cell.Address + "," + address
  84.                                                 Range("F_9269").Value = address
  85.                                         End If
  86.                                 Next cell


  87.                         End If
  88.             
  89.                         EV.ReleaseLock()
  90.                        
  91.                 End Sub'按钮/标签点击事件

  92.                 Public Sub FollowHyperlink(sender As Object)

  93.                 End Sub '暂不支持

  94.         End Class
  95.         '  注:除事件字眼下可以自定义代码外的所有代码不允许改动,否则编译将有可能失败。
  96. End Namespace
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则  允许回帖邮件提醒楼主

快表软件是国内较早研究表格类软件开发平台的团队之一,迄今已有十多年的行业经验.致力于为企事业单位提供实用可靠的数字化平台。
  • 微信公众号

  • 微信小商店

  • 微信客服

  • Powered by Discuz! X3.4 | Copyright © 2022-2024, XiRong Soft. | 快表软件
  • 沪ICP备13033196号 | 营业执照 |上海西戎软件科技有限公司|沪公网安备31011502002146号|沪ICP备13033196号 |