how to remove parentheses for negative number in excel - vba

I am trying to remove parentheses for negative numbers in excel, but I have so far been unable to remove them successfully
I tried below code:
sheet1.UsedRange.Select
sheet1.Range("A:XFD").NumberFormat = "0.00"
objExcel.ActiveWorkbook.PrecisionAsDisplayed = True
but it only works after doing TextToColumns manually. I need a more general solution, as I don't always know which rows have numbers.

That has to do with the format of the cell. Right-Click cell > Format > (Select Number or Currency) change how you want the negative number to look. ... or if you want to do it in VBA, this will change the negative values to red:
Range("A1").NumberFormat = "$#,##0.00;[Red]$#,##0.00"
This will change it back to ():
Range("A1").Style = "Currency"
or this:
Range("A1").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"

Related

Vba Numberformat muliple condtions to format cell value

Dear all i want to format my cell based on the Cell value. There are 3 possible Conditions:
Cell is larger than 0.05 -> the Value should stay the same but be fromated to 0.0
Cell is smaller than 0.05 -> the Value should be replaced with a String "a.C."
Cell is Zero -> the Value should be replaced with a Dash"
I found thisSolution for the dashes and could combine it with the number formating
.NumberFormat = "0.0;[=0]---"
This works. but if i add an additonal argument the vba code breaks.
.NumberFormat = "0.0;[=0]---;[<0.05]a.C."
Does not work. Would you mind telling me what i am doing wrong?
Thank you in advance
Edit:
FOr what ever reason this Order seems to work:
.NumberFormat = "[=0]---;[<0.05] ""a.C."";0.0 "

Convert string to double in word (vba)

I am trying to convert a text in a word document to be a double, so I can do currency formatting on it. I receive this text from a mail merge. How would I create a macro that can receive this text and return it as a number?
I'm unfamiliar with word, and VBA script. What I have made so far is
Function stringToDouble(baseString As String)
Dim num As Double
num = Val(baseString)
stringToDouble = num
End Function
I'm not sure how I would call this macro. Because it takes a parameter it does not show up in the macro table.
I may be completely off on how to convert text to a double in word, but any help is appreciated.
Thanks. Please comment for any clarifications.
You don't need a macro for this!!! All you need do is learn how to use formatting switches in Word fields.
To control number & currency formatting in Word, add a numeric picture switch to the mergefield. To do this:
select the mergefield;
press Shift-F9 to reveal the field coding. It should look something like {MERGEFIELD MyData};
edit the field so that you get {MERGEFIELD MyData # $,0.00} (or whatever other numeric format you prefer - see below);
position the cursor anywhere in this field and press F9 to update it.
Note 1: The '# $,0.00' in the field is referred to as a numeric picture switch. Other possibilities include:
# 0 for rounded whole numbers
# ,0 for rounded whole numbers with a thousands separator
# ,0.00 for numbers accurate to two decimal places, with a thousands separator
# $,0 for rounded whole dollars with a thousands separator
# "$,0.00;($,0.00);'-'" for currency, with brackets around negative numbers and a hyphen for 0 values
Note 2: The precision of the displayed value is controlled by the '0.00'. You can use anything from '0' to '0.000000000000000'.
If you use a final ';' in the formatting switch with nothing following, (eg # "$,0.00;($,0.00);") zero values will be suppressed. Note that this suppresses 0s resulting from empty fields and from fields containing 0s.
Note 3: If you use a decimal tab or right-aligned tab to align the values, wrap the switch in quotes (i.e. # "$,0.00") and insert a tab into the field code after the $ sign, you can have the values output with the decimal alignment occurring after the $ sign.
For more Mailmerge Tips & Tricks, see: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
If you want to convert the number to a Double data type then try this:
Function StringToDouble(ByVal baseString As String) As Double
StringToDouble = VBA.CDbl(baseString)
End Function
If you're only concerned about formatting currency, convert the string to the Currency data type like this:
Function StringToCurrency(ByVal baseString As String) As Currency
StringToCurrency = VBA.CCur(baseString)
End Function
You will still need to format the number but both functions give you a number that can be formatted.
Here's an example that also gives you a string formatted as USD (e.g. $4,999.75). It requires the StringToCurrency function above.
Sub test()
Dim stringNum As String
stringNum = "4,999.754501"
Debug.Print "stringNum=" & stringNum ' outputs 4,999.754501
Dim currencyNum As Currency
currencyNum = StringToCurrency(stringNum) ' outputs 4999.7545
Debug.Print "currencyNum=" & currencyNum
Dim formattedString As String
formattedString = Format$(currencyNum, "$#,##0.00")
Debug.Print "formattedString=" & formattedString ' outputs $4,999.75
End Sub

Concatenate string and number as number

I have a text made of some cells with concatenation. One of the items of the text is a number. How to make it a full number with all the necesarry comas etc.
I have: on stt 03/06 db corr PLN 60000000 val 03/06 pending
I need: on stt 03/06 db corr PLN 60,000,000.00 val 03/06 pending
I tried with CDbl(Sheets(1).Cells(i, 5).Value) but the number is still the same.
Please note that the source cell contains the number in correct format : 60,000,000.00 which is downloaded from DB2 database
Many thanks for any help how to achieve that
To concatenate a string and a number you just use &, then convert the result to a number with Val() function.
Then if you want to format the result number you can use Format() function. Then you get a formatted string.
Example:
j = Val("10" & 10)
s = Format(j, "##,###.00")
I think that the use Format Function, resolve your problem -> https://msdn.microsoft.com/en-us/library/office/gg251755.aspx
Maybe format(60000000,"##,##0.00")?
If your cell is already formatted appropriately, you can use its Text property:
Sheets(1).Cells(i, 5).Text
Note that because this is the displayed value, you can get #### returned if the column width is too narrow for the actual value, but it's still useful where you don't necessarily know the format ahead of time.

Highlight cells + adjacent 2 cells if certain text if located

If any cell contains the text "example", this cell + the two cells on the same row to the right of, to be highlighted.
So for instance
B5 contains "example", b5,c5,d5 need to be highlighted orange
b9 contains "example", b9,c9,d9 need to be highlighted orange.
And so forth across the whole sheet. Multiple rows and multiple columns could contain the specific text.
Any assistance, examples appreciated.
Private Sub CommandButton1_Click()
row_number = 4
Do
DoEvents
row_number = row_number + 1
swing_data = Sheet1.Range("B" & row_number)
If InStr(swing_data, "Test") >= 1 Then
With Range("B" & row_number).Offset(, 2).Interior
.Pattern = x1solid
.PatternColorIndex = x1automatic
.Color = 65535
.PatternTintAndShade = 0
End With
End If
Loop Until swing_data = ""
End Sub
This is not highlighting the 2 cells to the right, and if there is a blank cell it's stopping. Also it's only working on one column. Needs to work on columns B, E, N, Q, Z, AC,
Changed this line
<<code>With Range("B" & row_number).Offset(, 2).Interiorcode>
To read <code>With Range("B" & row_number).resize(, 3).Interiorcode>
And it works.
Would the be an easier way to include multiple columns in this...?
Conditional formatting:
As multiple columns may contain your criteria you need a bit more complex formula (conditional formatting - use a formula...): =or(iferror(rc[-1]="example",false),iferror(rc[-2]="example",false),rc="example") - notes: I switch to R1C1 reference style before entering conditional formatting as I find it mite clear there; iferror is necessary to make formula working also in first and second columns.
VBA:
you can use VBA find object to cycle through all instances and change formatting there (I think built-in help and maybe recording some short macros give you enough information to create it)
You can do this with conditional formatting if you want to avoid VBA. Select the cells you want you want to apply the formatting to (say B1:D10), Click conditional formatting -> New Rule... -> Use a formula to determine which cells to format. Use this formula
=EXACT($B1,"example")
The $ in front of the column makes sure only that column is looked at, the row will be independent. You then need to change the formatting to whatever you want. In your case change the fill to orange.

Differentiate between number and words with Convert to Number

I am running a vlookup in vba which returns some errors. Some are due to missing values (these should remain #N/A) some are due to the values not being converted to numbers (these need to be fixed). However when I run the below code it converts both numbers and letters into numeric values and returns #VALUE when it should do nothing. I need a way to differentiate between the numbers and any values with a letter in them. In the below example the code should loop through, skip TRD4 and convert 9501 to a number.
L26 BW
VR7F BW
TRD4 #N/A
TRD4 #N/A
9501 #N/A
XDTM BC
UDDP TE
For a = 2 To LCell2 Step 1
v = Cells(a, 9).Value
If IsError(v) Then
Cells(a, 8).Value = Evaluate("H" & a)
End If
Next a
It would also help if you included your full code such as what is v declared as, is Cells(a,9) your listed values such as L26 and what is the formula that is in H & a.
Running a quick check with the sample values you have provided you can used IsNumeric in your loop to test if a value is made of just numbers or also contains text. This will work even if the value is stored as text in the cell. To convert a text value to numeric you can use CInt or any number of conversion functions available.
So if you want to only evaluate if it's a number and ignore text you can do something similar to
For a = 2 to LCell2 Step 1
If IsNumeric(Cells(a,9)) then
Cells(a,8).Value = Evaluate("H" & a)
End If
Next a