Macro to Convert all the data in column in Negative Value

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
146
HI All ,

I have written below code for converting all the numbers in the column to negative value however if some text comes in between my macro is not running can any one help me also the macro should run in all the sheets in that workbook.


Sub Cnegative()
'
' Cnegative Macro
'
For Each cell In Range("e3:e5000")
If cell.Value > 0 Then
cell.Value = cell.Value * -1
End If
Next cell
End Sub
 
I used below code still its running on single sheet

[Sub TempLoopforall()
On Error Resume Next
For Each cell In Union(Range("C3:C250000"), Range("E3:E250000"), Range("Q3:Q250000"), Range("S3:s250000")).SpecialCells(xlCellTypeConstants, xlNumbers)
If cell.Value > 0 Then cell.Value = -cell.Value
Next cell
End Sub][/CODE]
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You did not apply the method to loop sheets provided in post 7 :confused:
Try
Code:
Sub Test4()
    Dim ws As Worksheet, cell As Range, rng As Range
    For Each ws In ThisWorkbook.Worksheets
        On Error Resume Next
        Set rng = Union(ws.Range("C3:C250000"), ws.Range("E3:E250000"), ws.Range("Q3:Q250000"), ws.Range("S3:s250000")).SpecialCells(xlCellTypeConstants, xlNumbers)
            For Each cell In rng
                If cell.Value > 0 Then cell.Value = -cell.Value
            Next cell
        On Error GoTo 0
        Set rng = Nothing
    Next ws
End Sub
 
Upvote 0
For Each cell In Range("C3:C250000,E3:E250000,Q3:Q250000,S3:s250000").SpecialCells(xlCellTypeConstants, xlNumbers)
If Icell.Value > 0 Then cell.Value = -cell.Value
Next cell
If you have anything like that number of rows and many of the relevant cells are numeric & several worksheets, you might find this code considerably faster.

Test in a copy of your workbook.
Code:
Sub ConvertToNegative()
  Dim a As Variant, aCol As Variant
  Dim i As Long
  Dim ws As Worksheet

  Const sCols As String = "C E Q S"
  
  For Each ws In Worksheets
    For Each aCol In Split(sCols)
      a = ws.Range(aCol & 3, ws.Cells(ws.Rows.Count, aCol).End(xlUp)).Value
      For i = 1 To UBound(a)
        If IsNumeric(a(i, 1)) Then
          If a(i, 1) > 0 Then a(i, 1) = -a(i, 1)
        End If
      Next i
      ws.Range(aCol & 3).Resize(UBound(a)).Value = a
    Next aCol
  Next ws
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top