列表要复制到另一个表格并生成PDF

发布于 2025-01-28 10:59:22 字数 1946 浏览 1 评论 0 原文

我有一个列表,必须在另一个工作表中的每一行中复制日期,并将其保存为PDF

数据在“指令”选项卡中的每一行中,需要在“移动策略”中复制并保存为pdf

以下是每行需要复制的地方,并保存为pdf

“在此处输入图像说明”

我尝试了以下代码,但是它确实可以正常工作,并且创建空白的PDF和第一行永远不会生成。有人可以帮我解决这个问题,以便它可以顺利

 Sub ARSOAPDF()

Dim varItemsToReplace As Variant
Dim varItem As Variant
Dim wksSource As Worksheet
Dim wksDest As Worksheet
Dim rngSource As Range
Dim rngSource2 As Range
Dim rngCell As Range
Dim email_ As String
Dim email2_ As String
Dim cc As String
Dim subject_ As String
Dim path_ As String
 
Path1 = Worksheets("Instruction").Range("G1").Value



Set wksSource = Worksheets("Instruction")
Set wksDest = Worksheets("Mobile Policy")

wksSource.Activate

email_ = cell.Offset(1, 0).Value
email2_ = cell.Offset(1, 1).Value
cc = cell.Offset(1, 2).Value
subject_ = cell.Offset(1, 3).Value
path_ = cell.Offset(1, 4).Value

With wksSource
    Set rngSource = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With

For Each rngCell In rngSource
    With wksDest
    
    
    Range("A75").Formula = email_
    Range("C75").Formula = email2_
    Range("E75").Formula = cc
    Range("G75").Formula = subject_
    
    Range("A1").Select
    
    wksDest.Activate
    
    

         ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
         path1 & ActiveSheet.Range("C2").Value & " - " & ActiveSheet.Range("B2").Value & ".pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
         :=False, OpenAfterPublish:=False
         ActiveWorkbook.Close SaveChanges:=False

         
    End With
Next rngCell

运行

I have a list where I have to copy date in each row in another worksheet and Save it as PDF

Data is in each row in "Instruction" tab, that need to be copied in "Mobile Policy" and saved as PDF

enter image description here

Below is the where each row needs to be copies and saved as PDF

enter image description here

I tried the below code , but it does work properly and creates blank PDF and 1st row never gets generated. Can someone help me fix this please so it can run smoothly

 Sub ARSOAPDF()

Dim varItemsToReplace As Variant
Dim varItem As Variant
Dim wksSource As Worksheet
Dim wksDest As Worksheet
Dim rngSource As Range
Dim rngSource2 As Range
Dim rngCell As Range
Dim email_ As String
Dim email2_ As String
Dim cc As String
Dim subject_ As String
Dim path_ As String
 
Path1 = Worksheets("Instruction").Range("G1").Value



Set wksSource = Worksheets("Instruction")
Set wksDest = Worksheets("Mobile Policy")

wksSource.Activate

email_ = cell.Offset(1, 0).Value
email2_ = cell.Offset(1, 1).Value
cc = cell.Offset(1, 2).Value
subject_ = cell.Offset(1, 3).Value
path_ = cell.Offset(1, 4).Value

With wksSource
    Set rngSource = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With

For Each rngCell In rngSource
    With wksDest
    
    
    Range("A75").Formula = email_
    Range("C75").Formula = email2_
    Range("E75").Formula = cc
    Range("G75").Formula = subject_
    
    Range("A1").Select
    
    wksDest.Activate
    
    

         ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
         path1 & ActiveSheet.Range("C2").Value & " - " & ActiveSheet.Range("B2").Value & ".pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
         :=False, OpenAfterPublish:=False
         ActiveWorkbook.Close SaveChanges:=False

         
    End With
Next rngCell

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

送君千里 2025-02-04 10:59:22

您未正确使用。在每行之前添加一个点。因此 range(“ A75”)。公式=电子邮件_ 应该是 .range(“ A75”)。公式=电子邮件_

它在第二和更多循环中工作,因为您有 wksdest.activate 在循环末端,因此第一个循环将读取ActivesHeet,而不是 wksdest 。在第二个循环中,因为 wksdest 是ActiveOne,所以它可以工作。

可能您需要 ActiveWorkBook.Close Savechanges:= false 在循环外或删除。

You are not using With properly. Add a dot before each line. So Range("A75").Formula = email_ should be .Range("A75").Formula = email_

It works in second and more loops because you have wksDest.Activate at the end of the loop, so the first loop will read the Activesheet, not wksDest. At second loop, because wksDest is the activeone, then it works.

And probably you'll need ActiveWorkbook.Close SaveChanges:=False outside the loop or deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文