【问题标题】:Clear different range of data in multiple worksheets清除多个工作表中不同范围的数据
【发布时间】:2020-10-20 23:38:52
【问题描述】:

我有一个工作簿,用作填写数据的模板。数据被清除,工作簿被重新使用。
工作簿有多个工作表,每个工作表需要清除的范围不同。

假设我要清除 A10:Y50 范围内的数据,我将值“开始”放在单元格 Z10 中,作为清除数据的起点。 “开始”位于每个工作表的不同单元格中。
该代码根据位于第一个工作表中的“开始”清除数据,而不是针对每个工作表独立。

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

【问题讨论】:

  • Cells.Find 和 Cells(iRow, "A") 查看活动工作表。您希望 sht.Cells.Find 和 sht.Cells(iRow, "A") 查看工作表 sht。 sht.Range 是正确的。

标签: excel vba


【解决方案1】:

正如@Tony Dallimore 在他的评论中提到的,您需要在哪个工作表中指定您正在寻找特定单元格(如果您不指定它,它假定您正在查看 ActiveSheet)。因此,最好始终指定您使用的工作表。最好使用 With 语句。当你使用 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

【讨论】:

  • 感谢您的建议,这非常有用!我实际上在工作簿中有 100 多张工作表,因此我更愿意指定要排除哪些工作表,然后指明我要清除内容的工作表。您知道如何做到这一点吗?
  • 我会在特定工作表的单元格中列出他们的名字,例如设置。例如A1 =“Sheet1”,A2 =“Sheet2”等。然后我会在该范围内使用Find,如果找到它,我会跳过它......我可以用简单的代码添加另一个答案......跨度>
  • 非常感谢,因为我是 VBA 新手。
【解决方案2】:

还为每个循环使用 Dim iRow as Long before... 但这里添加了如何管理要跳过的工作表的示例(创建工作表“设置”并在单元格 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-09
    • 2021-08-07
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2022-07-29
    • 2014-09-27
    • 2018-06-19
    相关资源
    最近更新 更多