简体   繁体   English

清除多个工作表中不同范围的数据

[英]Clear different range of data in multiple worksheets

I have a workbook that is used as a template to fill in data.我有一个工作簿,用作填写数据的模板。 The data is cleared and the workbook is reused.数据被清除,工作簿被重新使用。
The workbook has multiple worksheets and the range that needs to be cleared is different in every worksheet.工作簿有多个工作表,每个工作表需要清除的范围不同。

Let's say I want to clear the data in the range A10:Y50, I put value "Start" in the cell Z10, as a starting point to clear data.假设我想清除 A10:Y50 范围内的数据,我将值“开始”放在单元格 Z10 中,作为清除数据的起点。 "Start" is located in different cell in every worksheet. “开始”位于每个工作表的不同单元格中。
The code is clearing data based on the "Start" located in the first worksheet and not independently for each worksheet.该代码基于第一个工作表中的“开始”清除数据,而不是针对每个工作表独立清除数据。

Sub TestReset()
    
Dim sht As Worksheet
    
For Each sht In ActiveWorkbook.Sheets
    If sht.Name <> "Sheet1" And sht.Name <> "Sheet2" Then '
        Dim iRow As Long, iMax As Long
        
        iRow = Cells.Find(What:="start", LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Row
        iMax = Cells(iRow, "A").End(xlDown).Row
    
        sht.Range("A" & iRow & ":AY" & iMax).ClearContents
    End If   
Next sht
    
End Sub

As @Tony Dallimore mentioned in his comment you need to specify in what sheet you are looking for specific cells (if you dont specify it it assumes you are looking in ActiveSheet).正如@Tony Dallimore 在他的评论中提到的,您需要指定在哪个工作表中查找特定的单元格(如果您不指定它,它假定您正在查找 ActiveSheet)。 So it is always best to specify with what sheet you work.因此,最好始终指定您使用的工作表。 It is good to use With statement for that.最好为此使用 With 语句。 When you use With then it is enough to use only dot "."当您使用 With 时,仅使用点“.”就足够了。

Sub TestReset()
    
Dim sht As Worksheet
    
For Each sht In ActiveWorkbook.Sheets

    With sht

    If .Name <> "Sheet1" And .Name <> "Sheet2" Then '
        Dim iRow As Long, iMax As Long
        
        iRow = .Cells.Find(What:="start", LookIn:=xlFormulas, _
              LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
              MatchCase:=False, SearchFormat:=False).Row
        iMax = .Cells(iRow, "A").End(xlDown).Row
    
        .Range("A" & iRow & ":AY" & iMax).ClearContents
    End If   

    End with
Next sht
    
End Sub

Also use Dim iRow as Long before for each loop... But here is added example of how you can manage sheets to skip (Create sheet "Setup" and in cell A1 add name to skip, for example Sheet1, in cell A2 add Sheet2 and it should do the trick.在每个循环之前也使用 Dim iRow as Long ... 但是这里添加了如何管理要跳过的工作表的示例(创建工作表“设置”并在单元格 A1 中添加要跳过的名称,例如 Sheet1,在单元格 A2 中添加 Sheet2它应该可以解决问题。

Sub WorkInUnSpecifiedSheets()


    Dim xRng As Range
    'sheet "Setup" must exist and Range A1 contains name of sheet to skip, current region might not work on some PCs. But it is simple
    Set xRng = ThisWorkbook.Sheets("Setup").Range("A1").CurrentRegion
    'you can also use another method to specify range... Named ranges for example or using last row and last column...
    
    Dim sht As Worksheet
    
    For Each sht In ActiveWorkbook.Sheets 'i would recommend using ThisWorkbook or Workbook variable instead of Active
    
        If xRng.Find(What:=sht.Name, LookIn:=xlFormulas, _
              LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
              MatchCase:=False, SearchFormat:=False) Is Nothing Then
              
              'your code to work in unspecified sheets
              
        End If
    Next sht

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM