在多张纸上将单元格值增加一个

时间:2016-05-23 02:07:17

标签: excel vba excel-vba

我有一张40张工作簿,其中30张设置了相同的excel表。我需要一个宏,它会在列#34; Months Billed"的表格范围内的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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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