excel - 修改当前代码循环到下一行

标签 excel vba

我正在尝试创建一个宏,它将第一行 A8:V8 从 [Summary] 选项卡复制到 [Annual Statement] 选项卡,重新计算,然后将 [Annual Statement] 选项卡保存为 pdf,其中单元格 A8 的名称来自 [摘要] 选项卡。此代码按原样用于第一行 A8:V8。我想让这个宏更加动态并循环到下一行,A9:V9,然后重复将值复制到 [Annual Statement] 选项卡并保存为 pdf 的相同过程,然后再次重复整个过程下一行。
这是代码 -

Sub AnnualStatements()

Dim RI As Workbook
Set RI = ThisWorkbook

Dim strpath As String
Dim strName As String
Dim strFile As String
Dim strPathFile As String


**'Copies over policy information from summary to annual statement tab**

Worksheets("Summary").Range("A8:V8").Copy Worksheets("Annual Statement").Range("O3")

Calculate

**'Below is what I want to change the above line to but it isn't working**

For i = 1 to 10

Worksheets("Summary").Range(Cells(7+i, 1), Cells(7+i, 21)).Copy Worksheets("Annual Statement").Range("O3")

Calculate

**Creates location and path to save annual statement pdf file to**
strpath = "C:Users\Documents"
strName = Sheets("Summary").Range("A8")

**‘strName = Sheets(“Summary”).Cells(7+i,1)**  '' tried changing to this but not working

strFile = strName & "_Annual Statement" & ".pdf"
strPathFile = strpath & strFile

**Saves as pdf**

Worksheets("Annual Statement").ExportAsFixedFormat _
        Type:=xlTypePDF, _
        FileName:=strPathFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

**'Next i 'tried adding this but not working** 

End Sub

最佳答案

此代码将循环直到在 Summary 的 A 列中找到一个空白单元格。工作表。

Option Explicit

Sub AnnualStatements()
Dim RI As Workbook
Dim rngData As Range
Dim strpath As String
Dim strName As String
Dim strFile As String
Dim strPathFile As String

    Set RI = ThisWorkbook

    strpath = "C:Users\Documents\"

    Set rngData = RI.Worksheets("Summary").Range("A8:V8")

    Do
        'Copies over policy information from summary to annual statement tab**
        rngData.Copy RI.Worksheets("Annual Statement").Range("O3")

        Calculate

        ' Create filename for PDF

        strName = rngData.Cells(1,1).Value

        strFile = strName & "_Annual Statement" & ".pdf"

        strPathFile = strpath & strFile

        ' Saves as pdf**

        RI.Worksheets("Annual Statement").ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=strPathFile, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=False
                
        Set rngData = rngData.Offset(1)

    Loop Until rngData.Cells(1, 1).Value = ""
    
End Sub

关于excel - 修改当前代码循环到下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69017005/

相关文章:

excel - 使用 RELATED 和 FILTER 交叉引用 DAX 中的两个表?

excel - 在没有数据透视表的情况下根据每日数据计算月平均值

excel - 如何在表单中双击查看单元格详细信息?

excel - 从已关闭的 Excel 工作表中复制整个数据并将其作为值和格式粘贴到事件工作簿

vba - Excel VBA : Copy Sheet from a workbook to another workbook in different location

excel - 复制工作表 1 中的范围并粘贴到循环中下一张工作表的下一个空行

excel - POI : Unexpected record type org. apache.poi.hssf.record.HyperlinkRecord

excel - 查找指定列中的值

javascript - 无法在 IE11 中使用 SheetJS 在 AngularJS UI Grid 中上传 excel

vba - 带 double 的 Mod