当每个单元格填充时,从XLS文件自动生成PDF

发布于 2025-01-30 03:57:25 字数 1042 浏览 3 评论 0原文

我从VBA代码开始,想从XLS文件生成PDF。 此XLS文件是每天的一周时间表,每天填写0或1填充:

D1D2D2 D3D4验证预期
101144OK

,然后我计算总和并与预期的总和进行比较。如果是相等的,那么列验证还可以,否则是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 :

D1D2D3D4SUMEXPECTEDVALIDATE
101144OK

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末 2025-02-06 03:57:25

如果范围(“ ai2”)。选择='ok',那么 - 这将无法按照您的想法和使用“确定”而不是'ok'而不是

使用worksheet_change事件来控制该值。 (您只能在工作表模块中使用它)

    Private Sub Worksheet_Change(ByVal Target As Range)
         If Not (Intersect(Target, Range("AI2")) Is Nothing) Then
             If Target.Value2="OK" Then 
                  ... ' add your code
             End if
         End if
    End Sub

.ActiveSheet而无需“使用应用程序...结尾”构造将为您带来错误。使用Application.ActiveSheet

没有理由使用range.select方法。而不是:

    Range("AG2").Select
    ActiveCell.FormulaR1C1 = "=SUM((RC[-31]:RC[-1]))"

使用:

    Range("AG2").FormulaR1C1 = "=SUM((RC[-31]:RC[-1]))"

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)

    Private Sub Worksheet_Change(ByVal Target As Range)
         If Not (Intersect(Target, Range("AI2")) Is Nothing) Then
             If Target.Value2="OK" Then 
                  ... ' add your code
             End if
         End if
    End Sub

.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:

    Range("AG2").Select
    ActiveCell.FormulaR1C1 = "=SUM((RC[-31]:RC[-1]))"

Use:

    Range("AG2").FormulaR1C1 = "=SUM((RC[-31]:RC[-1]))"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文