为什么 For each 循环不转到 Next sheet?

时间:2021-02-04 15:12:36

标签: excel vba for-loop

我需要处理具有多个选项卡和格式日期的 Excel 工作簿。

我找到了一种格式化日期的方法,我想在它周围放置一个循环。但是循环不起作用,它只更新一张纸。

Sub dotoall()

 
    Dim LastRow As Integer
    Dim FindCol As Range
    Dim sAdd As String
    Dim ws As Worksheet

For Each Sheet In Worksheets

Set ws = ActiveSheet 

    
    With ws
    
     
    
        LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

        'find first instance where DATE exists in row 1 (headers)
        Set FindCol = .Rows(1).Find(What:="DTE", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                    False, SearchFormat:=False)
                    
    
  
    
                 
        'store address of first found instance (to check in loop)
sAdd = FindCol.Address




        
        Do
        
        
        
            'format column (row 2 to last used row)
            .Range(.Cells(2, FindCol.Column), .Cells(LastRow, FindCol.Column)).NumberFormat = "DD-MM-YYYY"
        

            'find next instance (begin search after current instance found)
            Set FindCol = .Cells.FindNext(After:=FindCol)
            
        'keep going until nothing is found or the loop finds the first address again (in which case the code can stop)
        Loop Until FindCol Is Nothing Or FindCol.Address = sAdd
     
    End With
        


Next Sheet




End Sub

1 个答案:

答案 0 :(得分:2)

代替:

For Each Sheet In Worksheets
Set ws = ActiveSheet 

你只想:

For Each ws In Worksheets