Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

摘要

本文包含使用中活頁簿中的所有工作表執行迴圈的巨集 (Sub 程序) 的 Microsoft Visual Basic for Applications。此巨集也會顯示每個工作表的名稱。

其他相關資訊

Microsoft 提供程式設計範例僅供說明,不做任何明示或默示的保證。這包括,但不限於適售性或適合某特定用途之默示擔保責任。本文假設您已相當熟悉使用的我們所示範的程式設計語言以及建立和偵錯程序所使用的工具。Microsoft 技術支援工程師可以協助解釋特定程序中,功能,但它們不會修改這些範例以提供附加功能或建構程序來滿足您特定需求。若要執行的巨集範例,請依照下列步驟執行:

  1. 輸入新的模組工作表中的下列的巨集程式碼。

          Sub WorksheetLoop()         Dim WS_Count As Integer         Dim I As Integer         ' Set WS_Count equal to the number of worksheets in the active         ' workbook.         WS_Count = ActiveWorkbook.Worksheets.Count         ' Begin the loop.         For I = 1 To WS_Count            ' Insert your code here.            ' The following line shows how to reference a sheet within            ' the loop by displaying the worksheet name in a dialog box.            MsgBox ActiveWorkbook.Worksheets(I).Name         Next I      End Sub
  2. 若要執行巨集,請將插入點放在一行的文字為"Sub WorksheetLoop(),",然後按 F5。

巨集將活頁簿中執行迴圈,並顯示訊息方塊,以不同的工作表名稱每次執行迴圈。請注意此巨集只會顯示工作表的名稱;活頁簿中,它將不會顯示其他類型的工作表的名稱。您也可以使用 'For Each' 迴圈間循環所有活頁簿中的工作表。

  1. 輸入新的模組工作表中的下列的巨集程式碼。

          Sub WorksheetLoop2()         ' Declare Current as a worksheet object variable.         Dim Current As Worksheet         ' Loop through all of the worksheets in the active workbook.         For Each Current In Worksheets            ' Insert your code here.            ' This line displays the worksheet name in a message box.            MsgBox Current.Name         Next      End Sub
  2. 若要執行巨集,請將插入點放在一行的文字為"Sub WorksheetLoop2(),",然後按 F5。

此巨集的功能相同的 WorksheetLoop 巨集,不同之處在於它會使用不同類型的迴圈,來處理所有使用中活頁簿中的工作表。

參考

如需關於取得說明與 Visual Basic for Applications 的詳細資訊,請參閱下列的文件 「 Microsoft 知識庫 」 中的文件:

VBA: 程式設計的 Visual Basic for Applications 資源

OFF2000: 程式設計的 Visual Basic for Applications 資源

Need more help?

Want more options?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×