最里面括号为黑色,第二层红色,第三层蓝色,第四层绿色,第五层又循环为黑色、红色、蓝色、绿色……单元格类似内容为:((((1+1)*2)+1)*2)*2、((1+1)*(1+1)+(1+1))*2、(1+1)*(1+1)*((1+1)*2)、总之就是括号比较多和复杂的计算式:而且如果计算式括号"("、与")"不匹配和先出现")"再出现“("这样的错误出现时跳过此单元格的染色。谢谢!
知道这个比较麻烦,自己弄了几天也弄不好,所以特此请教各位高手,万分感谢!!!!!
excel vba如何设置一个按钮,点击之后A1:A10里的四则运算式中的括号数自动染色?
答案:1 悬赏:80
解决时间 2021-03-14 06:43
- 提问者网友:全員惡人
- 2021-03-14 00:23
最佳答案
- 二级知识专家网友:野心和家
- 2021-03-14 01:41
自己粗略的测试了一下,试试能用不
Sub 括号颜色()
co = Array(7, 3, 5, 4) '第1,2,3,4层的颜色序号紫红蓝绿。第1层没用黑色,字本来就黑色分不出来。
For i = 1 To [a65536].End(xlUp).Row 'A列循环
tempstring = Cells(i, 1).Value '获取内容
If Len(Replace(tempstring, "(", "")) <> Len(Replace(tempstring, ")", "")) Then GoTo l3 '括号数不匹配跳过
l = Len(tempstring) '内容长度
ReDim t(1 To l) As Integer '存放括号对层数的数组
l1::
aa = InStr(1, tempstring, ")"): If aa = 0 Then GoTo l2 '查找)的位置,没找到表示已查找完
bb = InStrRev(tempstring, "(", aa): If bb = 0 Then GoTo l3 '在)的位置往左找(,没找到表示不配对,跳过
For j = bb + 1 To aa - 1 '计算找到的括号在哪一层
If Max < t(j) Then Max = t(j)
Next
tempstring = Application.WorksheetFunction.Replace(tempstring, aa, 1, 0): t(aa) = Max + 1 '找到的替换掉方便下次查找并保存括号的层次
tempstring = Application.WorksheetFunction.Replace(tempstring, bb, 1, 0): t(bb) = Max + 1
Max = 0
GoTo l1
l2::
For j = 1 To l '标记颜色
If t(j) > 0 Then
Cells(i, 1).Characters(Start:=j, Length:=1).Font.ColorIndex = co((t(j) - 1) Mod 4)
End If
Next
l3:: Next
End Sub
Sub 括号颜色()
co = Array(7, 3, 5, 4) '第1,2,3,4层的颜色序号紫红蓝绿。第1层没用黑色,字本来就黑色分不出来。
For i = 1 To [a65536].End(xlUp).Row 'A列循环
tempstring = Cells(i, 1).Value '获取内容
If Len(Replace(tempstring, "(", "")) <> Len(Replace(tempstring, ")", "")) Then GoTo l3 '括号数不匹配跳过
l = Len(tempstring) '内容长度
ReDim t(1 To l) As Integer '存放括号对层数的数组
l1::
aa = InStr(1, tempstring, ")"): If aa = 0 Then GoTo l2 '查找)的位置,没找到表示已查找完
bb = InStrRev(tempstring, "(", aa): If bb = 0 Then GoTo l3 '在)的位置往左找(,没找到表示不配对,跳过
For j = bb + 1 To aa - 1 '计算找到的括号在哪一层
If Max < t(j) Then Max = t(j)
Next
tempstring = Application.WorksheetFunction.Replace(tempstring, aa, 1, 0): t(aa) = Max + 1 '找到的替换掉方便下次查找并保存括号的层次
tempstring = Application.WorksheetFunction.Replace(tempstring, bb, 1, 0): t(bb) = Max + 1
Max = 0
GoTo l1
l2::
For j = 1 To l '标记颜色
If t(j) > 0 Then
Cells(i, 1).Characters(Start:=j, Length:=1).Font.ColorIndex = co((t(j) - 1) Mod 4)
End If
Next
l3:: Next
End Sub
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯