代码之家  ›  专栏  ›  技术社区  ›  Alvi

无法按单元格循环反转列选择

  •  0
  • Alvi  · 技术社区  · 6 年前

    我无法反转列选择我收到错误424对象要求。

    代码在这样的手动选择下工作得很好 Set MyRange = Selection

    Sub Text2Numb()
        Dim ws As Worksheet
        Set ws = ActiveSheet
        Dim MyRange As Range
        Dim MyCell As Range
        Set MyRange = Range("A:M").Select
        For Each cell In MyRange
            If cell.Value > 0 Then
                cell.Value = -cell.Value
            Else
                cell.Value = -cell.Value
            End If
        Next cell
    End Sub
    

    运行时错误424 所需对象

    1 回复  |  直到 6 年前
        1
  •  3
  •   Vityata    6 年前

    这是在单元格中反转值的方法:

    cell.Value = cell.Value * -1
    

    IsNumeric(myCell) .

    Sub ReverseNumbers()
    
        Dim myCell As Range
        Dim myRange As Range
        Dim ws As Worksheet
        Set ws = ActiveSheet
    
        Set myRange = ws.Range("A1:A5")
        For Each myCell In myRange
            If IsNumeric(myCell) Then
                If myCell.Value <> 0 Then
                    myCell.Value = myCell.Value * -1
                End If
            End If
        Next myCell
    
    End Sub
    

    另外,考虑避免 ActiveSheet Select - How to avoid using Select in Excel VBA .