基于多个范围值创建字符串时,Excel Delimiter更改为系统默认值

问题描述

这只是一个简单的用例,但是却困扰着我很多时间!

A1: 2046.40
B1: 504.30

当我使用以下方法从表中追加多个值时 Worksheet(sheetname).Range(“ C1”)= Worksheet(sheetname).Range(“ A1”)&“,”&Worksheet(sheetname).Range(“ B1”)

我根据系统设置=“获得结果分度符。 结果:2046.40,504.30(字符串)

当使用其他语言的用户打开并运行此功能时,即使在表中使用“。”显示A1和B1,分隔符也会在concat_value(C1)中更改。作为分隔符。

产生其他语言 2046,40,504,30(string)

我已经尝试了以下代码

Worksheets(sheetname).Range("A1").NumberFormat = "@"

Application.DecimalSeparator = "."
Application.UseSystemSeparators = False

有人可以启发我吗?

此致

M

解决方法

十进制分隔符问题

  • 以下内容对我有用(我用逗号作为小数点分隔符)。

代码

Option Explicit

Sub DecimalSeparatorIssue()
    
    Const sheetname As String = "Sheet1"
    Dim sep As String
    sep = Application.DecimalSeparator
    Dim wb As Workbook
    Set wb = ThisWorkbook ' The workbook containing this code.
    
    Dim ws As Worksheet
    Set ws = wb.Worksheets(sheetname)
    
    If sep = "." Then
        ws.Range("C1") = CStr(ws.Range("A1")) & "," & CStr(ws.Range("B1"))
    Else
        ws.Range("C1") = Replace(CStr(ws.Range("A1")),sep,".") & "," _
                       & Replace(CStr(ws.Range("B1")),".")
    End If

End Sub

此外,您还可以使用Format函数进行改进,例如:

Sub DecimalSeparatorIssue()
    
    Const sheetname As String = "Sheet1"
    Dim sep As String
    sep = Application.DecimalSeparator
    Dim wb As Workbook
    Set wb = ThisWorkbook ' The workbook containing this code.
    
    Dim ws As Worksheet
    Set ws = wb.Worksheets(sheetname)
    
    If sep = "." Then
        ws.Range("C1") = Format(ws.Range("A1").Value,"0.00") & "," _
                       & Format(ws.Range("B1").Value,"0.00")
    Else
        ws.Range("C1") = Replace(Format(ws.Range("A1").Value,"0.00"),".") _
                       & "," _
                       & Replace(Format(ws.Range("B1").Value,".")
    End If

End Sub