我从未写过VBA代码,但我在网上查了一些信息。
我的愿望如下:我有一个包含3张纸的Excel文件。在其中一个,我想添加一个按钮,可以:
[name of a cells of a page]_AP_[date of today].xls
。我已经开始做了一些事情,但我在编程上非常糟糕:
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
之后会有另一个选项和细节,但这确实是基础。
答案 0 :(得分: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) "打印3张照片中的2张,调整"
我不确定我是否得到这个......
打印命令由:
给出Set oSheet= thisworkbook.sheets(2)
with oSheet.PageSetup
.PrintArea = "$A1$1:$Q$40"
...
'任何其他属性: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
答案 1 :(得分:0)
选项明确 Sub 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
End Sub
此 1.以pdf格式保存我的文件 2.不会提示我参加另存为对话框 3.使用A1和日期戳中的单元格值保存文件