问题描述
我正在尝试使用vba将Excel中的当前工作表保存为PDF并将其导出为PDF.该代码有效,但是要发布的PDF是整个工作表(其中包括几百行空白行和列,这些行和列超出了我的文字位置.在此链接 1 ,其中提供了有关如何通过另存为"中的选项"菜单执行此操作的教程我希望我的代码为我做到这一点,并选择仅包含文本的区域,此导出的工作表将被许多人用来创建PDF,并且该工作表中的数据大小会发生变化这说明了我在导出文件时希望通过代码自动执行此操作的原因.任何帮助都将非常有用.
I am trying to save and export a current sheet in Excel as a PDF using vba. The code is working, however the PDF that is being published is the entire sheet (which includes a couple hundred blank rows and columns past where I have text. At this link 1, there is a tutorial on how to do this through the "Options" menu in the Save As window. I would like my code to do this for me, and select the range that encompasses only where I have text. This worksheet being exported is going to be used by many to create PDF's, with changes in the size of data in this worksheet. This explains my reasoning for wanting to do this through the code automatically as the file is exported. Any help would be great. Thanks!
推荐答案
设置打印区域,这会将您的打印区域从A1设置为"D"列中的最后一个单元格
Set print area,This will set your print area from A1 to the last cell in Column "D"
Sub Set_PrintRnag()
Dim LstRw As Long
Dim Rng As Range
LstRw = Cells(Rows.Count, "D").End(xlUp).Row
Set Rng = Range("A1:D" & LstRw)
ActiveSheet.PageSetup.PrintArea = Rng.Address
End Sub
这篇关于将特定范围的单元格导出为PDF-VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!