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. iOS 十种线程锁

    锁 是什么意思? 我们在使用多线程的时候多个线程可能会访问同一块资源,这样就很容易引发数据错乱和数据安全等问题,这时候就需要我们保证每次只有一个线程访问这一块资源,锁 应运而生. 这里顺便提一下,上锁 ...

  2. shell-Startup-Files

    shell-Startup-Files 1. 相关阅读 2. 主流shell 3. shell实例类型 4. Shell启动文件的必要元素 4.1 路径: 命令路径, 4.2 提示符 5. 主流she ...

  3. Linux-Journal

    Linux-Journal 1. 日志简介 2. 日志的优先级和分类 2.1 优先级 2.2 设施分类 3. 命令帮助 4. 日志查看示例 5. 日志大小限制 6. 手动清理日志文件 1. 日志简介 ...

  4. linux 下office软件推荐

    概述 最近想使用LINUX下搭建服务器,所以查找一些需要用的软件. linux下最好的office解决办法 其实因为我是不怎么使用office的,我也不知道不同office有什么不一样,直到有一次写奖 ...

  5. 009、MySQL取当前时间Unix时间戳,取今天Unix时间戳

    #取Unix时间戳 SELECT unix_timestamp( ) ; #取今天时间戳 SELECT unix_timestamp( curdate( ) ); 显示如下: 不忘初心,如果您认为这篇 ...

  6. NAND Flash驱动

    硬件原理及分析 管脚说明         Pin Name Pin Function R/B(RnB) The R/B output indicates the status of the devic ...

  7. python-处理json、处理时间

    1.处理json:import json #json串就是字符串. d = { 'car1':{'color':'red','price':100,'count':50}, 'car2':{'colo ...

  8. springboot - 在servlet中映射Errors 脱离spring mvc

    应用不用Spring MVC, 采用ErrorPageRegistrar 接口能直接映射errors. 1.概览 2.java代码 1).MyAppServlet package com.ebc.se ...

  9. POJ 1061:青蛙的约会

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95878   Accepted: 17878 Descripti ...

  10. JavaScript原生Ajax请求纯文本数据

    源代码 ajax1.html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...