Separatore Ultimate 1000 utilizzando VBA
Ho provato a ottenere la soluzione VBA per il separatore 1000 poiché nel mio caso non è possibile utilizzare la formula e dovrebbe essere fatto con codice personalizzato. La soluzione corrente è presa dalla risposta Formato numero con separatore di migliaia e decimale se necessario
Ecco il codice:
Function CustomFormat(InputValue As Double) As String
CustomFormat = Format(InputValue, "# ###")
If (Right(CustomFormat, 1) = ".") Then
CustomFormat = Left(CustomFormat, Len(CustomFormat) - 1)
End If
End Function
Funziona per numeri come 1000, ma non funziona per 1000000. Anche 1000000000 non funzionerà. Attualmente sto lavorando alla soluzione, ma se qualcuno ha qualcosa da condividere, sarebbe apprezzato.
In caso di utilizzo della soluzione originale:
Function CustomFormat(InputValue As Double) As String
CustomFormat = Format(InputValue, "#,###.##")
If (Right(CustomFormat, 1) = ".") Then
CustomFormat = Left(CustomFormat, Len(CustomFormat) - 1)
End If
End Function

Risposte
Penso che vba abbia bisogno del separatore delle migliaia definito nelle impostazioni regionali. Dato che nel tuo caso è una virgola puoi fare qualcosa del genere
Function CustomFormat(InputValue As Double) As String
CustomFormat = Format(InputValue, "#,###")
If (Right(CustomFormat, 1) = ".") Then
CustomFormat = Left(CustomFormat, Len(CustomFormat) - 1)
End If
CustomFormat = Replace(CustomFormat, ",", " ")
End Function
Un altro approccio consiste nel leggere il separatore dal registro. Questo dovrebbe funzionare in diverse impostazioni regionali.
Function CustomFormat(InputValue As Double) As String
Dim sThousandsSep As String
Dim sDecimalSep As String
Dim sFormat As String
sThousandsSep = Application.International(xlThousandsSeparator)
sDecimalSep = Application.International(xlDecimalSeparator)
' Up to 6 decimal places
sFormat = "#" & sThousandsSep & "###" & sDecimalSep & "######"
CustomFormat = Format(InputValue, sFormat)
If (Right$(CustomFormat, 1) = sDecimalSep) Then CustomFormat = Left$(CustomFormat, Len(CustomFormat) - 1)
End If
' Replace the thousands separator with a space
' or any other character
CustomFormat = Replace(CustomFormat, sThousandsSep, " ")
End Function
Modifica Funzione modificata da utilizzare Application.International
come suggerito da @RonRosenfeld.
È necessario estendere il formato del numero per includere i numeri più grandi.
### ### ## 0 ","
