Office常用函数

本文介绍了Excel中的IF函数用法,以及如何通过VBA实现工作表拆分、选择并删除Word文档中的表格和超链接。此外,还详细讲解了按页拆分Word文档的动态脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Excel常用函数

一、检查第一个字符是否是A,如果是,它返回“Yes”,否则返回”No“。

=IF(LEFT(A2, 1)="A", "Yes", "No")

二、Excel 拆分工作表

Private Sub 分拆工作表()
    Dim sht As Worksheet
    Dim MyBook As Workbook
    Dim NewBook As Workbook
    Set MyBook = ActiveWorkbook
    Application.ScreenUpdating = False ' 关闭屏幕更新以提高性能
    
    For Each sht In MyBook.Sheets
        sht.Copy
        Set NewBook = ActiveWorkbook ' 明确引用新工作簿
        NewBook.SaveAs Filename:=MyBook.Path & "\" & sht.Name & ".xlsx", FileFormat:=xlOpenXMLWorkbook ' 保存为.xlsx格式
        NewBook.Close False ' 关闭新工作簿,不保存更改(因为已经保存过了)
    Next sht
    
    Application.ScreenUpdating = True ' 恢复屏幕更新
    MsgBox "文件已经被分拆完毕!"
End Sub

Word常用函数

一、选择文档中所有表格

Sub SelectAllTables()
     Dim mytable As Table
      Application.ScreenUpdating = False
      For Each mytable In ActiveDocument.Tables
         mytable.Range.Editors.Add wdEditorEveryone
     Next
    ActiveDocument.SelectAllEditableRanges (wdEditorEveryone)
    ActiveDocument.DeleteAllEditableRanges (wdEditorEveryone)
     Application.ScreenUpdating = True
End Sub

二、删除文档中所有超链接

Sub RemoveHyperlinksImproved()
    Dim hyperlink As Hyperlink
    Application.ScreenUpdating = False ' 关闭屏幕更新以提高性能
    On Error GoTo ErrorHandler ' 添加错误处理
    
    ' 遍历并删除所有超链接
    For Each hyperlink In ActiveDocument.Hyperlinks
        hyperlink.Delete
    Next hyperlink
    
    ' 保存文档(可选,如果需要)
    ' ActiveDocument.Save
    
    Application.ScreenUpdating = True ' 恢复屏幕更新
    Exit Sub ' 正常退出
    
ErrorHandler:
    ' 错误处理代码
    MsgBox "发生错误: " & Err.Description, vbCritical
    Application.ScreenUpdating = True ' 确保即使发生错误也恢复屏幕更新
End Sub

三、按页拆分word文件

Option Explicit
Sub DynamicSplitPagesAsDocuments()
    Dim oSrcDoc As Document, oNewDoc As Document
    Dim strSrcName As String, strNewName As String
    Dim oRange As Range
    Dim nIndex As Integer, nSubIndex As Integer, nTotalPages As Integer, nBound As Integer
    Dim fso As Object
    Const nSteps = 2 ' 设置分割步长2表示每2页生成一个新word文件
    
    ' 关闭屏幕更新以提高性能
    Application.ScreenUpdating = False
    
    ' 创建文件系统对象
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' 获取源文档
    Set oSrcDoc = ActiveDocument
    
    ' 获取总页数
    nTotalPages = oSrcDoc.ComputeStatistics(wdStatisticPages)
    
    ' 遍历源文档的每一页,按步长分割
    For nIndex = 1 To nTotalPages Step nSteps
        ' 创建新文档
        Set oNewDoc = Documents.Add
        
        ' 计算分割的边界
        If nIndex + nSteps - 1 > nTotalPages Then
            nBound = nTotalPages
        Else
            nBound = nIndex + nSteps - 1
        End If
        
        ' 复制并粘贴页面到新文档
        For nSubIndex = nIndex To nBound
            ' 复制当前页面
            oSrcDoc.Bookmarks("\page").Range.Copy
            
            ' 粘贴到新文档
            oNewDoc.Content.Paste
            
            ' 切换到下一页(源文档)
            Application.Browser.Target = wdBrowsePage
            Application.Browser.Next
        Next nSubIndex
        
        ' 添加页码和总页数到页脚
        With oNewDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
            .Collapse wdCollapseEnd
            .InsertAfter vbCrLf ' 添加换行以分隔页码和文本(可选)
            .Collapse wdCollapseEnd
            .Fields.Add Range:=.Duplicate, Type:=wdFieldPage, Text:="Page "
            .Collapse wdCollapseEnd
            .InsertAfter " / "
            .Fields.Add Range:=.Duplicate, Type:=wdFieldNumPages, Text:=" of "
            .Fields.Update
            .ParagraphFormat.Alignment = wdAlignParagraphCenter
        End With
        
        ' 构建并保存新文档的文件名
        strSrcName = oSrcDoc.FullName
        strNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _
            fso.GetBaseName(strSrcName) & "_20240" & (nIndex \ nSteps + 1) & "." & fso.GetExtensionName(strSrcName))
        oNewDoc.SaveAs2 FileName:=strNewName, FileFormat:=wdFormatXMLDocument ' 使用SaveAs2方法,并指定文件格式,strNewName 表示新生成word文件的文件名
        oNewDoc.Close False
    Next nIndex
    
    ' 清理对象
    Set oNewDoc = Nothing
    Set oSrcDoc = Nothing
    Set fso = Nothing
    
    ' 恢复屏幕更新
    Application.ScreenUpdating = True
    
    ' 显示完成消息
    MsgBox "结束!"
End Sub

四、将所有文本框中的内容转换成文本

Sub RemoveTextBox2()
    Dim shp As Shape
    Dim oRngAnchor As Range
    Dim sString As String
    For Each shp In ActiveDocument.Shapes
        If shp.Type = msoTextBox Then
            ' copy text to string, without last paragraph mark
            sString = Left(shp.TextFrame.TextRange.Text, _
              shp.TextFrame.TextRange.Characters.Count - 1)
            If Len(sString) > 0 Then
                ' set the range to insert the text
                Set oRngAnchor = shp.Anchor.Paragraphs(1).Range
                ' insert the textbox text before the range object
                oRngAnchor.InsertBefore _
                  "" & sString & ""
            End If
            shp.delete
        End If
    Next shp
End Sub

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值