Code to format Numbers

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,601
Office Version
  1. 2021
Platform
  1. Windows
I am looking for VBA code the will convert numbers that appear with a minus at the end of the number for eg 2,384.34- to be be converted to a negative in front of the number for eg (2,384.34)

See Example below

Your assistance will be most appreciated

Volvo Parts Statement Template.xls
KLMN
24,384.2197
32,384.34-97
41,457.3825-
531.425-
62,140.0925-
76,450.8425-
82,228.0825-
9846.4525-
10164.7925-
1177.5225-
1212,323.7425-
Sheet1
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Assuming the data you want to ammend is on cell k2,paste this code on L2 and drag down through the lenght of your data.:

=VALUE(RIGHT(k2,1)&LEFT(k2,FIND("-",k2)-1))

Regards

'Rotimi
 
Upvote 0
try this
Code:
Sub Test()
Dim LR As Long
LR = Range("a" & Rows.Count).End(xlUp).Row
With Range("a1:a" & LR)
    .NumberFormat = "#,##0.00;#,##0.00"
End With
End Sub
 
Upvote 0
Hi Rotimi & Yahya thanks for the help.

I have adpated Rotimi'a formula as follows:

=IFERROR(VALUE(RIGHT(K2,1)&LEFT(K2,FIND("-",K2)-1)),VALUE(K2))

I would like VBA code that will copy this formula into M2 and then copy this down in line with the last cell containing data in Col K

Once the formula chas been copied into in col M and must then be cut and paste using Paste Special Values into K2 onwards

Your assistance in this regard will be most appreciated


Regards

Howard
 
Upvote 0
OK.

Run the code below to resolve your issue:
=======================================================
Application.ScreenUpdating = False
Range("M2").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(VALUE(RIGHT(RC[-2],1)&LEFT(RC[-2],FIND(""-"",RC[-2])-1)),VALUE(RC[-2]))"
Range("M2").Select
Selection.AutoFill Destination:=Range("M2:M50000"), Type:=xlFillDefault
Range("M2:M5000").Select
Range("M2").Select
Columns("K:K").Select
Selection.Copy
Columns("O:O").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("O:O").Select
Selection.TextToColumns Destination:=Range("O1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(0, 1), TrailingMinusNumbers:=True
Range("J1").Select
Columns("o:o").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Columns("o:o").Select
Selection.Delete
Range("M2").Select
Columns("M:M").Select
Selection.NumberFormat = "0"
Columns("M:M").EntireColumn.AutoFit
Columns("M:M").EntireColumn.AutoFit
Selection.NumberFormat = "#,##0.00000"
Columns("K:K").Select
Columns("M:M").ColumnWidth = 10.86
Columns("M:M").EntireColumn.AutoFit
Range("K1").Select
Application.ScreenUpdating = True

===================================================


Regards

Rotimi
 
Upvote 0
Howard, if your objective is just to move the trailing minus numbers in column K you should be able to use the code below rather than doing all the copy/pasting depending on your version of excel

Code:
Sub Macro3()
 Columns("K:K").TextToColumns Destination:=Range("K:K"), TrailingMinusNumbers:=True
Selection.NumberFormat = "#,##0"
End Sub
 
Upvote 0
Howard, if your objective is just to move the trailing minus numbers in column K you should be able to use the code below rather than doing all the copy/pasting depending on your version of excel

Code:
Sub Macro3()
 Columns("K:K").TextToColumns Destination:=Range("K:K"), TrailingMinusNumbers:=True
Selection.NumberFormat = "#,##0"
End Sub


Mark,

For the trailing minus all he needed was this:

=IFERROR(VALUE(RIGHT(K2,1)&LEFT(K2,FIND("-",K2)-1)),VALUE(K2))


He however wanted to some other work on the data.Please read the thread completely for a full understanding.

Cheers!

Rotimi
 
Upvote 0
Hi Guys

Thanks for the input, this is much appreciated

Regards

Howard
 
Upvote 0
Try Highlighting a column of numbers
Click Data - Text To Columns
Deliminated - Next
Next
On the 3rd screen, click Advanced, Check "Trailing minus for negative numbers"
Finish

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,225
Members
452,896
Latest member
IGT

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