字符串函数
trim函数示例
Sub 高亮清朝()
Dim a
Dim i
i = 2
Do While Cells(i, 1) <> ""
a = Trim(Cells(i, 1)) '去掉左右两端的空格,但并不改变cells本身
If Left(a, 4) = "爱新觉罗" Then '选中前四位为爱新觉罗的标记为红色
With Cells(i, 1).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
i = i + 1
Loop
End Sub
replace示例
Sub 替换()
Dim s
Dim a
s = " 陈治锦 真的 好好 耍哦!"
a = Replace(s, " ", "") 'replace示例
MsgBox a '显示窗口
End Sub
lcase()示例
Option Explicit
Sub 大小写()
Dim s1
Dim s2
Dim a
Dim b
s1 = "abc"
s2 = "ABc"
b = LCase(s2) '统一转为小写
If b = s1 Then
a = "相等"
Else
a = "不相等"
End If
MsgBox a
End Sub
instr 示例
a串在s串出现的位置 instr(s,a)
也可以写 instr(i,s,a)从第i个字符开始找a
Sub instr函数演示()
Dim a
Dim s
Dim b
s = "锦到黑是真的锦到黑"
a = "锦到黑"
b = InStr(s, a) '在s中找a,找得到返回自然数,找不到返回0
'或者写为 b = InStr(3, s, a)
MsgBox b
End Sub
分解地址演示
Option Explicit
Sub 地址分解()
Dim s
Dim i
Dim 路
Dim 号
i = 3
Do While Cells(i, 2) <> ""
s = Cells(i, 2)
路 = InStr(s, "路") '取出路在字符串的位置
号 = InStr(路, s, "号") '取出号在字符串的位置
Cells(i, 3) = Left(s, 路)
Cells(i, 4) = Right(s, Len(s) - 号) '从右往左取,确定号
Cells(i, 5) = Mid(s, 路 + 1, 号 - 路)
i = i + 1
Loop
End Sub
for each 案例
Option Explicit
Sub 汇总示范()
Dim i, j, k, name
Dim r As Worksheet, w As Worksheet
Set r = Worksheets("季度汇总")
For i = 3 To 10 Step 1
name = r.Cells(i, 2) '赋值给name,便于比较
For Each w In Worksheets '扫描每一个工作表
If Right(w.name, 1) = "月" Then '确保只取月度报表,不取季度汇总
j = 3
Do While w.Cells(j, 2) <> ""
If LCase(Trim(w.Cells(j, 2))) = LCase(Trim(name)) Then 'lcase和trim双保险
For k = 3 To 6 Step 1
r.Cells(i, k) = w.Cells(j, k) + r.Cells(i, k) '累加赋值
Next k
End If
j = j + 1
Loop
End If
Next w '不能忘了要next一下
Next i
End Sub
来源:CSDN
作者:高开低走。
链接:https://blog.csdn.net/qq_43568982/article/details/103754190