2016-05-23 41 views
0

我有一个工作簿40个工作表,其中30个工作表具有相同的Excel表格设置。我需要一个宏,它将在“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 

回答

1

我认为这30出来的40张有名称,如“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) 
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 
相关问题