当每个单元格填充时,从XLS文件自动生成PDF
我从VBA代码开始,想从XLS文件生成PDF。 此XLS文件是每天的一周时间表,每天填写0或1填充:
D1 | D2 | D2 D3 | D4 | 总 | 验证 | 预期 |
---|---|---|---|---|---|---|
1 | 0 | 1 | 1 | 4 | 4 | OK |
,然后我计算总和并与预期的总和进行比较。如果是相等的,那么列验证还可以,否则是KO。
最后,我想从该文件中生成一个PDF,当用户每天填充用户(D1,..,D4),然后计算validate列和pdf中的导出文件,如果是“确定”,
我可以暂时尝试此操作:
Sub generate_PDF_()
'
' generate_PDF_ Macro
'
Dim saveLocation As String
saveLocation = "path/to/savefolder"
Range("AG2").Select
ActiveCell.FormulaR1C1 = "=SUM((RC[-31]:RC[-1]))"
Range("AI2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]=RC[-1],""OK"",""KO"")"
If Range("AI2").Select = 'OK' Then
.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=saveLocation & "/attendance_sheet" & .ActiveSheet.Name & ".pdf"
End If
End Sub
谢谢提前为您的帮助
I begin in VBA code and i want to generate a PDF from a xls file.
this xls file is a schedule of the week with for each day fill by 0 or 1 like this :
D1 | D2 | D3 | D4 | SUM | EXPECTED | VALIDATE |
---|---|---|---|---|---|---|
1 | 0 | 1 | 1 | 4 | 4 | OK |
Then, i compute the sum and compare with the expected sum. If it's equal then the column validate is OK else it's KO.
Finally, i want to generate a PDF from this file, when the user fill for each DAY (D1,..,D4) then compute the validate column and export file in PDF if it's "OK"
I try this for the moment :
Sub generate_PDF_()
'
' generate_PDF_ Macro
'
Dim saveLocation As String
saveLocation = "path/to/savefolder"
Range("AG2").Select
ActiveCell.FormulaR1C1 = "=SUM((RC[-31]:RC[-1]))"
Range("AI2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]=RC[-1],""OK"",""KO"")"
If Range("AI2").Select = 'OK' Then
.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=saveLocation & "/attendance_sheet" & .ActiveSheet.Name & ".pdf"
End If
End Sub
Thanks in advance for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果范围(“ ai2”)。选择='ok',那么 - 这将无法按照您的想法和使用“确定”而不是'ok'而不是
使用worksheet_change事件来控制该值。 (您只能在工作表模块中使用它)
.ActiveSheet而无需“使用应用程序...结尾”构造将为您带来错误。使用Application.ActiveSheet
没有理由使用range.select方法。而不是:
使用:
If Range("AI2").Select = 'OK' Then - this will not work as you think and use "OK" instead of 'OK'
Use the Worksheet_Change Event to control the value. (You can use it in a worksheet module only)
.Activesheet without "With Application ... end with" construction will give you an Error. Use Application.ActiveSheet
There are no reason to use Range.Select method. Instead of:
Use: