Excel VBA搜索功能仅在从其他工作表触发时工作
此函数循环遍历工作簿中的所有文本框,以查找并突出显示包含结果的文本+文本框 问题是:如果我从一个特定的工作表中触发它,那么该工作表中包含结果的所有文本框都不会高亮显示(虽然文本被找到了,所以它工作了一半) 如果我从一个不包含结果文本框的工作表触发它,那么一切都正常Excel VBA搜索功能仅在从其他工作表触发时工作,excel,vba,Excel,Vba,此函数循环遍历工作簿中的所有文本框,以查找并突出显示包含结果的文本+文本框 问题是:如果我从一个特定的工作表中触发它,那么该工作表中包含结果的所有文本框都不会高亮显示(虽然文本被找到了,所以它工作了一半) 如果我从一个不包含结果文本框的工作表触发它,那么一切都正常 Dim shp As Shape Dim Color As String Dim ColorIndexobj As String Dim Sizeobj As Integer Dim sFind
Dim shp As Shape
Dim Color As String
Dim ColorIndexobj As String
Dim Sizeobj As Integer
Dim sFind As String
Dim sFind2 As String
Dim sTemp As String
Dim iPos As Integer
Dim sTemp2 As String
Dim iPos2 As Integer
Dim Response
Dim sourceSheet As Worksheet
Set sourceSheet = ActiveSheet
sFind = InputBox("Search for?")
If Trim(sFind) = "" Then
MsgBox "Nothing entered"
Exit Sub
End If
For Each ws In ActiveWorkbook.Worksheets
ws.Select
For Each shp In ws.Shapes
If shp.Type = msoTextBox Then
sTemp = shp.TextFrame.Characters.Text
If InStr(LCase(sTemp), LCase(sFind)) <> 0 Then
shp.Select
With shp.Line
Color = shp.Line.ForeColor.RGB
Weight = shp.Line.Weight
.ForeColor.RGB = vbRed
.Weight = 5
End With
sFind2 = LCase(sFind)
sTemp2 = LCase(shp.TextFrame.Characters.Text)
iPos2 = InStr(sTemp2, sFind2)
If iPos2 > 0 Then
With shp.TextFrame.Characters(Start:=iPos2, _
Length:=Len(sFind2)).Font
Sizeobj = .Size
.Size = 35
End With
End If
Set sourceSheet = ActiveSheet
Response = MsgBox( _
"Do you want to continue?", _
Buttons:=vbYesNo, Title:="Continue?")
If Response = vbYes Then
With shp.Line
.ForeColor.RGB = Color
.Weight = Weight
End With
With shp.TextFrame.Characters(Start:=iPos2, _
Length:=Len(sFind2)).Font
.Size = Sizeobj
End With
End If
If Response = vbNo Then
With shp.Line
.ForeColor.RGB = Color
.Weight = Weight
End With
With shp.TextFrame.Characters(Start:=iPos2, _
Length:=Len(sFind2)).Font
.Size = Sizeobj
End With
Exit Sub
End If
End If
End If
Next
Next
Call sourceSheet.Activate
End Sub
将shp尺寸设置为形状
像字符串一样暗淡的颜色
Dim ColorIndexobj作为字符串
Dim Sizeobj为整数
将sFind设置为字符串
将sFind2设置为字符串
作为字符串的Dim sTemp
将IPO作为整数进行调整
Dim sTemp2作为字符串
将iPos2设置为整数
微弱反应
将源表设置为工作表
设置sourceSheet=ActiveSheet
sFind=InputBox(“搜索?”)
如果微调(sFind)=“则
MsgBox“未输入任何内容”
出口接头
如果结束
对于ActiveWorkbook.Worksheets中的每个ws
ws.Select
对于ws.形状中的每个shp
如果shp.Type=msoTextBox,则
sTemp=shp.TextFrame.Characters.Text
如果仪表(LCase(sTemp),LCase(sFind))为0,则
选择
带小功率线
颜色=shp.Line.ForeColor.RGB
重量=shp.Line.Weight
.ForeColor.RGB=vbRed
.重量=5
以
sFind2=LCase(sFind)
sTemp2=LCase(shp.TextFrame.Characters.Text)
iPos2=仪表(sTemp2,sFind2)
如果iPos2>0,则
使用shp.TextFrame.Characters(开始:=iPos2_
长度:=Len(sFind2)).Font
Sizeobj=.Size
.尺寸=35
以
如果结束
设置sourceSheet=ActiveSheet
响应=MsgBox(_
“是否要继续?”_
按钮:=vbYesNo,标题:=“继续?”)
如果响应=vbYes,则
带小功率线
.ForeColor.RGB=颜色
.重量=重量
以
使用shp.TextFrame.Characters(开始:=iPos2_
长度:=Len(sFind2)).Font
.Size=Sizeobj
以
如果结束
如果响应=vbNo,则
带小功率线
.ForeColor.RGB=颜色
.重量=重量
以
使用shp.TextFrame.Characters(开始:=iPos2_
长度:=Len(sFind2)).Font
.Size=Sizeobj
以
出口接头
如果结束
如果结束
如果结束
下一个
下一个
调用sourceSheet.Activate
端接头
问题在于,在重新绘制窗口之前,您所做的更改已撤消
这里有一个混乱的(来自):
紧接着
With shp.Line
Color = shp.Line.ForeColor.RGB
Weight = shp.Line.weight
.ForeColor.RGB = vbRed
.weight = 5
End With
请注意:
ActiveWindow.SmallScroll 0
在运行宏期间,导致窗口重新绘制的确切原因并没有明确的文档记录。规则明显不同,因为它们适用于活动工作表,这将解释您正在观察的行为。工作表中没有任何简单的RePaint
方法,因此使用了kludge,因为即使滚动的距离为零,滚动也会触发重新绘制
出于某种原因,使用
而不是这个乱七八糟的代码似乎不起作用。为什么不以一致且可读的方式缩进代码?对不起,您的代码需要格式化,以便可以读取。请这样做,这样我们就可以在不浪费时间缩进的情况下为您提供帮助。您的代码写得相当好,但会更加精巧,更易于阅读,如果您1)在所有模块的顶部使用Option Explicit
来强制自己声明所有变量,2)使用一致的缩进方案,则可以进行调试和修改,这在VBA编辑器中非常容易。抱歉,各位,您完全正确。我不是一个程序员,只是想调整我在网上找到的东西!做了工作:)谢谢,真让我抓狂!