【问题标题】:Change Range of Numbers to be Negative in VBA [closed]在VBA中将数字范围更改为负数[关闭]
【发布时间】:2018-06-07 20:52:00
【问题描述】:

我正在尝试在 excel 中编写宏。理想情况下,我会选择一组带有数字的单元格并按下一个键,这些数字都会变成负数。这将为我节省大量时间,我希望有一个解决方案。

【问题讨论】:

  • 在单元格中输入-1,复制它,选择要更新的范围,然后粘贴乘法。
  • 欢迎来到 SO。即使您的问题很容易使用 VBA 或公式来解决,但这并不是提供即用型代码服务的地方。作为一个好的开始,我建议您在网上搜索“Excel 宏录制”,当您遇到这种情况时返回这里寻求帮助。
  • 现有数字是否为负数?是否有任何单元格包含公式?
  • selection.numberformat = "-general"

标签: excel range negative-number vba


【解决方案1】:

考虑:

Sub Negativize()
    Dim cel As Range
    For Each cel In Selection
        cel.Value = -Abs(cel.Value)
    Next cel
End Sub

【讨论】:

    【解决方案2】:

    尝试使用此代码进行选择:

    Sub negative()
    
        Dim cel As Range
        Dim selectedRange As Range
    
        Set selectedRange = Application.Selection
    
        For Each cel In selectedRange.Cells
            If cel.Value > 0 Then
            cel.Value = cel.Value - (cel.Value * 2)
            End If
        Next cel
    
    End Sub
    

    或者这个对于一个单元格:

    Sub negative()
    if ActiveCell.Value > 0 then
    ActiveCell.Value = ActiveCell.Value - (ActiveCell.Value * 2)
    end if
    
    End Sub
    

    【讨论】:

      【解决方案3】:
      Sub Make_Selection_Negative()
          Dim cell As Object
      
          For Each cell In Selection
              cell = cell - cell - cell
          Next cell
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-23
        • 2023-04-06
        • 2021-08-25
        • 2021-07-05
        • 1970-01-01
        • 2021-11-24
        • 2013-09-15
        相关资源
        最近更新 更多