Excelフォーム内のテキストボックスに数値を表示したい。レポート、ただし、小数点が存在する場合にのみ表示し、小数点以下2桁のみを表示したい.
たとえば、数値が 12 の場合、12 を表示したい
数値が 12.1 の場合、12.10 を表示したい
数値が 12.126 の場合、12.13 を表示したい
現時点では、以下のコードがあり、小数点が表示されていません。
Me.Amount.Value = Format(Me.Amount, "#,###")
2 つのフォーマット文字列のいずれかを条件付きで返す関数を作成できます。
Function GetFormatString(varValue As Variant) As String
Dim dblValue As Double
If IsNumeric(varValue) Then
dblValue = CDbl(varValue)
If dblValue = Int(dblValue) Then
GetFormatString = "#,###"
Else
GetFormatString = "#,###.00"
End If
End If
End Function
Private Sub Amount_AfterUpdate()
Dim strFormat As String
strFormat = GetFormatString(Me.Amount.Value)
Me.Amount.Value = Format(Me.Amount.Value, strFormat)
End Sub