2016-05-23 49 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 
相關問題