
有时我们需要将指定单元格或区域中的公式打印出来,以便分析。下面的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
excel利用VBA获取系统特殊文件夹路径
Windows 特殊文件夹,包括 "Desktop" 文件夹、"MyDocuments" 文件夹等,如果我们要在VBA中用到这些文件夹,可以用下列代码来获取这些特殊文件夹的路径。 Option ExplicitSub GetMyDocumentsPath()Dim WSHShel
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编辑器中,运行代码。
EXCEL如何使用VBA汇总当前路径下的工作簿的相应的工作表?
一个论坛上的会员有如下的要求:如图需要把当前路径中的财务、采购、工厂、计划、人士等工作簿的“出勤明细”工作表的记录复制汇总在“加班汇总表”工作簿中的“加班原因汇总”工作表中,并需要取出各部门的名称,希望批量使用VBA完成?很久没有玩VBA了,今晚手