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

最も簡単な方法で停止するOK, I'm trying to add a unit of "1" to many cells in a range.のは、多くの場合、VBA やコードを使用しないことです。スプレッドシートのどこかに挿入1し、そのセルをコピーし、関心のある範囲を選択して、Operation Add で貼り付けます。その後1、 を削除できます。

于 2013-11-27T04:07:48.200 に答える
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 に答える
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セル= 0が必要ですか??? 私があなたの質問に答えたかどうかを確認するためにあなたのコメントを待っています。

于 2013-11-06T19:37:35.717 に答える