格式为百分比10或在vba中有很多小数

我有一个棘手的问题,我不明白发生了什么,为什么。

我有一个具有很多百分比值的用户窗体。

在这里输入图像说明

这些值来自Excel表格,他们看起来像这样。 用户现在可以更改这些值并将其放回到该Excel表单中。 用户的活动也被保存在日志表中。 所有值都保存在那里。 我把这样的价值放在这里:

Private Sub saveV_Click() Dim wsLog As Worksheet Dim i As Long Dim j As Long Dim lastRow As Long Dim tbNr As String j = 3 i = 2 Set wsLog = ThisWorkbook.Worksheets("Log") With wsLog lastRow = findLastRow(wsLog) lastColumn = findLastColumn(wsLog) For Each tb In Me.Controls If TypeName(tb) = "TextBox" Then Select Case i 'case 2 and 3 values are fixed and not of any interest Case 2 .Cells(lastRow + 1, 1).Value = tb.Value Case 3 .Cells(lastRow + 1, 2).Value = tb.Value 'the following cases refer to the textboxes you can see in the other picture (there are some more) Case 4 To 46 .Cells(lastRow + 1, j).Value = tb.Value Case 47 To 89 .Cells(lastRow + 2, j).Value = Format(tb.Value, "0.00%") Case 90 To 132 .Cells(lastRow + 3, j).Value = Format(tb.Value, "0.00%") End Select i = i + 1 j = j + 1 If j > lastColumn Then j = 3 End If Next End With Unload Me End Sub 

代码本身运行良好,但它没有正确的格式化值。

如果我使用Format(tb.Value, "0.00%")我的数值将变为四舍五入,因此101,49316739%变为101,49%

我试图添加更多的零,但如果我使用Format(tb.Value, "0.000%")我的价值获得乘以1000所以它变成101493% 。 (甚至更多的零)

如何用十(或更多)小数保存我的值?

Format函数将一个值转换成一个String 。 这意味着你在这里编码…

 .Cells(lastRow + 2, j).Value = Format(tb.Value, "0.00%") 

…将采用Double101,49316739并将其转换为“101,49%”的String值。 .Cells(lastRow + 2, j).Value的数据types是一个Variant ,但是你给它一个Stringtypes的Variant 。 然后,Excel必须确定您正在编写的值是否可以实际表示为一个数字。 请记住, Format用于显示数字的可读版本 – 不用于存储值

只要让Excel做格式化:

 .Cells(lastRow + 2, j).Value = CDbl(tb.Value) \ 100 'Cast from string to double and scale. .Cells(lastRow + 2, j).NumberFormat = "0.000000000000000%" 'Format the cell. 

如果你使用format()函数将你的值转换成一个string,这个string被excel解释为一个格式。 只需使用.Cells().Value = tb.Value.Cells().NumberFormat = "0.0000%"即可获得您想要的结果。