Macro to Convert all the data in column in Negative Value

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
101
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
 

arpd123

New Member
Joined
May 14, 2018
Messages
10
You can add to your if statement the following to get it to ignore non-numerical data like text:
Code:
If cell.Value > 0 And IsNumeric(cell.Value) Then
To get it to run through each sheet in the workbook, you can enclose your code in another for loop like so:

Code:
For i = 1 to ActiveWorkbook.Sheets.Count
    For Each cell in ActiveWorkboook.Sheets(i).Range("E3:E5000")
.
.
.
    Next cell
Next i
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,686
Office Version
365
Platform
Windows
Perhaps
Code:
 If cell.Value < 999999999 And cell.Value > 0 Then
or
Code:
If IsNumeric(cell.Value) And cell.Value > 0 Then
 
Last edited:

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,686
Office Version
365
Platform
Windows
or using SpecialCells property
Code:
Sub Cnegative3()
    On Error Resume Next
    For Each cell In Range("e3:e5000").SpecialCells(xlCellTypeConstants, xlNumbers)
        If Icell.Value > 0 Then cell.Value = -cell.Value
    Next cell
End Sub
 
Last edited:

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,686
Office Version
365
Platform
Windows
perhaps simplest of all...

Code:
Sub Cnegative()
    For Each cell In Range("e3:e5000")
    On Error Resume Next
       If cell.Value > 0 Then cell.Value = -cell.Value
    Next cell
    On Error GoTo 0
End Sub
 

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
101
hi its working fine thanks for your help ...however how to loop this for all sheets in this workbook.
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,686
Office Version
365
Platform
Windows
Like this:
Code:
Sub Cnegative()
Dim ws As Worksheet, cell as Range
For Each ws In ThisWorkbook.Worksheets
    For Each cell In ws.Range("e3:e5000")
        On Error Resume Next
           If cell.Value > 0 Then cell.Value = -cell.Value
        Next cell
    On Error GoTo 0
Next ws
End Sub
 
Last edited:

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
101
HI Cold you please help me looping below macro in all the worksheet ... am confused looping the same

Sub Loopforall()

On Error Resume Next
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

Range("G26658,C:C,E:E,Q:Q,S:S").Select
Range("S26636").Activate
Selection.NumberFormat = "0"
Selection.NumberFormat = "0.00"
Range("A1").Activate
End Sub
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,686
Office Version
365
Platform
Windows
Using code tags around your code mnakes it easier to read (to do this next time: click on # icon above post and paste your code inside the code tags)

Try this
Code:
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
 
Last edited:

Forum statistics

Threads
1,082,281
Messages
5,364,219
Members
400,787
Latest member
bs04c

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top