2012-07-03 79 views
0

我有一個寫入一行的宏。我想在突出顯示的所有行上運行此宏。運行宏多行

有沒有辦法在所選行之間循環併爲每個行運行宏。

由於公司政策,我無法顯示整個宏,但它基本上取得了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

問:有沒有一種方法對所選行並運行爲每一個宏之間的循環?答:是的。例如,Google針對「VBAScript單元格範圍」。例如:

「希望幫助...至少有一點。

您必須發佈一些代碼並解釋更多關於您的應用程序,然後才能進一步幫助您。

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 
+0

謝謝,我更新了我的帖子。這種工作,但它使用了第一行的數據,並重復了x次。 – mgalal

+0

如果它將其標記爲正確答案 –

+0

雖然它沒有正常工作,但它運行宏的正確次數(突出顯示的行數),但它僅使用來自第一行的數據,並且不會抓取來自其他人的數據。 – mgalal