Reputation: 291
Got a tiny problem, but I can't get it right. Perhaps someone could help me out. Much appriciated.
my Regional Settings for Decimal Separator is "." (Dot).
i have an excel file, which contains decimals places in some columns. while updating the data in excel template i turned off the usersystem setting then applied DOT as the separator. While converting it to text file it is showing the comma "," as the decimal separator.
I am turning of the the user settings by using below code
With Application
StrDecimal = .DecimalSeparator
StrThousand = .ThousandsSeparator
.DecimalSeparator = "."
.ThousandsSeparator = "'"
.UseSystemSeparators = False
End With
Need to check if the Decimal Separator is not eqaul to "." then we need to force to use "." aS Decimal separator.
Please help how to achieve this by using VBA or by using Datavalidation
Thank you very much in advance!
Upvotes: 0
Views: 11403
Reputation: 4518
So you've got your data and are writing it to a textfile. Assuming your data is in an array, I'd loop through the array and convert the data after converting it to a string, like so.
Sub ChangeDecimalSeperator()
Dim vData As Variant, ii As Integer, jj As Integer
vData = ActiveSheet.Range("A1:B2")
For ii = LBound(vData, 1) To UBound(vData, 1)
For jj = LBound(vData) To UBound(vData, 2)
'Only convert if it's a number
If VBA.IsNumeric(vData(ii, jj)) Then
'You can also specify the format using VBA.Format$() here, ie force the decimal places
vData(ii, jj) = ReplaceCharacter(VBA.CStr(vData(ii, jj)), ".", ",")
End If
Next jj
Next ii
'Now output vData to your textfile
End Sub
Function ReplaceCharacter(sForString As String, sReplacingCharacter As String, sWithCharacter As String) As String
Dim sStringPrevious As String
'While there is a character that we still need replacing left, keep going. If we can't find one, we're done
Do
sStringPrevious = sForString
sForString = VBA.Replace(sForString, sReplacingCharacter, sWithCharacter)
Loop Until sStringPrevious = sForString
ReplaceCharacter = sForString
End Function
Upvotes: 1