可以执行循环来运行一组定义的变量吗?

时间:2019-06-26 13:53:15

标签: vba loops ms-word

我正在编写一些代码,想知道是否可以循环。实际上,我实际上是为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+次。

2 个答案:

答案 0 :(得分:0)

我建议使用数组。您可以使用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

答案 1 :(得分:0)

谢谢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