运行宏多行

时间:2012-07-03 21:44:47

标签: excel excel-vba vba

我有一个写在一行上工作的宏。我想在所有突出显示的行上运行此宏。

是否有办法在所选行之间循环并为每个行运行宏。

由于公司政策,我无法显示整个宏,但它基本上将excel中的值连接起来并将其填充到单词模板中。这是宏的开始:

Sub OpenForm()
With Selection
    Dim pappWord As Object
    Dim docWord As Object
    Dim wb As Excel.Workbook
    Dim TodayDate As String
    Dim Path As String

    Set wb = ActiveWorkbook
    TodayDate = Format(Date, "mmmm d, yyyy")
    Path = wb.Path & "\MAF Template.dot"

    On Error GoTo ErrorHandler

     'Create a new Word Session
    Set pappWord = CreateObject("Word.Application")

    On Error GoTo ErrorHandler

     'Open document in word
    Set docWord = pappWord.Documents.Add(Path)
    'Blank for Qty
    docWord.FormFields("Text38").Result = Range(ActiveCell.EntireRow.Address)(1, 2) 'Part of System (System ID)

....它填充更多字段并以:

结束
    With pappWord
        .Visible = True
        .ActiveWindow.WindowState = 0
        .Activate
    End With


     'Release the Word object to save memory and exit macro
ErrorExit:
    Set pappWord = Nothing
    Exit Sub

     'Error Handling routine
ErrorHandler:
    If Err Then
        MsgBox "Error No: " & Err.Number & "; There is a problem"
        If Not pappWord Is Nothing Then
            pappWord.Quit False
        End If
        Resume ErrorExit
    End If
End With
End Sub

因此它适用于单个excel行,而不是为每一行运行此行,我想选择一些行并通过所有行运行宏。

谢谢,

2 个答案:

答案 0 :(得分:0)

问:有没有办法在所选行之间循环并为每个行运行宏?答:是的。例如,Google为“VBAScript cell range”。例如:

'希望这有助于......至少一点点。

在我们可以帮助您之前,您必须发布一些代码并详细说明您的应用程序。

答案 1 :(得分:0)

试试这个:

Dim a As Range, b As Range

Set a = Selection

For Each b In a.Rows

    b.Value = "xpto"
    //your code will here...but only seeing your code to be more precise

Next
相关问题