对于具有非特定工作表名称或工作表数量的多个工作表,将单元格范围替换为零

时间:2016-12-01 14:55:59

标签: excel vba loops

我想清除mutliple工作表中特定范围内值= 0的单元格。每次运行宏时,工作表的数量和名称都会不同。这就是我想出来的,但它没有用。

'
' REMOVEZERO_KVEMoxyModel Macro
'
'

Sub REMOVEZERO_KVEMoxyModel()


     Dim ws As Worksheet
     For Each ws In ActiveWorkbook.Worksheets
     Call RemoveZeros


 End Sub

 Sub RemoveZeros()


  For Each cell In Range("I3:I500")
  If cell.Value = "0" Then cell.Clear

End Sub

1 个答案:

答案 0 :(得分:1)

尝试以下代码:它将循环遍历List test = new ArrayList(); try { test.forEach(obj -> { //let say some functionality throws an exception try { throw new IOException("test"); } catch(Exception e) { throw new RuntimeException(e); } }); } catch (RuntimeException re) { if(re.getCause() instanceof IOException) { //do your logic for catching checked } else throw re; // it might be that there is real runtime exception } 中的所有工作表。在此示例中,如果ActiveWorkbookSheet.Name = "Sheet1"(使用Sheet.Name = "Sheet2"),则会进入并清除您指定的Select Case中的所有“0”。

<强>代码

Range

修改1 :清除所有工作表的“0”单元格(删除Option Explicit Sub REMOVEZERO_KVEMoxyModel() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets Select Case ws.Name ' only run the clear "0" cells in "Sheet1" and "Sheet2" >> modify according to your needs Case "Sheet1", "Sheet2" Call RemoveZeros(ws) End Select Next ws End Sub Sub RemoveZeros(sht As Worksheet) Dim cell As Range For Each cell In sht.Range("I3:I500") If cell.Value = "0" Then cell.ClearContents Next cell End Sub ,因为它不需要)

Select Case ws.Name