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

扫码微信咨询

关注公众号

关注微信公众号

电话: 021 5161 9370
返回顶部
求助 vba中如何将高亮显示限制在指定范围内
显示全部楼层 倒序浏览 发表日期 2024-12-27 09:20:09 2429次阅读 3次回复
微信图片_20241227091759.png
  1. Dim currentRow As Long, currentCol As Long
  2.                     Dim colorIndex As Long
  3.                         If GL Then
  4.                             ' 获取当前选中单元格的行号和列号
  5.         '                        msgbox("1")
  6.                             currentRow = e.RangeSelection.Row
  7.                             currentCol = e.RangeSelection.Column
  8.         '                    msgbox(currentRow & "12" & currentCol)
  9.                             ' 定义要使用的颜色索引(可以根据需要修改)
  10.                             colorIndex = 16 ' 例如,使用黄色(索引6)
  11.                             
  12.                             ' 清除之前的高亮(如果有的话)
  13.                             With AW.ActiveWorksheet.UsedRange.Interior
  14.                                         .Pattern = SpreadsheetGear.Pattern .None
  15.                             End With
  16.         '                    msgbox("1234")
  17.                             ' 高亮显示当前行和列
  18.                                 If e.RangeSelection.Intersect(Range("T_113")) IsNot Nothing Then
  19.                                     With e.RangeSelection.EntireRow.Interior  
  20.         '                                With Range("T_113").Rows(currentRow).EntireRow.Interior
  21.                                         .colorIndex = colorIndex
  22.                                     End With
  23.                 '                        msgbox("12345")
  24.                                     With e.RangeSelection.EntireColumn.Interior
  25.         '               With Range("T_113").EntireColumn.Interior
  26.                                         .colorIndex = colorIndex
  27.                                     End With       
  28.                                 End If
  29.                         End If
复制代码
e.RangeSelection.EntireRow.Interior 只能设置到整行 或是整列
  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
  17.         Dim AW As SpreadsheetGear.IWorkbook
  18.         Dim ActiveRange As SpreadsheetGear.IRange

  19.         ' ===== 十字高亮相关变量 =====
  20.         ' 高亮区域边界常量
  21.         Const RANGE_ROW_START As Integer = 1   ' A2 → 行索引从0起,第2行=索引1
  22.         Const RANGE_ROW_END   As Integer = 49  ' 第50行=索引49
  23.         Const RANGE_COL_START As Integer = 0   ' A列=索引0
  24.         Const RANGE_COL_END   As Integer = 25  ' Z列=索引25

  25.         ' 高亮颜色
  26.         Dim HIGHLIGHT_COLOR As SpreadsheetGear.Color = SpreadsheetGear.Colors.Yellow

  27.         ' 上次高亮的行列(-1 表示无)
  28.         Dim lastRow As Integer = -1
  29.         Dim lastCol As Integer = -1

  30.         ' 保存原始颜色:rowColors(i) = 第i列单元格原色(同行高亮时)
  31.         '              colColors(i) = 第i行单元格原色(同列高亮时)
  32.         Dim savedRowColors() As Integer   ' 保存上次高亮行的各列原色
  33.         Dim savedColColors() As Integer   ' 保存上次高亮列的各行原色
  34.         Dim savedCrossColor As Integer    ' 保存交叉单元格原色(防止双重覆盖)

  35.         Public Sub Workbook_Open(OldRoot As Object, NewRoot As Object, Excel As SpreadsheetGear.Windows.Controls.WorkbookView)
  36.             EV = Excel
  37.             Excel.GetLock()
  38.             AW = Excel.ActiveWorkbook
  39.             ActiveRange = AW.ActiveWorksheet.Cells
  40.             Excel.ReleaseLock()
  41.         End Sub '打开时执行事件

  42.         Public Sub RangeSelection(sender As Object, e As SpreadsheetGear.Windows.Controls.RangeSelectionChangedEventArgs)
  43.             EV.GetLock()
  44.             Try
  45.                 Dim cell As SpreadsheetGear.IRange = e.RangeSelection
  46.                 If cell Is Nothing Then Return

  47.                 Dim newRow As Integer = cell.Row
  48.                 Dim newCol As Integer = cell.Column

  49.                 ' 判断选中单元格是否在目标区域内
  50.                 Dim inRange As Boolean = (newRow >= RANGE_ROW_START AndAlso newRow <= RANGE_ROW_END _
  51.                                     AndAlso newCol >= RANGE_COL_START AndAlso newCol <= RANGE_COL_END)

  52.                 Dim ws As SpreadsheetGear.IWorksheet = AW.ActiveWorksheet

  53.                 ' ── Step 1:恢复上次高亮区域的原始颜色 ──
  54.                 If lastRow >= 0 AndAlso lastCol >= 0 Then
  55.                     ' 恢复上次高亮的行(跳过交叉点,最后单独恢复)
  56.                     For c As Integer = RANGE_COL_START To RANGE_COL_END
  57.                         If c <> lastCol Then
  58.                             ws.Cells(lastRow, c).Interior.Color = SpreadsheetGear.Color.FromArgb(savedRowColors(c - RANGE_COL_START))
  59.                         End If
  60.                     Next
  61.                     ' 恢复上次高亮的列(跳过交叉点)
  62.                     For r As Integer = RANGE_ROW_START To RANGE_ROW_END
  63.                         If r <> lastRow Then
  64.                             ws.Cells(r, lastCol).Interior.Color = SpreadsheetGear.Color.FromArgb(savedColColors(r - RANGE_ROW_START))
  65.                         End If
  66.                     Next
  67.                     ' 恢复交叉点原色
  68.                     ws.Cells(lastRow, lastCol).Interior.Color = SpreadsheetGear.Color.FromArgb(savedCrossColor)
  69.                 End If

  70.                 ' ── Step 2:若新选中单元格在区域内,保存并高亮 ──
  71.                 If inRange Then
  72.                     Dim colCount As Integer = RANGE_COL_END - RANGE_COL_START + 1
  73.                     Dim rowCount As Integer = RANGE_ROW_END - RANGE_ROW_START + 1

  74.                     ReDim savedRowColors(colCount - 1)
  75.                     ReDim savedColColors(rowCount - 1)

  76.                     ' 保存新高亮行的各单元格原色
  77.                     For c As Integer = RANGE_COL_START To RANGE_COL_END
  78.                         savedRowColors(c - RANGE_COL_START) = ws.Cells(newRow, c).Interior.Color.ToArgb()
  79.                     Next
  80.                     ' 保存新高亮列的各单元格原色
  81.                     For r As Integer = RANGE_ROW_START To RANGE_ROW_END
  82.                         savedColColors(r - RANGE_ROW_START) = ws.Cells(r, newCol).Interior.Color.ToArgb()
  83.                     Next
  84.                     ' 交叉点的原色(行保存中已含,此处单独记录供精确恢复)
  85.                     savedCrossColor = ws.Cells(newRow, newCol).Interior.Color.ToArgb()

  86.                     ' 高亮整行(在范围内)
  87.                     For c As Integer = RANGE_COL_START To RANGE_COL_END
  88.                         ws.Cells(newRow, c).Interior.Color = HIGHLIGHT_COLOR
  89.                     Next
  90.                     ' 高亮整列(在范围内)
  91.                     For r As Integer = RANGE_ROW_START To RANGE_ROW_END
  92.                         ws.Cells(r, newCol).Interior.Color = HIGHLIGHT_COLOR
  93.                     Next

  94.                     lastRow = newRow
  95.                     lastCol = newCol
  96.                 Else
  97.                     ' 选中区域外,清除记录
  98.                     lastRow = -1
  99.                     lastCol = -1
  100.                 End If

  101.             Finally
  102.                 EV.ReleaseLock()
  103.             End Try
  104.         End Sub '单元格选择后执行的事件

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

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

  107.         Public Sub ButtunClick(sender As Object, e As SpreadsheetGear.Windows.Controls.ShapeActionEventArgs)

  108.         End Sub '按钮/标签点击事件

  109.         Public Sub FollowHyperlink(sender As Object, e As EventArgs)

  110.         End Sub '暂不支持

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

本帖最后由 快表帝国客服01 于 2025-1-10 08:28 编辑

将currentRow与 currentCol限制在T_113的区域行列之内,也即是行号大于区域的第一行 小于最后一行,列大于第一列小于最后一列,在整个范围内起作用
另:企业版默认支持该功能,不用自定义写VBA
If e.RangeSelection.Intersect(Range("T_123")) IsNot Nothing Then
        MessageBox.Show("YES!")
End If

精彩评论3

  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
  17.         Dim AW As SpreadsheetGear.IWorkbook
  18.         Dim ActiveRange As SpreadsheetGear.IRange

  19.         ' ===== 十字高亮相关变量 =====
  20.         ' 高亮区域边界常量
  21.         Const RANGE_ROW_START As Integer = 1   ' A2 → 行索引从0起,第2行=索引1
  22.         Const RANGE_ROW_END   As Integer = 49  ' 第50行=索引49
  23.         Const RANGE_COL_START As Integer = 0   ' A列=索引0
  24.         Const RANGE_COL_END   As Integer = 25  ' Z列=索引25

  25.         ' 高亮颜色
  26.         Dim HIGHLIGHT_COLOR As SpreadsheetGear.Color = SpreadsheetGear.Colors.Yellow

  27.         ' 上次高亮的行列(-1 表示无)
  28.         Dim lastRow As Integer = -1
  29.         Dim lastCol As Integer = -1

  30.         ' 保存原始颜色:rowColors(i) = 第i列单元格原色(同行高亮时)
  31.         '              colColors(i) = 第i行单元格原色(同列高亮时)
  32.         Dim savedRowColors() As Integer   ' 保存上次高亮行的各列原色
  33.         Dim savedColColors() As Integer   ' 保存上次高亮列的各行原色
  34.         Dim savedCrossColor As Integer    ' 保存交叉单元格原色(防止双重覆盖)

  35.         Public Sub Workbook_Open(OldRoot As Object, NewRoot As Object, Excel As SpreadsheetGear.Windows.Controls.WorkbookView)
  36.             EV = Excel
  37.             Excel.GetLock()
  38.             AW = Excel.ActiveWorkbook
  39.             ActiveRange = AW.ActiveWorksheet.Cells
  40.             Excel.ReleaseLock()
  41.         End Sub '打开时执行事件

  42.         Public Sub RangeSelection(sender As Object, e As SpreadsheetGear.Windows.Controls.RangeSelectionChangedEventArgs)
  43.             EV.GetLock()
  44.             Try
  45.                 Dim cell As SpreadsheetGear.IRange = e.RangeSelection
  46.                 If cell Is Nothing Then Return

  47.                 Dim newRow As Integer = cell.Row
  48.                 Dim newCol As Integer = cell.Column

  49.                 ' 判断选中单元格是否在目标区域内
  50.                 Dim inRange As Boolean = (newRow >= RANGE_ROW_START AndAlso newRow <= RANGE_ROW_END _
  51.                                     AndAlso newCol >= RANGE_COL_START AndAlso newCol <= RANGE_COL_END)

  52.                 Dim ws As SpreadsheetGear.IWorksheet = AW.ActiveWorksheet

  53.                 ' ── Step 1:恢复上次高亮区域的原始颜色 ──
  54.                 If lastRow >= 0 AndAlso lastCol >= 0 Then
  55.                     ' 恢复上次高亮的行(跳过交叉点,最后单独恢复)
  56.                     For c As Integer = RANGE_COL_START To RANGE_COL_END
  57.                         If c <> lastCol Then
  58.                             ws.Cells(lastRow, c).Interior.Color = SpreadsheetGear.Color.FromArgb(savedRowColors(c - RANGE_COL_START))
  59.                         End If
  60.                     Next
  61.                     ' 恢复上次高亮的列(跳过交叉点)
  62.                     For r As Integer = RANGE_ROW_START To RANGE_ROW_END
  63.                         If r <> lastRow Then
  64.                             ws.Cells(r, lastCol).Interior.Color = SpreadsheetGear.Color.FromArgb(savedColColors(r - RANGE_ROW_START))
  65.                         End If
  66.                     Next
  67.                     ' 恢复交叉点原色
  68.                     ws.Cells(lastRow, lastCol).Interior.Color = SpreadsheetGear.Color.FromArgb(savedCrossColor)
  69.                 End If

  70.                 ' ── Step 2:若新选中单元格在区域内,保存并高亮 ──
  71.                 If inRange Then
  72.                     Dim colCount As Integer = RANGE_COL_END - RANGE_COL_START + 1
  73.                     Dim rowCount As Integer = RANGE_ROW_END - RANGE_ROW_START + 1

  74.                     ReDim savedRowColors(colCount - 1)
  75.                     ReDim savedColColors(rowCount - 1)

  76.                     ' 保存新高亮行的各单元格原色
  77.                     For c As Integer = RANGE_COL_START To RANGE_COL_END
  78.                         savedRowColors(c - RANGE_COL_START) = ws.Cells(newRow, c).Interior.Color.ToArgb()
  79.                     Next
  80.                     ' 保存新高亮列的各单元格原色
  81.                     For r As Integer = RANGE_ROW_START To RANGE_ROW_END
  82.                         savedColColors(r - RANGE_ROW_START) = ws.Cells(r, newCol).Interior.Color.ToArgb()
  83.                     Next
  84.                     ' 交叉点的原色(行保存中已含,此处单独记录供精确恢复)
  85.                     savedCrossColor = ws.Cells(newRow, newCol).Interior.Color.ToArgb()

  86.                     ' 高亮整行(在范围内)
  87.                     For c As Integer = RANGE_COL_START To RANGE_COL_END
  88.                         ws.Cells(newRow, c).Interior.Color = HIGHLIGHT_COLOR
  89.                     Next
  90.                     ' 高亮整列(在范围内)
  91.                     For r As Integer = RANGE_ROW_START To RANGE_ROW_END
  92.                         ws.Cells(r, newCol).Interior.Color = HIGHLIGHT_COLOR
  93.                     Next

  94.                     lastRow = newRow
  95.                     lastCol = newCol
  96.                 Else
  97.                     ' 选中区域外,清除记录
  98.                     lastRow = -1
  99.                     lastCol = -1
  100.                 End If

  101.             Finally
  102.                 EV.ReleaseLock()
  103.             End Try
  104.         End Sub '单元格选择后执行的事件

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

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

  107.         Public Sub ButtunClick(sender As Object, e As SpreadsheetGear.Windows.Controls.ShapeActionEventArgs)

  108.         End Sub '按钮/标签点击事件

  109.         Public Sub FollowHyperlink(sender As Object, e As EventArgs)

  110.         End Sub '暂不支持

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

本帖最后由 快表帝国客服01 于 2025-1-10 08:28 编辑

将currentRow与 currentCol限制在T_113的区域行列之内,也即是行号大于区域的第一行 小于最后一行,列大于第一列小于最后一列,在整个范围内起作用
另:企业版默认支持该功能,不用自定义写VBA
If e.RangeSelection.Intersect(Range("T_123")) IsNot Nothing Then
        MessageBox.Show("YES!")
End If
您需要登录后才可以回帖 登录 | 立即注册

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

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

  • 微信小商店

  • 微信客服

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