【问题标题】:Can a loop be performed to run through a set of defined variables?可以执行循环以遍历一组定义的变量吗?
【发布时间】:2019-11-08 11:59:38
【问题描述】:

我正在编写一些代码,想知道是否可以使用循环。我实际上是为 Word 而不是 Excel 编写这段代码,我通常这样做,但我认为这并不重要。我会说我的 vba 技能水平是初级到中级。该代码旨在使用 Word 中的邮件合并功能,该功能更改文档中的一些功能,然后对文件执行另存为,然后转到下一个文件。

Sub Finsh_Merge_Save_302()


Dim date1 As String
Dim date2 As String
Dim filepath As String
Dim mainfile As String



Dim fund1 As String, fund2 As String, fund3 As String, fund4 As String 'etc.

Dim fundabbv1 As String, fundabbv2 As String, fundabbv3 As String, fundabbv4 As String ' etc.

mainfile = ActiveDocument.Name

fund1 = "blah blah blah 1"
fund2 = "blah blah blah 2"
fund3 = "blah blah blah x"
fund4 = "blah blah blah y"
'etc.

fundabbv1 = " stuff here 1"
fundabbv2 = " stuff here 2"
fundabbv3 = " stuff here x"
fundabbv4 = " stuff here y"
'etc.




    date1 = InputBox("Enter year of filing: 20xx")
    date2 = InputBox("Enter full date of filing: mm-dd-yyyy")
    filepath = "\\Bp211\sys\FAD\FA\TRES (March 2015)\Financial Reporting\Certifications\SOX Compliance and Certifications\N-CSR Certifications\NCSR Filings\" & date1 & "\" & date2

Application.ScreenUpdating = False


    With ActiveDocument.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
            .LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
        End With
        .Execute Pause:=False
    End With
    ActiveDocument.SaveAs2 FileName:=filepath & "\" & fund1 & "\NCSR 302 Certifications" & fundabbv1 & ".docx"
    ActiveDocument.Close

    Windows(mainfile).Activate
    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord



    With ActiveDocument.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
            .LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
        End With
        .Execute Pause:=False
    End With
    ActiveDocument.SaveAs2 FileName:=filepath & "\" & fund2 & "\NCSR 302 Certifications" & fundabbv2 & ".docx"
    ActiveDocument.Close

    Windows(mainfile).Activate
    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord

您可以看到我在哪里使用定义的变量以正确的文件名将文件保存到正确的文件位置。目前,这可以正常工作。问题是我有很多定义的变量,70+,所以我想知道我是否可以创建一个循环而不是编写那段代码 70+ 次。

【问题讨论】:

  • 在一个数组中输入你的变量并循环遍历它,如果你有这么多我会把它们写到一个工作表的单元格中,然后MyArray = Sheets("MySheet").Range("A1:A100").Value 例如,你会得到一个二维数组可以循环,你的文件名将在MyArray(i, 1)
  • 这些变量从何而来?这是某种静态文本吗?还是从数据源中提取的?您真的是指 Word 的邮件合并功能,还是说“邮件合并”是指将信息合并到 Word 文档中?马上,我会质疑将 70 个变量的信息硬编码到宏中。如果这些位置中的任何一个发生更改,则意味着更改宏。更好地从更易于管理的来源读取信息?

标签: vba loops ms-word


【解决方案1】:

我建议使用数组。可以使用Dim fund(1 To 4) As String声明数组,fund(1)获取位置1的值,fund(2)获取位置2的值,等等。

您可以使用索引计数器对它们进行迭代

For i = LBound(fund) To UBound(fund)
   MsgBox fund(i)
Next i

或者每个都有一个

For Each element In fund
   MsgBox element
Next element

【讨论】:

    【解决方案2】:

    谢谢 Nirostar - 您的回答正是我所需要的。这会快速遍历所有文件!

    For i = LBound(fund) To UBound(fund)
    
        With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
                .LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
            End With
            .Execute Pause:=False
        End With
        ActiveDocument.SaveAs2 FileName:=filepath & "\" & fund(i) & "\NCSR 302 Certifications" & fundabbv(i) & ".docx"
        ActiveDocument.Close
    
        Windows(mainfile).Activate
        ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
    
    Next i
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 2011-09-24
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      相关资源
      最近更新 更多