Sub 选择A列最后一个非空单元格()
Range("a1048576").End(xlUp).Select
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Cells(Target.Row + 1, 16384).End(xlToLeft).Offset(0, 1).Select
End Sub
Sub 选择当前列最大值()
Dim rng As Range, rng2 As Range '声明变量'
Set rng2 = Application.Intersect(ActiveCell.EntireColumn, ActiveCell.CurrentRegion) '将本列已用区域赋值给rng2'
For Each rng In rng2 '开始循环检测单元格值'
If rng.Value = WorksheetFunction.Max(rng2) Then '如果等于最大值'
rng.Select
Exit For '退出循环'
End If
Next
End Sub
数据类型 | 类型声明字符 |
---|---|
integer | % |
long | & |
single | ! |
double | # |
currency | @ |
string | $ |
自动选择最大值
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range, rng2 As Range '声明变量'
Set rng2 = Application.Intersect(ActiveCell.EntireColumn, ActiveCell.CurrentRegion) '将本列已用区域赋值给rng2'
For Each rng In rng2 '开始循环检测单元格值'
If rng.Value = WorksheetFunction.Max(rng2) Then '如果等于最大值'
rng.Select
Exit For '退出循环'
End If
Next
End Sub
Sub 选择负数单元格()
Dim rg As Range, rng As Range
For Each rng In Range("A1:B7")
If rng < 0 Then
If rg Is Nothing Then
Set rg = rng
Else
Set rg = Application.Union(rg, rng)
End If
End If
Next
rg.Select
End Sub
Sub 选择已用区域()
ActiveCell.CurrentRegion.Select
MsgBox "已选择单元格所在区域。", 64, "提示"
ActiveSheet.UsedRange.Select
MsgBox "已选择本工作表所在区域。", 52, "提示"
End Sub
Sub 选择数组公式区域()
ActiveCell.CurrentArray.Select
End Sub
Sub 单元格合集与交集()
Application.union(range("A1:F8"),Range("H1:J8")).Select
MsgBox "单元格合集",64,"提示"
Application.Intersect([A1:F8],columns("E:E")).Select
MsgBox "单元格交集",64,"提示"
end Sub
Sub 选择黄色的单元格()
Dim rng as range,rg as range
for each rng in activesheet.UsedRange
if rng.interior.color = 65535 then
if rg is Nothing then
set rg = rng
else
set rg = application.union(rg,rng)
end if
end if
next
rg.select
end Sub
sub 单元格颜色()
[a5].interior.color = RGB(255,0,0)
end sub
Sub 选择蓝色字体的单元格()
Dim rng As Range, rg As Range
For Each rng In ActiveSheet.UsedRange
If rng.Font.ColorIndex = 5 Then
If rg Is Nothing Then
Set rg = rng
Else
Set rg = Application.Union(rg, rng)
End If
End If
Next
rg.Select
End Sub
Sub 颜色()
Dim i As Byte
For i = 1 To 56
Cells(i, 1) = "colorindex" & i
Cells(i, 2).Interior.ColorIndex = i
Next
End Sub
Sub 选择粗线边框的单元格()
On Error Resume Next
Dim rng As Range, rg As Range
For Each rng In ActiveSheet.UsedRange
If rng.Borders(xlEdgeRight).Weight = xlMedium And rng.Borders(xlEdgeTop).Weight = xlMedium _
And rng.Borders(xlEdgeLeft).Weight = xlMedium And rng.Borders(xlEdgeBottom).Weight = xlMedium Then
If rg Is Nothing Then
Set rg = rng
Else
Set rg = Application.Union(rg, rng)
End If
End If
Next
rg.Select
End Sub
名称 | 值 | 描述 |
---|---|---|
xlhairline | 1 | 细线(最细边框) |
xlmediumline | -4138 | 中等 |
xlthick | 4 | 粗线(最宽边框) |
xlthin | 2 | 细 |
Sub 反向选择工作表()
Application.DisplayAlerts = False '禁用警告提示'
Application.ScreenUpdating = False '禁止屏幕更新'
Dim raddress As String, taddress As String '声明变量'
raddress = Selection.Address
taddress = ActiveSheet.UsedRange.Address
With Sheets.Add '添加一个新工作表'
.Range(taddress) = 0 '对新表赋值'
.Range(raddress) = "=0" '对新表赋值'
'raddress = .Range(taddress).SpecialCells(xlCellTypeConstants, 1).Address 重新设置address为含常量的单元格地址'
.Delete
End With
ActiveSheet.Range(raddress).Select '反向区域选择'
Application.ScreenUpdating = True '开启屏幕更新'
End Sub
Type常量 | 值 | 说明 |
---|---|---|
xlCellTypeAllFormatConditions | -4172 | 任意格式单元格 |
xlCellTypeAllValidation | -4174 | 含有验证条件的单元格 |
xlCellTypeBlanks | 4 | 空单元格 |
xlCellTypeComments | -4144 | 含有注释的单元格 |
xlCellTypeConstants | 2 | 含有常量的单元格 |
xlCellTypeFormulas | -4123 | 含有公式的单元格 |
xlCellTypeLastCell | 11 | 已用区域中的最后一个单元格 |
xlCellTypeSameFormatConditions | -4173 | 含有相同格式的单元格 |
xlCellTypeSameValidation | -4175 | 含有相同验证条件的单元格 |
xlCellTypeVisible | 12 | 所有可见单元格 |
Value常量 | 值 | 说明 |
---|---|---|
xlErrors | 16 | 错误值 |
xlLogical | 4 | 逻辑值 |
xlNumbers | 1 | 数字 |
xlTextValues | 2 | 文本 |
sub 排除标题行()
Application.Intersect(activesheet.UsedRange,activesheet.UsedRange.offset(1,0)).select
end sub
Sub 每隔三行选一行()
Dim rng As Range, i As Long
Application.ScreenUpdating = False '禁止屏幕更新'
i = ActiveSheet.UsedRange.Rows.Count '计算已用行数'
With Range("XFD1:XFD" & i) '在最末列输入公式作为辅助列'
.Formula = "=if(mod(row(),3),1,0/0)" '行号除以3余数为1,2时显示1,否则显示一个0/0的错误值'
Set rng = .SpecialCells(xlCellTypeFormulas, 16).EntireRow '参数16表示错误值'
rng.Select '选择目标行'
.Value = "" '清空输入区数据'
End With
Application.ScreenUpdating = True '开启屏幕更新'
End Sub
Sub 选择奇数列()
Dim rng As Range, rang As Range, i As Long
Application.ScreenUpdating = False '禁止屏幕更新'
i = ActiveSheet.UsedRange.Columns.Count '计算已用列数'
Set rang = ActiveSheet.UsedRange
With Range(Range("A1048576"), Cells(1048576, i)) '在最末行输入公式作为辅助列'
.Formula = "=if(mod(column(),2),0/0,1)" '列号除以2余数为1时显示一个0/0的错误值,否则显示1'
Set rng = .SpecialCells(xlCellTypeFormulas, 16).EntireColumn '参数16表示错误值'
Application.Intersect(rng, rang, rang.Offset(1, 0)).Select '选择目标列与已用区域的交集'
.Value = "" '清空输入区数据'
End With
Application.ScreenUpdating = True '开启屏幕更新'
End Sub
Sub 选择三个表的B2到B11区域()
Sheets(Array("A组", "B组", "C组")).Select '组合工作表'
Sheets("A组").Activate '激活第一个表'
Range("B2:B" & [b1048576].End(xlUp).Row).Select '选择目标区域'
End Sub
Sub 选择本表以外所有工作表的B2到B11区域()
Dim sh As Worksheet, n%, i%, arr
n = ThisWorkbook.Sheets.Count '取得工作表总数'
ReDim arr(1 To n) '声明变量'
For Each sh In ThisWorkbook.Sheets '循环检查工作表表名'
If sh.Name <> ActiveSheet.Name Then
i = i + 1
arr(i) = sh.Name
End If
Next
If i > 0 Then
ReDim Preserve arr(1 To i)
ThisWorkbook.Sheets(arr).Select '组合工作表'
ActiveSheet.[B2:B11].Select '选择区域'
End If
End Sub
Sub 多表选择()
Dim wks As Worksheet, shtCnt As Integer
Dim arr() As Variant, i As Integer
shtCnt = ThisWorkbook.Sheets.Count '取得工作表总数'
ReDim arr(1 To shtCnt) '声明变量'
For Each wks In ThisWorkbook.Sheets '在所有工作表中循环'
If wks.Name Like "星期*" Then
i = i + 1
arr(i) = wks.Name
End If
Next
If i > 0 Then
ReDim Preserve arr(1 To i)
ThisWorkbook.Sheets(arr).Select '组合工作表'
ActiveSheet.UsedRange.Select '选择区域'
End If
End Sub
Sub 提取选区地址并计数()
MsgBox "你选择了" & Selection.Address & Chr(10) & _
"共有" & Selection.Count & "个"
End Sub
Sub 提取选区地址并计数()
MsgBox "你选择了" & Selection.Address(RowAbsolute:=False,ColumnAbsolute:=False,ReferenceStyle:=xlR1C1) & Chr(10) & _
"共有" & Selection.Count & "个"
End Sub
名称 | 数据类型 | 描述 |
---|---|---|
RowAbsolute | Variant | 如果为True,则以绝对引用返回引用的行部分。默认值为True |
ColumnAbsolute | Variant | 如果为True, 则以绝对引用返回引用的列部分。默认值为True |
ReferenceStyle | XIReferenceStyle | 引用样式。默认值为xIAI |
External | Variant | 如果为True,则返回外部引用:如果为False, 则返回本地引用。默认值为False |
如果RowAbsolute 和ColumnAbsolute 为False, 并且ReferenceStyle | ||
RelativeTo | Variant | 为xIR1Cl, 则必须包括相对引用的起始点。此参数是定义起始点的Range对象 |
Sub 判断选区隐藏的单元格个数()
Dim cell as range,i%
for each cell in selection
if cell.EntireRow.Hidden Or cell.EntireColumn.Hidden then
i = i+1
end if
next
MsgBox "隐藏单元格的个数为" & i
End Sub
Sub 判断单元格的隐藏状态()
Dim cell As Range, temp As String
On Error GoTo err
For Each cell In Selection
If cell.EntireRow.Hidden Or cell.EntireColumn.Hidden Then
temp = temp & cell.Address & "、"
End If
Next
temp = Left(temp, Len(temp)-1)
MsgBox "以下单元格处于隐藏状态" & Chr(10) & temp: Exit Sub
err:
MsgBox "没有处于隐藏状态的单元格"
End Sub
讲解
(1)GoTo:无条件地转移到过程中指定的行,用于程序代码的转移。
(2)On Error GoTo err:Error是程序错误,整句即表示当程序有错误时则运行标签“err”之后的代码。
补充
一开始不太理解temp=left(temp,len(temp)-1)这个代码的意思
应该是将temp删除最后一个"、“号后的语句,如果没有这句代码就会多个”、"
Sub 空白单元格个数()
MsgBox "空白单元格个数" & activesheet.UsedRange.SpecialCells(xlCellTypeBlanks).count
End Sub
Sub 空白单元格个数()
MsgBox "空白单元格个数" & Selection.SpecialCells(xlCellTypeBlanks).count
End Sub
Sub 统计公式个数()
MsgBox "统计公式个数" & activesheet.UsedRange.SpecialCells(xlCellTypeFormulas).count
End Sub
Sub 统计公式个数()
MsgBox "统计公式个数" & activesheet.UsedRange.SpecialCells(-4123).count
End Sub
Sub 统计公式个数()
activesheet.UsedRange.SpecialCells(-4123).select
MsgBox "统计公式个数" & Selection.count
End Sub
Sub 计算已用行列数()
dim rng as range,r%,c%
set rng = activesheet.UsedRange
r = rng.Rows.count
c = rng.columns.count
MsgBox "已用行数" & r & "已用列数" & c,64,"提示"
End Sub
Sub 计算已用行列数()
MsgBox "已用行数" & activesheet.UsedRange.Rows.count & "已用列数" & activesheet.UsedRange.columns.count
End Sub
Sub 带批注之单元格个数()
Dim i As Integer, cell As Range
On Error GoTo err
For Each cell In Selection
If Not Intersect(cell, Cells.SpecialCells(xlCellTypeComments)) Is Nothing Then
i = i + 1
End If
Next
MsgBox "带批注之单元格个数" & i, 64, "提示"
End
err:
MsgBox "工作表中没有批注", 64
End Sub
Sub 常规格式的单元格个数()
dim i as integer,cell as range
for each cell in selection
if cell.numberformatlocal="G/通用格式" then
i = i + 1
end if
next
MsgBox "常规格式的单元格个数" & i
end Sub
Sub 统计选区中文本与字母数字个数()
Dim te as Long,eng as Long,num as Long
Dim i as Long,j as Long,cell as range,str as string
if typename(selection) <> "Range" then MsgBox "请选择单元格!",64,"友情提示":exit sub
for each cell in selection
j = j+len(cell.value) '计算字符总长度'
for i = 1 to len(cell)
str = mid(cell.value,i,1)
if str like "[一-龥]"=true then
te = te + 1
elseif str like "[a-zA-Z]"=true then
eng = eng + 1
elseif str like "[0-9]"=true then
num = num + 1
end if
next
next
MsgBox "所选单元格区域中共有字数" & j & Chr(10) & "汉字" & te _
& "字母" & eng & "数字" & num
end Sub
Sub 统计选区中负数个数()
Dim rng as range,i%
for each rng in selection
if rng.value < 0 then
i = i + 1
end if
next
MsgBox "选区中负数个数" & i,64,"提示"
End Sub
Sub 将选区公式转换成数值()
Dim rng As Range
Set rng = Application.InputBox("请选择公式单元格区域", "转换为数值", "a1", Type:=8) '选择区域'
If rng Is Nothing Then Exit Sub '若点击取消则退出程序'
rng = rng.Value '公式转为值'
End Sub
值 | 含义 |
---|---|
0 | 公式 |
1 | 数字 |
2 | 文本(字符串) |
4 | 逻辑值(True或False) |
8 | 单元格引用,作为一个range对象 |
16 | 错误值,如N/A |
64 | 数值数组 |
Sub 将选区公式转换成数值()
Dim rng As Range
Set rng = Application.InputBox("请选择公式单元格区域", "转换为数值", "a1", Type:=8) '选择区域'
If rng Is Nothing Then GoTo endd '若点击取消则退出程序'
rng = rng.Value '公式转为值'
endd:
End Sub
Sub 将当前区域公式转换成数值()
Dim rng As Range
Set rng = ActiveSheet.UsedRange '选择区域'
If rng Is Nothing Then Exit Sub '若点击取消则退出程序'
rng = rng.Value '公式转为值'
End Sub
Sub 将当前区域公式转换成数值()
ActiveCell.CurrentRegion.Copy '复制'
ActiveCell.CurrentRegion.PasteSpecial Paste:=xlPasteValues '粘贴数值'
End Sub
值 | 描述 |
---|---|
xIPasteAll | 全部 |
xlPasteAllExceptBorders | 边框除外 |
xlPasteColumnWidths | 列宽 |
xIPasteComments | 批注 |
xIPasteFormats | 格式 |
xIPasteFormulas | 公式 |
xIPasteFormulasAndNumberFormats | 公式和数字格式 |
xlPasteValidation | 有效性验证 |
xIPasteValues | 值 |
xlPasteValuesAndNumberFormats | 值和数字格式 |
Sub 将数字转换为文本()
Selection.NumberFormatLocal = "@"
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If IsEmpty(Target) Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If Target.Column <> 2 Then Exit Sub
Application.EnableEvents = False
Target.Value = UCase(Target.Value)
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Target = WorksheetFunction.Proper(Target.Text)
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Target = StrConv(Target, vbProperCase)
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Column = 1 And Target.Count = 1 Then
Target = Application.WorksheetFunction.Text(Target.Text, "yyyy""年""m""月""d""日""[$-804]aaaa")
End If
End Sub
Sub 将零值替换为空()
Selection.Replace what:="0", replacement:="", lookat:=xlWhole
End Sub
Sub 将区域数据改成以万为单位()
Selection.NumberFormatLocal="#"".""#,万"
End Sub
Sub 标示上标()
Dim FindStr As String, FirstAddress As String, tran As Range, i, j, k, l
FindStr = "#" '标上上标的字符串'
With Selection
Set tran = .Find(FindStr, LookIn:=xlValues, lookat:=xlPart) '设定查找值'
If Not tran Is Nothing Then '如果找到'
FirstAddress = tran.Address '记录地址'
Do
i = (Len(tran.Value) - Len(WorksheetFunction.Substitute(tran.Value, FindStr, ""))) / Len(FindStr)
k = 1
For j = 1 To i
l = WorksheetFunction.Find(FindStr, tran.Value, k)
tran.Characters(Start:=l, Length:=Len(FindStr)).Font.Superscript = True '标示上标'
k = l + Len(FindStr)
Next '查找下一个'
Set tran = .FindNext(tran)
Loop While Not tran Is Nothing And tran.Address <> FirstAddress '直到返回第一个地址'
End If
End With
End Sub
Do [{while|until} condition]
[statements]
[Exit Do]
[statements]
loop
或
Do
[statements]
[Exit Do]
[statements]
loop [{while|until} condition]
Sub 修改任意字符为上标()
Dim r As Range, i%, First$, inputt
inputt = InputBox("上表对象", "请输入加上标的对象", "#")
Application.ScreenUpdating = False
Set r = Cells.Find(inputt, lookat:=xlPart) 'xlpart表示单元格不同完全匹配'
If Not r Is Nothing Then '当找到时'
First = r.Address '用First记录下第一个单元格的地址'
Do
For i = 1 To Len(r) '对找到的单元格,从第一个字符到最后一个字符'
If Mid(r, i, 1) = inputt Then '假如是inputt指定字符时,则设置它为上标'
r.Characters(Start:=i, Length:=1).Font.Superscript = True
End If
Next
Set r = Cells.FindNext(r) '在找到的单元格之后,查找新一个单元格'
Loop Until r.Address = First '重复过程,直到最后找到的单元格的地址等于第一个单元格的地址'
End If
Application.ScreenUpdating = True
End Sub
Sub 任意字符添加下划线()
Dim r As Range, i%, First$, inputt
inputt = InputBox("下划线对象", "请输入加下划线的对象", "#")
Application.ScreenUpdating = False
Set r = Cells.Find(inputt, lookat:=xlPart) 'xlpart表示单元格不同完全匹配'
If Not r Is Nothing Then '当找到时'
First = r.Address '用First记录下第一个单元格的地址'
Do
For i = 1 To Len(r) '对找到的单元格,从第一个字符到最后一个字符'
If Mid(r, i, 1) = inputt Then '假如是inputt指定字符时,则加下划线'
r.Characters(Start:=i, Length:=1).Font.Underline = xlUnderlineStyleSingle
End If
Next
Set r = Cells.FindNext(r) '在找到的单元格之后,查找新一个单元格'
Loop Until r.Address = First '重复过程,直到最后找到的单元格的地址等于第一个单元格的地址'
End If
Application.ScreenUpdating = True
End Sub
Sub 任意字符添加下划线()
Dim r As Range, i%, First$, inputt,indexx
inputt = InputBox("下划线对象", "请输入加下划线的对象", "#")
set indexx = Application.InputBox("请输入欲加下划线的单元格区域,也可以用鼠标选择","定位","A1",,,,,8)
Application.ScreenUpdating = False
Set r = indexx.Find(inputt, lookat:=xlPart) 'xlpart表示单元格不同完全匹配'
If Not r Is Nothing Then '当找到时'
First = r.Address '用First记录下第一个单元格的地址'
Do
For i = 1 To Len(r) '对找到的单元格,从第一个字符到最后一个字符'
If Mid(r, i, 1) = inputt Then '假如是inputt指定字符时,则加下划线'
r.Characters(Start:=i, Length:=1).Font.Underline = xlUnderlineStyleSingle
End If
Next
Set r = indexx.FindNext(r) '在找到的单元格之后,查找新一个单元格'
Loop Until r.Address = First '重复过程,直到最后找到的单元格的地址等于第一个单元格的地址'
End If
Application.ScreenUpdating = True
End Sub
Sub 在任意字符上方添加着重符()
Dim r As Range, i%, First$, inputt
inputt = InputBox("输入欲加着重符的字符", "定位", "#", 50, 50)
Application.ScreenUpdating = False
Set r = Selection.Find(inputt, lookat:=xlPart) 'xlpart表示单元格不同完全匹配'
If Not r Is Nothing Then '当找到时'
First = r.Address '用First记录下第一个单元格的地址'
Do
For i = 1 To Len(r) '对找到的单元格,从第一个字符到最后一个字符'
If Mid(r, i, 1) = inputt Then '假如是inputt指定字符时,则加上着重符'
r.Phonetics.Visible = True
r.Characters(Start:=i, Length:=1).PhoneticCharacters = "."
r.Phonetics.Font.Size = r.Font.Size + 2
r.Phonetics.Font.Name = "黑体"
Else
r.Characters(Start:=i, Length:=1).PhoneticCharacters = ""
r.Phonetics.Alignment = xlPhoneticAlignCenter
End If
Next
Set r = Selection.FindNext(r) '在找到的单元格之后,查找新一个单元格'
Loop Until r.Address = First '重复过程,直到最后找到的单元格的地址等于第一个单元格的地址'
End If
Application.ScreenUpdating = True
End Sub
Sub 数据重排()
Dim ans As Byte, anss As Byte, a(1 To 50) As Integer, i As Byte
ans = InputBox("请选择:1升序,2降序", "选项", 1, 10, 10)
anss = InputBox("请选择新数据产生在那一列" & Chr(10) & "只能输入数字", "选项", 2, 10, 10)
For i = 1 To 50
a(i) = ActiveSheet.Cells(i + 1, 1)
Next
Dim b(1 To 50)
For i = 1 To 50
If ans = 1 Then b(i) = Application.WorksheetFunction.Small(a, i)
If ans = 2 Then b(i) = Application.WorksheetFunction.Large(a, i)
Next
For i = 1 To 50
ActiveSheet.Cells(i + 1, anss) = b(i)
Next
ActiveSheet.Cells(1, anss) = "重排序"
End Sub