簡體   English   中英

循環遍歷單元格范圍與工作表

[英]Loop through cells range against worksheets

我有一張有很多床單的工作簿。 我有一張工作表中某處寫的工作表名稱列表。 我需要編寫一個函數來保護那些不在此列表中的工作表。

我想我被困在一行代碼中。 你可以幫幫我嗎。

Public Function Test()
On Error GoTo Test_Err

Dim wks, xSheet As Worksheet
Dim myRange As Range
Dim Cell As Range

Set wks = Worksheets("HideTabs")

With wks
Set myRange = .Range(.Cells(1, "D"), .Cells(.Rows.Count, "D").End(xlUp))
End With

For Each Cell In myRange
For Each xSheet In ActiveWorkbook.Worksheets

If xSheet.Name = Cell.Value Then
' Do Something
Exit For
Else
' Do Something else
End If

Next xSheet
Next Cell

Exit Function
Test_Err:
      MsgBox "The Error Happened on (Test Function) Line : " & Erl & vbNewLine & _
           "Error Message : " & Err.Description & vbNewLine & _
           "Error Number : " & Err.Number & vbNewLine & vbNewLine & _
           sMsg, vbCritical, sTitle

End Function

我成功地編寫了一個函數來遍歷所有工作簿表並將其與單元格范圍中的已定義列表進行比較,然后執行一些操作。

Public Function Test()

Dim wks, xlWSH As Worksheet
Dim myRange, Cell As Range
Dim ProtectIt As Boolean

'Refer to sheet name where you save your sheet names list
Set wks = Worksheets("SheetName")
With wks
'Refer to first cell where your sheet names list starts. Here is "A1"
Set myRange = .Range(.Cells(1, "A"), .Cells(.Rows.Count, "A").End(xlUp))
End With

For Each xlWSH In ActiveWorkbook.Worksheets

For Each Cell In myRange
'If sheet name is in your list then set DoIt to False
If xlWSH.Name = Cell.Value Then
DoIt = False
Exit For
Else
DoIt = True
End If
Next Cell

If DoIt = True Then
With xlWSH
'Do Some Actions With Sheet
End With
End If

Next xlWSH

End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM