使用 VB 6.0 将 Excel 单元格格式化为小数点后两位

使用 VB 6.0 将 Excel 单元格格式化为小数点后两位

使用 vb 6.0 创建单元格时,如果数字不是以 1-9 结尾,则无法强制将金额显示为小数点后 2 位。例如,59.12 可以正确显示,但如果我们尝试 59.10,单元格中会显示 59.1。尝试过多种不同的格式和文本输出,但均未成功。

为了回答我们所尝试的问题,下面是我们尝试过的部分代码,其中最引人注目的是:

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=================================================

答案1

对于电子表格:单元格中显示的内容和实际存储的内容可能有很大差异。

示例:日期和时间值为每天 1.0,小时、分钟和秒以小数形式显示,因此 42205.5 等于“2015-07-20 12:00”。

要将“59.1”(或任何可以四舍五入为小数点后两位的数字)显示为“59.10”,您需要将显示格式设置为“0.00”或类似的格式。

按住并CTRL点击1;在对话框中,找到“数字”,单击它并将“小数位数”更改为“2”。现在单击OK进行设置并离开对话框。

另一种方法是在同一个对话框中使用“自定义”(或 LibreOffice 的“用户定义”)并输入“0.00”作为“格式代码” - 最终结果相同。

那么如何通过 VBA 来实现呢?我将把这个任务留给您。

相关内容