1、单元格所在行和列高亮

第一种方式

Private Sub worksheet_selectionchange(ByVal target As Range)
Cells.Interior.ColorIndex = xlColorIndexNone
target.EntireRow.Interior.ColorIndex = 6
target.EntireColumn.Interior.ColorIndex = 50
End Sub

第二种方式

Private Sub worksheet_selectionchange(ByVal target As Range)
Dim rng As Range
Cells.Interior.ColorIndex = xlNone
Set rng = Application.Union(target.EntireRow, target.EntireColumn)
rng.Interior.ColorIndex = 56
End Sub

需注意:代码需在sheet中运行,运行区域高亮后不可使用复制粘贴等操作,以前单元格格式会被新格式被覆盖。同时若代码中含单元格值改变时保护单元格也会和上面代码冲突。

2、展示所有颜色代码到单元格中

Sub 颜色()
Dim i
With Sheet2
For i = 1 To 56
.Cells(i, 1).Value = i
.Cells(i, 1).Interior.ColorIndex = i
Next
End With
End Sub

3、数据录入后的单元格自动保护

Private Sub worksheet_selectionchange(ByVal target As Range)
On Error Resume Next
Sheet1.Unprotect Password:="hj1905" '取消工作表单元格保护
If target.Value <> "" Then
target.Locked = True
Sheet1.Protect Password:="hj1905"
End If
End Sub

4、关于事件中的target

1)事件中可使用target的address属性限定target的单元格触发范围

2)target的row和column属性限定行列范围,整行和整列用entirerow 和entirecolumn

3) 用于intersect函数中,也可判定选定某区域执行某个操作

target在

[a1:a10], [c1:c10]范围会弹出相应地址
Private Sub worksheet_selectionchange(ByVal target As Range)
If Not Application.Intersect(target, Union([a1:a10], [c1:c10])) Is Nothing Then
MsgBox "you choose " & target.Address(0, 0)
End If
End Sub

 保护单元格操作写法2:

Private Sub worksheet_selectionchange(ByVal target As Range)
Dim rng As Range
Set rng = Union([b2:c10], [e1:f10])
If Application.Intersect(target, rng) Is Nothing Then Exit Sub
[a1].Select
MsgBox "you cannot edit " & rng.Address(0, 0) & " area"
End Sub

  

 

VBA单元格自适应高亮操作的更多相关文章

  1. C# NPOI Export DataTable C# NPOI导出DataTable 单元格自适应大小

    1.Install-Package NPOI -v 2.4.0 2. using NPOI.XSSF; using NPOI.XSSF.UserModel; using NPOI.SS.UserMod ...

  2. 使用js方法将table表格中指定列指定行中相同内容的单元格进行合并操作。

    前言 使用js方法对html中的table表格进行单元格的行列合并操作. 网上执行此操作的实例方法有很多,但根据实际业务的区别,大多不适用. 所以在网上各位大神写的方法的基础上进行了部分修改以适合自己 ...

  3. 基于.NET的Excel开发:单元格区域的操作(读取、赋值、边框和格式)

    引用 using Excel = Microsoft.Office.Interop.Excel; 定义 Excel.ApplicationClass app; Excel.Workbooks book ...

  4. Excel单元格的日常操作

    通过右键选择插入来移动单元格 灵活的运用"整行" 与 "整列" 选中区域之后 通过点击区域边框进行移动 按住shift之后框会变成线 更容易拖动 按住ctrl拖 ...

  5. vba单元格背景色

    cells(i,j).interior.color=5287936  green =255 red

  6. Excel 2003 中如何用VBA 代码访问单元格里的值及操作单元格 - 唐诗宋词的专栏 - 博客频道 - CSDN.NET

    在Excel 中编写VBA 代码,最常做的事可能就是操作表单中单元格里的数据. 我这里总结一下如何从VBA 代码中操作单元格的数据. 在VBA 代码中操作单元格需要用到Range 对象,Range 是 ...

  7. [Xcode 实际操作]五、使用表格-(3)设置UITableView单元格图标

    目录:[Swift]Xcode实际操作 本文将演示如何给表格行设置图标. 打开资源文件夹[Assets.xcassets], 在资源文件夹中导入两张图片:一张彩色,一张灰色,作为单元格的图标. [+] ...

  8. c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index ...

  9. [转]Handsontable对单元格的操作

    原文地址:http://blog.csdn.net/mafan121/article/details/46119905 1.自动填充单元格数据 fillHandle:true/false    //当 ...

随机推荐

  1. mongodb- 备份和导入备份

    一.使用 mongodump 命令备份数据 mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表 -o 文件存放路径 参数说明: -h 指明数据库宿主机 ...

  2. H7-TOOL脱机烧录器功能开源发布

    H7-TOOL汇总帖:https://www.cnblogs.com/armfly/p/12283459.html 当前已经对STM32F030,STM32F103,STM32F429,STM32F7 ...

  3. Redis详解(四)——删除策略

    Redis详解(四)--删除策略 Redis中的数据特征 Redis是一种内存级数据库,所有数据均存放在内存中,内存中的数据可以通过TTL指令来获取其状态,当 key 不存在时,返回 -2 . 当 k ...

  4. Spring配置数据源以及hibernate

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. use matplotlib to draw scatter plot

    There are many pionts in this kind of table. How to do it? We can use scatter() to draw it. Code: im ...

  6. golang调用CertUtil获取文件md5

    func getFileMD5(fileName string) string { cmd := exec.Command("cmd", "/C", " ...

  7. centos6.7开启linux虚拟机内部错误

    如图 这个需要用管理员身份运行就好了 右键----->以管理员身份打开,正常启动

  8. Python的一些常用知识

    1.How to force urllib2 not to use a proxy Here is an example to remove proxy settings for all reques ...

  9. JavaScript.descriptor(属性描述符)

    属性描述符是对JavaScript属性的描述,包括:value.writable.enumerable.configurable,除value其他默认为true. 本文包括: 取得属性描述符. Obj ...

  10. javascript中window.open()与window.location.href

    1.window.location是window对象的属性,而window.open是window对象的方法    window.location是你对当前浏览器窗口的URL地址对象的参考!      ...