顶部广告

如何将excel公式打印到Word中

这里是正文头部广告

正文:

如何将excel公式打印到Word中

有时我们需要将指定单元格或区域中的公式打印出来以便分析。下面的VBA代码可以实现这个功能可以将选择的单元格或区域中的公式打印到Word中方便打印。

使用方法:

1.单击菜单“工具→引用”在弹出的“引用 VBAproject”窗口中勾选“Microsoft Word 11.0 Object Library ”

2.在VBA编辑器中单击菜单“插入→模块”在右侧的代码窗口中输入下列代码。

Public Sub PrintFormulasToWord(

Dim Cnt As StringDim C As Range

Dim WordObj As Word.ApplicationDim HasArr As Boolean

On Error Resume NextErr.Number = 0

Set WordObj = GetObject(, "Word.Application")If Err.Number = 429 ThenSet WordObj = CreateObject("Word.Application")Err.Number = 0End If

WordObj.Visible = TrueWordObj.Documents.Add

With WordObj.Selection.Font.Name = "Courier New".TypeText "工作表名称:" + ActiveSheet.Name.TypeParagraph.TypeText "单元格: " + Selection.Cells(1, 1).Address(False, False, xlA1) _& " to " & Selection.Cells(Selection.Rows.Count, _Selection.Columns.Count).Address(False, False, xlA1).TypeParagraph.TypeParagraphEnd With

For Each C In SelectionHasArr = C.HasArrayCnt = C.FormulaIf HasArr ThenCnt = "{" + Cnt + "}"End IfIf Cnt <> "" ThenWith WordObj.Selection.Font.Bold = True.TypeText C.Address(False, False, xlA1) & ": ".Font.Bold = False.TypeText Cnt.TypeParagraph.TypeParagraphEnd WithEnd IfNext CMsgBox "已完成将指定单元格公式打印到Word中。 ", , "打印公式到Word"

End Sub

3.在工作表中选择需要打印的区域回到VBA编辑器中运行代码。

banner广告2
banner广告3

发表评论