2

我正在尝试将一个单元添加1到一个范围内的多个单元格中。下面是我所在的位置,我不断收到类型不匹配错误:

Dim r As Range, cell As Range

Set r = Range("D2:E1000")

For Each cell In r
    If cell.Value > 0 Then
        cell.Value = cell.Value + 1
    End If
Next
4

4 回答 4

5

这是另一种方式

Sub Sandwich()

    Dim rTemp As Range
    Dim rTarget As Range
    Dim sNumFmt As String

    'Define the ranges
    Set rTemp = Sheet2.Cells(Sheet2.Rows.Count, 1).End(xlUp).Offset(1, 0)
    Set rTarget = Sheet2.Range("D2:E1000")

    'store a temporary value and the current number format
    rTemp.Value = 1
    sNumFmt = rTarget.Cells(1).NumberFormat

    'copy and paste special-add
    rTemp.Copy
    rTarget.PasteSpecial , xlPasteSpecialOperationAdd

    'get rid of the temp and reapply the format
    rTemp.ClearContents
    rTarget.NumberFormat = sNumFmt

End Sub
于 2013-11-06T20:51:34.823 回答
3

怎么样:

Sub marine()
    Dim r As Range, cell As Range

    Set r = Range("D2:E1000")

    For Each cell In r
        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
于 2013-11-06T19:39:18.620 回答
3

停止在OK, I'm trying to add a unit of "1" to many cells in a range.最简单的方式通常是没有 VBA 或代码。只需1在电子表格的某处插入,复制该单元格,选择感兴趣的范围并使用添加操作选择性粘贴。然后1可以删除。

于 2013-11-27T04:07:48.200 回答
0
Dim r As Range, cell As Range

Set r = Range("D2:E1000")

For Each cell In r
    If cell > 0 Then
        cell.Value = cell.Value + 1
    End If
Next

为我工作,也许你想要 IF cell = 0 ??? 等待您的评论,看看我是否回答了您的问题。

于 2013-11-06T19:37:35.717 回答