使用macros在excel文件的所有表格中searchstring
我写了一个macros,它将在excel文件的所有表格中search一个string。 这个macros将激活第一个表格以及包含searchstring的表格中的单元格。 如果没有find,它会显示一条消息。 这个macros运行良好。 我想扩展这个function来覆盖包含这个string而不是第一个的所有表单。 所以我修改了这个macros,但没有按预期工作。 我已经给出了下面的代码,并在它显示错误的地方发表评论。
Dim sheetCount As Integer Dim datatoFind Sub Button1_Click() Find_Data 结束小组 Private Sub Find_Data() Dim counter As Integer Dim currentSheet As Integer Dim notFound As Boolean Dim yesNo As String notFound = True 在错误恢复下一步 currentSheet = ActiveSheet.Index datatoFind = InputBox(“请input要search的值”) 如果datatoFind =“”然后退出子 sheetCount = ActiveWorkbook.Sheets.Count 如果IsError(CDbl(datatoFind))= False那么datatoFind = CDbl(datatoFind) 对于计数器= 1到sheetCount 表(计数器).Activate Cells.Find(What:= datatoFind,After:= ActiveCell,LookIn:= xlFormulas,LookAt _ := xlPart,SearchOrder:= xlByRows,SearchDirection:= xlNext,MatchCase:= _ False,SearchFormat:= False).Activate 如果InStr(1,ActiveCell.Value,datatoFind)那么 如果HasMoreValues(counter + 1)则'未完成方法并直接进入 yesNo = MsgBox(“你想继续search?”,vbYesNo) 如果yesNo = vbNo那么 notFound = False 退出 万一 万一 表(计数器).Activate 万一 下一个计数器 如果没有find然后 MsgBox(“找不到值”) 表(currentSheet).Activate 万一 结束小组 私有函数HasMoreValues(ByVal sheetCounter As Integer)作为布尔值 HasMoreValues = False Dim str As String 对于counter = sheetCounter为sheetCount 表(计数器).Activate str = Cells.Find(What:= datatoFind,After:= ActiveCell,LookIn:= xlFormulas,LookAt _ := xlPart,SearchOrder:= xlByRows,SearchDirection:= xlNext,MatchCase:= _ False,SearchFormat:= False).Value'不会超过这个值,即下面的代码不会被执行 如果InStr(1,str,datatoFind)那么 HasMoreValues = True 退出 万一 下一个计数器 结束function
我能解决我的问题,并发布了可能需要它的代码
Dim sheetCount As Integer Dim datatoFind Sub Button1_Click() Find_Data 结束小组 Private Sub Find_Data() Dim counter As Integer Dim currentSheet As Integer Dim notFound As Boolean Dim yesNo As String notFound = True 在错误恢复下一步 currentSheet = ActiveSheet.Index datatoFind = StrConv(InputBox(“请input要search的值”),vbLowerCase) 如果datatoFind =“”然后退出子 sheetCount = ActiveWorkbook.Sheets.Count 如果IsError(CDbl(datatoFind))= False那么datatoFind = CDbl(datatoFind) 对于计数器= 1到sheetCount 表(计数器).Activate Cells.Find(What:= datatoFind,After:= ActiveCell,LookIn:= xlFormulas,LookAt _ := xlPart,SearchOrder:= xlByRows,SearchDirection:= xlNext,MatchCase:= _ False,SearchFormat:= False).Activate 如果InStr(1,StrConv(ActiveCell.Value,vbLowerCase),datatoFind)则 notFound = False 如果HasMoreValues(counter)那么 yesNo = MsgBox(“你想继续search?”,vbYesNo) 如果yesNo = vbNo那么 表(计数器).Activate 退出 万一 其他 表(计数器).Activate 退出 万一 表(计数器).Activate 万一 下一个计数器 如果没有find然后 MsgBox(“找不到值”) 表(currentSheet).Activate 万一 结束小组 私有函数HasMoreValues(ByVal sheetCounter As Integer)作为布尔值 HasMoreValues = False Dim str As String Dim lastRow As Long Dim lastCol As Long Dim rRng As Excel.Range 对于计数器= sheetCounter + 1到sheetCount 表(计数器).Activate lastRow = ActiveCell.SpecialCells(xlLastCell).Row lastCol = ActiveCell.SpecialCells(xlLastCell).Column 对于vRow = 1到lastRow 对于vCol = 1 To lastCol str = Sheets(counter).Cells(vRow,vCol).Text 如果InStr(1,StrConv(str,vbLowerCase),datatoFind)那么 HasMoreValues = True 退出 万一 下一个vCol 如果HasMoreValues那么 退出 万一 下一个vRow 如果HasMoreValues那么 表(sheetCounter).Activate 退出 万一 下一个计数器 结束function
问候,
萨马
问题是Cells.Find
返回一个范围。 当你在你的函数HasMoreValues
使用它时,你可以像这样使用它:
Cells.Find(...).Value
但返回的范围不会正确转换为.value。 你可以通过使用.text
而不是.value
来解决这个问题,就像这样:
Cells.Find(...).text
或完全:
str = Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).text
为了完全正确,您应该将find的结果set
为一个Rangevariables,然后通过该variables进行访问,以防Findsearch返回任何内容。 然而,根据文档Cells.Find
总是返回一个单元格的范围,所以你可能会没事的。