【问题标题】:Increase cell value in range by one on multiple sheets在多个工作表上将范围内的单元格值增加一
【发布时间】:2016-05-23 13:37:54
【问题描述】:

我有一个 40 张工作簿,其中 30 张设置了相同的 Excel 表格。我需要一个宏,它将在“计费月数”列的表格范围内的 30 个工作表中的所有单元格加 1。我尝试了以下在一张纸上工作的方法 - 但不确定如何在多张纸上进行相同的工作 - 我也不需要消息框:

    Sub MonthIncrease()
     Dim r1 As Range
     Dim cell As Range
      Set r1 = Sheets("Customer 1").Range("Customer1[Months Billed]")
    For Each cell In r1
        If IsNumeric(cell.Value) Then
           If cell.Value > 0 Then
           cell.Value = cell.Value + 1
        End If
    Else
        MsgBox "Cell " & cell.Address(0, 0) & " does not have a number"
    Exit Sub
    End If
    Next
   End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我假设 40 张表中的 30 张的名称类似于“客户 1”、“客户 2”……

    在这种情况下是这样的

    Option Explicit
    
    Sub MonthIncrease()
    Dim cell As Range
    Dim i As Long
    
    For i = 1 To 30
        With Sheets("Customer " & i).Range("Customer1[Months Billed]")
            For Each cell In .SpecialCells(xlCellTypeConstants, xlNumbers)
               If cell.Value > 0 Then cell.Value = cell.Value + 1
            Next cell
        End With
    Next i
    
    End Sub
    

    我还删除了 Exit Sub 声明以及 MsgBox 一个

    如果您要搜索的数字是公式的结果,那么只需更改

    .SpecialCells(xlCellTypeConstants, xlNumbers)
    

    进入

    .SpecialCells(xlCellTypeFormulas, xlNumbers)
    

    【讨论】:

      【解决方案2】:
      Sub MonthIncrease()
      Dim r1 As Range, cell As Range, i As Integer
      
      For i = 1 To 30
          Set r1 = Sheets(i).Range("Customer1[Months Billed]")
      
          For Each cell In r1
              If IsNumeric(cell.Value) Then
                 If cell.Value > 0 Then
                 cell.Value = cell.Value + 1
              Else
                  MsgBox "Cell " & cell.Address(0, 0) & " does not have a number"
                  Exit Sub
              End If
          Next
      Next i
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 2020-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-06
        相关资源
        最近更新 更多