VBA单元格自适应高亮操作
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单元格自适应高亮操作的更多相关文章
- 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 ...
- 使用js方法将table表格中指定列指定行中相同内容的单元格进行合并操作。
前言 使用js方法对html中的table表格进行单元格的行列合并操作. 网上执行此操作的实例方法有很多,但根据实际业务的区别,大多不适用. 所以在网上各位大神写的方法的基础上进行了部分修改以适合自己 ...
- 基于.NET的Excel开发:单元格区域的操作(读取、赋值、边框和格式)
引用 using Excel = Microsoft.Office.Interop.Excel; 定义 Excel.ApplicationClass app; Excel.Workbooks book ...
- Excel单元格的日常操作
通过右键选择插入来移动单元格 灵活的运用"整行" 与 "整列" 选中区域之后 通过点击区域边框进行移动 按住shift之后框会变成线 更容易拖动 按住ctrl拖 ...
- vba单元格背景色
cells(i,j).interior.color=5287936 green =255 red
- Excel 2003 中如何用VBA 代码访问单元格里的值及操作单元格 - 唐诗宋词的专栏 - 博客频道 - CSDN.NET
在Excel 中编写VBA 代码,最常做的事可能就是操作表单中单元格里的数据. 我这里总结一下如何从VBA 代码中操作单元格的数据. 在VBA 代码中操作单元格需要用到Range 对象,Range 是 ...
- [Xcode 实际操作]五、使用表格-(3)设置UITableView单元格图标
目录:[Swift]Xcode实际操作 本文将演示如何给表格行设置图标. 打开资源文件夹[Assets.xcassets], 在资源文件夹中导入两张图片:一张彩色,一张灰色,作为单元格的图标. [+] ...
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index ...
- [转]Handsontable对单元格的操作
原文地址:http://blog.csdn.net/mafan121/article/details/46119905 1.自动填充单元格数据 fillHandle:true/false //当 ...
随机推荐
- webpack 命令 Module build failed (from ./node_modules/babel-loader/lib/index.js) 错误问题解决方案
在项目中运行的时候出现报错,错误为Module build failed (from ./node_modules/babel-loader/lib/index.js) 解决方案: 控制台输入 np ...
- Swift Access Levels (变量关键字访问权限)
- git 创建分支并提交代码
1.查看所有分支 git branch -a 2.查看当前分支 git branch 3.新建一个分支 git branch feature-xx 4.切换到新建分支上面 git checkout f ...
- Linux-initramfs
1. 内核启动问题2. 解决方案2.1 ramdisk(比如initrd)2.2 tmpfs(比如initramfs)2.3 ramdisk vs ramfs2.4 临时文件系统2.4.1 观察tmp ...
- php截取指定两个字符之间的字符串
//截取指定两个字符之间的字符串 public function cut($begin,$end,$str){ $b = mb_strpos($str,$begin) + mb_strlen($beg ...
- linux 下office软件推荐
概述 最近想使用LINUX下搭建服务器,所以查找一些需要用的软件. linux下最好的office解决办法 其实因为我是不怎么使用office的,我也不知道不同office有什么不一样,直到有一次写奖 ...
- Apache http 包中的常量
org.apache.* org.apache.http.Consts public static final int CR 13 public static final int HT 9 publi ...
- PHP: isset与empty的区别
PHP的isset()函数 一般用来检测变量是否设置 功能:检测变量是否设置 返回值: 若变量不存在则返回 FALSE 若变量存在且其值为NULL,也返回 FALSE 若变量存在且值不为NULL,则返 ...
- mysql 存储引擎入门
- ListView的DrawSubItem时间添加边框,字体变粗问题
procedure TFrmrdp.ListView1AdvancedCustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubIt ...