我在Excel中编写VBA代码以修改特定列的值:


如果单元格C(i)是“借方”,则将单元格B(i)中的金额乘以-1
否则什么都不做!


下面是我的代码,但可惜它不起作用:(

    Private Sub Calc()
    For Each transType In Worksheets("Sheet2").Range("C4", "C100")
    myRow = transType.Row
    oldAmount = Worksheets("Sheet2").Range("B" & myRow)
    If transType.Value = "D" Then
    newAmount.Value = oldAmount.Value * -1
    Else:
    newAmount = oldAmount
    End If
    Cells(myRow, "B").Value = newAmount
    Next transType
    End Sub

最佳答案

Private Sub Calc()
Dim transType As Range, oldAmount as range 'important
For Each transType In Worksheets("Sheet2").Range("C4", "C100")
myRow = transType.Row
If transType.Value Like "D*" Then 'if it's "D" something, e.g. "D" or "Deb" or "Debit"
    Worksheets("Sheet2").Range("B" & myRow).Value = Worksheets("Sheet2").Range("B" & myRow).Value * -1
End If
'Cells(myRow, "B").Value = newAmount 'this won't work, it asks for index, the index for column B is 2
Next transType
End Sub

关于excel - Excel VBA代码可否定值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30139927/

10-12 18:28