Excel-在文本框中搜索文本

Excel-在文本框中搜索文本,excel,vba,search,textbox,find,Excel,Vba,Search,Textbox,Find,在excel中创建文本框时,无法使用搜索/查找文本功能 Excel不会搜索文本框中包含的文本 对于像我这样的人来说,这是一个巨大的限制,有500多个文本框分布在多个工作表上 我看到许多人在帖子中建议解决方案,这些解决方案根本不等于或取代原来的excel“查找文本”功能 例如: 我将在这里分享我的工作方法,希望也能帮助他人 这个vba代码的作用是:它将所有形状(包括文本框)导出到一个新的word文档中 在word中,搜索功能在文本框中工作,问题得到解决。 Sub Export() ' THIS

在excel中创建文本框时,无法使用搜索/查找文本功能

Excel不会搜索文本框中包含的文本

对于像我这样的人来说,这是一个巨大的限制,有500多个文本框分布在多个工作表上

我看到许多人在帖子中建议解决方案,这些解决方案根本不等于或取代原来的excel“查找文本”功能

例如:


我将在这里分享我的工作方法,希望也能帮助他人

这个vba代码的作用是:它将所有形状(包括文本框)导出到一个新的word文档中

在word中,搜索功能在文本框中工作,问题得到解决。

Sub Export()
' THIS must be enabled in Excel: Developer > Visual basic > Tools > References > Microsoft word 1x Object library
'Known bug: if the worksheet has only 1 textbox it will mess up the copy to word. You can manually fix it by adding another textbox in that worksheet. It can be empty.
'Ctrl+break -> will stop the process
'If Word crashes -> the clipboard size is too large.
'Reduce the sheet size or split it in 2 sheets.
'The clipboard limitation is an excel wide limitation.
    
Dim StartTime As Double
Dim MinutesElapsed As String
StartTime = Timer

MsgBox " Wait for job completed textbox in excel!" & vbCrLf & "Close any other WORD files!"
Dim WordApp As Word.Application
Dim i As Integer
Dim sourceSheet As Worksheet
Set sourceSheet = ActiveSheet
Application.ScreenUpdating = False
Sheet1.Activate
Set WordApp = CreateObject("Word.Application")
On Error Resume Next
WordApp.Documents.Add
    With WordApp.ActiveDocument.PageSetup
            .PageWidth = InchesToPoints(22)
            .PageHeight = InchesToPoints(22)
    End With

WordApp.ActiveWindow.View.Type = wdWebView

WordApp.Visible = True
WordApp.Application.ScreenUpdating = False
WS_Count = ActiveWorkbook.Worksheets.Count

For i = 1 To WS_Count
    ActiveWorkbook.Sheets(i).Activate
    ActiveWorkbook.Sheets(i).Shapes.SelectAll
    Selection.Copy

PasteChartIntoWord WordApp

If i <> WS_Count Then
    With WordApp.Selection
        .Collapse Direction:=0
        .InsertBreak Type:=7
    End With
End If

Application.CutCopyMode = False

Next i
' Text in textboxes -> apply style: nospacing so that text fits in the textboxes in Word

  Dim objTextBox As Object
  Dim objDoc As Object
  Set objDoc = GetObject(, "Word.Application").ActiveDocument
  For Each objTextBox In objDoc.Shapes
  If objTextBox.TextFrame.HasText Then
  objTextBox.TextFrame.TextRange.ParagraphFormat.LineSpacingRule = 0
  objTextBox.TextFrame.TextRange.ParagraphFormat.SpaceAfter = 0
  End If
  Next objTextBox



Call sourceSheet.Activate
Application.ScreenUpdating = True
WordApp.Application.ScreenUpdating = True


'Determine how many seconds code took to run
  MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
'Notify user in seconds
  MsgBox "Done! " & MinutesElapsed & " minutes", vbInformation
 End Sub




 Function PasteChartIntoWord(WordApp As Object) As Object

' Remove textbox selection
ActiveCell.Select
  Range("BB100").Select
  ActiveWindow.SmallScroll up:=100
  ActiveWindow.SmallScroll ToLeft:=44

' create a header with sheetname for quick referencing!
WordApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
WordApp.Selection.Font.Size = 36
WordApp.Selection.Font.Underline = wdUnderlineSingle
WordApp.Selection.Font.ColorIndex = wdRed
WordApp.Selection.TypeText Text:=ActiveSheet.Name

' Paste the textboxes
WordApp.Selection.PasteSpecial DataType:=wdPasteShape

End Function
这是唯一一个与受损的excel查找文本函数相等的解决方案。

Sub Export()
' THIS must be enabled in Excel: Developer > Visual basic > Tools > References > Microsoft word 1x Object library
'Known bug: if the worksheet has only 1 textbox it will mess up the copy to word. You can manually fix it by adding another textbox in that worksheet. It can be empty.
'Ctrl+break -> will stop the process
'If Word crashes -> the clipboard size is too large.
'Reduce the sheet size or split it in 2 sheets.
'The clipboard limitation is an excel wide limitation.
    
Dim StartTime As Double
Dim MinutesElapsed As String
StartTime = Timer

MsgBox " Wait for job completed textbox in excel!" & vbCrLf & "Close any other WORD files!"
Dim WordApp As Word.Application
Dim i As Integer
Dim sourceSheet As Worksheet
Set sourceSheet = ActiveSheet
Application.ScreenUpdating = False
Sheet1.Activate
Set WordApp = CreateObject("Word.Application")
On Error Resume Next
WordApp.Documents.Add
    With WordApp.ActiveDocument.PageSetup
            .PageWidth = InchesToPoints(22)
            .PageHeight = InchesToPoints(22)
    End With

WordApp.ActiveWindow.View.Type = wdWebView

WordApp.Visible = True
WordApp.Application.ScreenUpdating = False
WS_Count = ActiveWorkbook.Worksheets.Count

For i = 1 To WS_Count
    ActiveWorkbook.Sheets(i).Activate
    ActiveWorkbook.Sheets(i).Shapes.SelectAll
    Selection.Copy

PasteChartIntoWord WordApp

If i <> WS_Count Then
    With WordApp.Selection
        .Collapse Direction:=0
        .InsertBreak Type:=7
    End With
End If

Application.CutCopyMode = False

Next i
' Text in textboxes -> apply style: nospacing so that text fits in the textboxes in Word

  Dim objTextBox As Object
  Dim objDoc As Object
  Set objDoc = GetObject(, "Word.Application").ActiveDocument
  For Each objTextBox In objDoc.Shapes
  If objTextBox.TextFrame.HasText Then
  objTextBox.TextFrame.TextRange.ParagraphFormat.LineSpacingRule = 0
  objTextBox.TextFrame.TextRange.ParagraphFormat.SpaceAfter = 0
  End If
  Next objTextBox



Call sourceSheet.Activate
Application.ScreenUpdating = True
WordApp.Application.ScreenUpdating = True


'Determine how many seconds code took to run
  MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
'Notify user in seconds
  MsgBox "Done! " & MinutesElapsed & " minutes", vbInformation
 End Sub




 Function PasteChartIntoWord(WordApp As Object) As Object

' Remove textbox selection
ActiveCell.Select
  Range("BB100").Select
  ActiveWindow.SmallScroll up:=100
  ActiveWindow.SmallScroll ToLeft:=44

' create a header with sheetname for quick referencing!
WordApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
WordApp.Selection.Font.Size = 36
WordApp.Selection.Font.Underline = wdUnderlineSingle
WordApp.Selection.Font.ColorIndex = wdRed
WordApp.Selection.TypeText Text:=ActiveSheet.Name

' Paste the textboxes
WordApp.Selection.PasteSpecial DataType:=wdPasteShape

End Function
子导出()
'这必须在Excel中启用:开发者>Visual basic>工具>引用>Microsoft word 1x对象库
'已知错误:如果工作表只有一个文本框,它会将复制到word的内容弄乱。您可以通过在该工作表中添加另一个文本框来手动修复它。它可以是空的。
“Ctrl+break->将停止进程
'如果Word崩溃->剪贴板大小太大。
'减小图纸尺寸或将其拆分为两张图纸。
'剪贴板限制是excel范围内的限制。
暗淡的开始时间是双倍的
微弱的分钟以字符串形式流逝
开始时间=计时器
MsgBox“在excel中等待作业完成文本框!”&vbCrLf&“关闭任何其他WORD文件!”
Dim WordApp作为Word.Application
作为整数的Dim i
将源表设置为工作表
设置sourceSheet=ActiveSheet
Application.ScreenUpdating=False
表1.激活
设置WordApp=CreateObject(“Word.Application”)
出错时继续下一步
WordApp.Documents.Add
使用WordApp.ActiveDocument.PageSetup
.PageWidth=InchesToPoints(22)
.PageHeight=英寸的切点(22)
以
WordApp.ActiveWindow.View.Type=wdWebView
可见=True
WordApp.Application.ScreenUpdate=False
WS\u Count=ActiveWorkbook.Worksheets.Count
对于i=1到WS\u计数
ActiveWorkbook.Sheets(i).激活
ActiveWorkbook.Sheets(i).Shapes.SelectAll
选择,复制
粘贴图表到Word WordApp
如果我算的话
使用WordApp.Selection
.折叠方向:=0
.InsertBreak类型:=7
以
如果结束
Application.CutCopyMode=False
接下来我
'文本框中的文本->应用样式:无空格,以便文本适合Word中的文本框
作为对象的Dim objTextBox
作为对象的Dim objDoc
设置objDoc=GetObject(,“Word.Application”).ActiveDocument
对于objDoc.Shapes中的每个objTextBox
如果是objTextBox.TextFrame.HasText,则
objTextBox.TextFrame.TextRange.ParagraphFormat.LineSpacingRule=0
objTextBox.TextFrame.TextRange.ParagraphFormat.SpaceAfter=0
如果结束
下一个objTextBox
调用sourceSheet.Activate
Application.ScreenUpdating=True
WordApp.Application.ScreenUpdate=True
'确定代码运行所需的秒数
分钟经过=格式((计时器-开始时间)/86400,“hh:mm:ss”)
'在几秒钟内通知用户
MsgBox“完成!”&minutesapped和“分钟”,vbInformation
端接头
函数PasteChartIntoWord(WordApp作为对象)作为对象
'删除文本框选择
ActiveCell。选择
范围(“BB100”)。选择
ActiveWindow.small向上滚动:=100
ActiveWindow.SmallScroll ToLeft:=44
'使用sheetname创建标题,以便快速引用!
WordApp.Selection.ParagraphFormat.Alignment=wdAlignParagraphRight
WordApp.Selection.Font.Size=36
WordApp.Selection.Font.Underline=wdUnderlineSingle
WordApp.Selection.Font.ColorIndex=wdRed
WordApp.Selection.TypeText:=ActiveSheet.Name
'粘贴文本框
WordApp.Selection.PasteSpecial数据类型:=wdPasteShape
端函数