如何将excel公式打印到Word中
时间:2023-07-01 23:06:12
有时我们需要将指定单元格或区域中的公式打印出来,以便分析。下面的VBA代码可以实现这个功能,可以将选择的单元格或区域中的公式打印到Word中,方便打印。
使用方法:
1.单击菜单“工具→引用”,在弹出的“引用 VBAproject”窗口中,勾选“Microsoft Word 11.0 Object Library ”
2.在VBA编辑器中,单击菜单“插入→模块”,在右侧的代码窗口中输入下列代码。
Public Sub PrintFormulasToWord(
Dim Cnt As String
Dim C As Range
Dim WordObj As Word.Application
Dim HasArr As Boolean
On Error Resume Next
Err.Number = 0
Set WordObj = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set WordObj = CreateObject("Word.Application")
Err.Number = 0
End If
WordObj.Visible = True
WordObj.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
.TypeParagraph
End With
For Each C In Selection
HasArr = C.HasArray
Cnt = C.Formula
If HasArr Then
Cnt = "{" + Cnt + "}"
End If
If Cnt <> "" Then
With WordObj.Selection
.Font.Bold = True
.TypeText C.Address(False, False, xlA1) & ": "
.Font.Bold = False
.TypeText Cnt
.TypeParagraph
.TypeParagraph
End With
End If
Next C
MsgBox "已完成将指定单元格公式打印到Word中。 ", , "打印公式到Word"
End Sub
3.在工作表中选择需要打印的区域,回到VBA编辑器中,运行代码。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
大学生购买电脑有什么推荐,不玩游戏?
![](https://img.aspxhome.com/file/2023/9/a343114_0s.jpg)
WPS word怎么添加下一页
![](https://img.aspxhome.com/file/2023/8/a185828_0s.jpg)
网页设计需要学什么
![](https://img.aspxhome.com/file/2023/9/a342767_0s.png)
多个EXCEl工作表Sheet怎么转换成一个PDF?
![](https://img.aspxhome.com/file/2023/7/42327_0s.jpg)
斗鱼tv怎么调亮度?斗鱼tv调亮度教程
![](https://img.aspxhome.com/file/2023/8/a337811_0s.jpg)
PPT怎么制作三维立体的层级金字塔?
![](https://img.aspxhome.com/file/2023/10/a345826_0s.jpg )
ppt中如何加入word文件_把word文档内容导入ppt教程
![](https://img.aspxhome.com/file/2023/5/a315169_0s.png)
赛博朋克2077搜索荒坂办公室怎么过关?赛博朋克2077搜索荒坂办公室过关方法
![](https://img.aspxhome.com/file/2023/6/a324727_0s.jpg)
PPT模版去除水印的方法实例教程
Win7系统开机后右下角的PC问题提示关闭方法
![](https://img.aspxhome.com/file/2023/29/a256448_0s.jpg)
修改注册表显示被病毒恶意隐藏文件
Excel打开工作簿时总是打开指定的工作表
![](https://img.aspxhome.com/file/2023/0/a156430_0s.jpg)
不用光盘直接硬盘安装Windows 2008系统方法
Win10电脑怎么设置定时关机?
![](https://img.aspxhome.com/file/2023/1/a278948_0s.png)
win10升级注意事项有哪些以及免费升级win10需要什么条件?
![](https://img.aspxhome.com/file/2023/28/a248618_0s.jpg)
Win7系统格式化命令format怎么用?
![](https://img.aspxhome.com/file/2023/26/a228822_0s.jpg)
Win10 1909怎么升级21H1?Win10 1909升级21H1图文教程
![](https://img.aspxhome.com/file/2023/26/a222034_0s.jpg)
Excel的相对引用.绝对引用和混合引用
![](https://img.aspxhome.com/file/2023/1/38311_0s.gif)
328元 双千兆网络 华为荣耀路由你值得拥有
![](https://img.aspxhome.com/file/2023/2/a285819_0s.jpg)
“由于没有远程桌面授权服务器可以提供许可证,远程会话被中断。”的解决方法
![](https://img.aspxhome.com/file/2023/30/a263604_0s.png)