Format an Excel cell from VB 6.0 with an amount to two decimal places

0

When using vb 6.0 to make a cell, we cannot force the amount to be shown with 2 places after the decimal if the number does not have end in 1-9. For example 59.12 shows up correctly but if we try 59.10 we get 59.1 in the cell. Have tried many different formats and text outputs but with no success.

In answer to what we have tried below is a portion of the code we have been trying with the most remarked out:

StartForm:


ActiveWorkbook.Styles.Add(Name:="NelcoAmt").NumberFormat = "#.00"

'ActiveWorkbook.Styles("Number").NumberFormat = "######.00"
'ActiveWorkbook.Styles("Number").Application.FixedDecimalPlaces = 2
'ActiveWorkbook.Styles("Currency").Application.Caption = ""

'ActiveWorkbook.Styles("Currency").IncludePatterns = False

'ActiveWorkbook.Styles("Currency").Application. = ""

'ActiveWorkbook.Styles("Currency").Application.

'xlSheet.Cells(1, 34).NumberFormat = "@"

'xlSheet.Cells(2, 34).NumberFormat = "#.00"

'xlSheet.Cells(2, 34).Format = "#.00"

ActiveWorkbook.Styles("NelcoAmt").Application.FixedDecimalPlaces = 2
xlSheet.Cells(2, 34).Style = "NelcoAmt":  '.NumberFormat = "#.00": 'Number

'For i% = 1 To ActiveWorkbook.Styles.Count


'Text$ = ActiveWorkbook.Styles(i%).Name

'xlSheet.Cells(i%, 1).Style = Text$
'xlSheet.Cells(i%, 1).Value = 59.4

'xlSheet.Cells(i%, 2).Value = Text$

'Next i%



Rem-----9/15/14---set header for variables
xlSheet.Cells(1, 1).Value = "UniqueFormKey"
xlSheet.Cells(1, 2).Value = "FormName"
xlSheet.Cells(1, 3).Value = "OR_EFIN"
xlSheet.Cells(1, 4).Value = "OR_Type"










Rem======================================start of test section
Dim NumAns As Double

'7/16/15
    'NumAns = Format$(Format$(Val(Frm941Inp.Scr941(19)), "########.00"), "@@@@@@@@@@@")
    txtOut = Format$(Format$(Val(Frm941Inp.Scr941(19)), "########.00"), "@@@@@@@@@@@")

    'NumAns = FormatNumber((Val(Frm941Inp.Scr941(19))), 2)

    NumAns = Val(Frm941Inp.Scr941(19))


    If Val(Frm941Inp.Scr941(18)) = 0 Then txtOut = "0.00"
    txtOut = Frm941Inp.Scr941(19)
'xlSheet.Cells(2, 34).NumberFormat = "@": ' = txtOut: '"########.00"]
'xlSheet.Cells(2, 34) =  (FormatNumber(Val(Frm941Inp.Scr941(19)), 2), "text"): 'txtOut ', "########.00"): '5d 2 tax on add medi wage
'xlSheet.Cells(2, 34) = "'" & FormatNumber(Val(Frm941Inp.Scr941(19)), 2): 'txtOut ', "########.00"): '5d 2 tax on add medi wage
'txtOut = "'" & txtOut

xlSheet.Cells(2, 34).Style.NumberFormat = "#.00": 'Number
xlSheet.Cells(2, 34) = Format(NumAns, "#.00"): 'txtOut: 'Val(Frm941Inp.Scr941(19)):  'txtOut ', "########.00"): '5d 2 tax on add medi wage

'xlSheet.Cells(2, 34).Style.NumberFormat = "#.00": 'Number

'xlSheet.Cells(2, 34).NumberFormat = "#.00"

'xlSheet.Cells(2, 34).Value = Val(Frm941Inp.Scr941(19))

'xlSheet.Cells(2, 34).value = FormatNumber(Val(Frm941Inp.Scr941(19)), 2): '"#######.00")
'xlSheet.Cells(2, 34).Value = Format$(Format$(Val(Frm941Inp.Scr941(19)), "########.00"), "@@@@@@@@@@@")

'xlSheet.Cells(2, 34).Value = Val(Frm941Inp.Scr941(19)): '5d 2 tax on add medi wage
Rem===================================end of test section=================================================

HPrussack

Posted 2015-07-20T16:07:06.977

Reputation: 1

Change the decimal places displayed for numbers – DavidPostill – 2015-07-20T16:37:33.777

Answers

0

For spreadsheets: What is displayed in the cell, and what is actually stored may differ quite much.

Example: Date and time values are 1.0 per day, with hours minutes and seconds present in the decimals so that 42205.5 equals "2015-07-20 12:00".

To have "59.1" (or anything else that may be rounded to that in two decimals) display as "59.10" you need to set the display format to "0.00" or similar.

Press and hold CTRL then hit 1; In the dialogue, find "Number", click on it and change "Decimal places" to read "2". Now click OK to set it and leave the dialogue.

An alternative is using "Custom" (or "User defined" for LibreOffice) in the same dialogue and type in "0.00" as "Format code" - with the same end result.

Exactly how to do it from VBA? I'll leave that as a task for you.

Hannu

Posted 2015-07-20T16:07:06.977

Reputation: 4 950

Thanks, I know how to do it in excel the problem to control it from VB – HPrussack – 2015-07-21T16:56:47.043