0

我有一个宏,它是为在一行上工作而编写的。我想在所有突出显示的行上运行这个宏。

有没有办法在选定的行之间循环并为每一行运行宏。

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

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 行,而不是为每一行运行它,我想选择一些行并在所有行中运行宏。

谢谢,

4

2 回答 2

0

Try this:

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
于 2012-07-03T21:58:26.610 回答
0

问:有没有办法在选定的行之间循环并为每一行运行宏?答:是的。例如,Google 搜索“VBAScript 单元格范围”。例如:

'希望这会有所帮助......至少有一点。

在我们为您提供进一步帮助之前,您必须发布一些代码并详细解释您的应用程序。

于 2012-07-03T21:50:55.860 回答