2012-07-04 36 views
0

我从来没有写过VBA代码,但我在网上查了一些信息。保存一个Excel文件并将其导出为PDF格式的另一张表

我的愿望如下:我有一个Excel文件3张。其中之一,我想添加一个按钮,它可以:

  1. 保存我的Excel文件遵循此命名约定的整体:[name of a cells of a page]_AP_[date of today].xls
  2. 将其中一张纸保存在.pdf文件中。
  3. 调整内容时打印3张纸中的2张。

我已经开始了一些东西,但我真的不擅长编程:

Public Sub Savefile_Click() 'copie sauvegarde classeur 

' save my file following a name 
Dim nom As String 
Dim chemin As String 
Dim wSheet As Worksheet 

    chemin = "C:\Users\aaa\Desktop" 
    nom = [Q13].Value & "_" & Day(Date) & "-" & Month(Date) & "-" & Year(Date) _ 
      & ".xlsm" 
    With ActiveWorkbook 
    .SaveAs Filename:=chemin & nom 
    .Close 
    rep = MsgBox("Fichier excell sauvegardé") 
    End With 

' ... and print my active sheet (where the button will stay) 
For Each wSheet In ActiveWorkbook.Worksheets 
If wSheet.Visible Then wSheet.PrintOut 
Next 

'Save my page 'offre' in pdf on my desktop and print it 
    Worksheets("OFFRE A ENVOYER").Range("A1:i47").ExportAsFixedFormat _ 
    Type:=xlTypePDF, _ 
    Filename:=[Q13].Value & "_Offre de prix", _ 
    Quality:=xlQualityStandard, _ 
    IncludeDocProperties:=True, _ 
    IgnorePrintAreas:=False, _ 
    OpenAfterPublish:=False 

End Sub 

后会有另一种选择和细节,但这是真正的基础。

回答

1

1)另存为Excel

Dim nom As String 

nom = ThisWorkbook.Sheets(1).Range("Q13").Value & "AP" & Format(Date, "ddmmyyyy") & ".xls" 
thisworkbook.saveas sPath & nom 'Define path first, don't forget the \ at the end. 

更妙的是创建范围是“Q13”中的命名范围及用途:

nom = thisworkbook.names("Something").referstorange.value 

,使链接动态的情况下,您将列或行移动所有范围。

2)保存工作簿保存为PDF

ThisWorkbook.ExportAsFixedFormat xlTypePDF, sPath & sFile 'Define here .pdf 

3) “的3sheets的打印2调整的的contenant”

我不知道如果我得到这个...

Set oSheet= thisworkbook.sheets(2) 

with oSheet.PageSetup 
    .PrintArea = "$A1$1:$Q$40" 
    ... 

“任何其他propertie:

打印命令由下式给出S:http://www.java2s.com/Code/VBA-Excel-Access-Word/Excel/AllpropertiesofPageSetup.htm

end with 
oSheet.printout 
以往的方式你要哪,以便检索,你需要打印的纸张设定此

。 您可以通过计数器循环显示工作表,并将if语句添加到条件中。

dim oSheet as Excel.worksheet 
dim iCnt as integer  

For each oSheet in thisworkbook.sheets 
    iCnt = iCnt + 1 
    'Include conditions here 
    If ... then 'Whatever condition 
     set oSheet = thisworkbook.sheets(iCnt) 
     'Print 
    end if 
next oSheet 
0

谢谢你...我正在寻找这个。这工作得很好。

显式的选项 子SVME()“保存文件名作为A1的值加上当前日期

Dim newFile As String, fName As String 
' Don't use "/" in date, invalid syntax 
fName = Range("A1").Value 
newFile = fName & " " & Format$(Date, "mm-dd-yyyy") 
' Change directory to suit 
ChDir _ 
"C:\Users\user\Desktop" 'YOU MUST Change USER NAME to suit 
ThisWorkbook.ExportAsFixedFormat xlTypePDF, Filename:=newFile 

末次

这 1.保存我的文件为PDF格式, 2.做不要提示我参加另存为对话框 3.使用单元格值A1保存文件和日期戳

相关问题