0

我正在尝试将 Variant/String 的二维数组粘贴到一个范围中。那没问题。此 Variant/String 列可以包含字符串或双精度值。

除了双精度值包含 3 个或更多十进制值的单元格外,一切正常。逗号 (,) 是小数点分隔符,点 (.) 是我的 Excel 的千位分隔符。

例如:

当数组中出现 2,3 时,它将 2,3 粘贴到 Excel 单元格中

当数组中出现 2,34 时,它将 2,34 粘贴到 Excel 单元格中

当数组中出现 2,321 时,它将 2321 粘贴到 Excel cell.value 中,并在 cell.text 中显示 2.321

当数组中出现 2,3215 时,它将 23215 粘贴到 Excel cell.value 中,并在 cell.text 中显示 23.215

代码:

Dim DataArray(2, 2) As Variant 
...
... code that fills DataArray
...
Range("A1").Resize(UBound(DataArray, 1) + 1,UBound(DataArray, 2) + 1).Offset(1, 0) = DataArray
4

2 回答 2

2

我有一个类似的问题。这只是 Excel 误解变体结束类型的经典案例。不幸的是,用于填充您的范围的简洁矢量公式必须取消。相反,您可以使用这样的东西(i 和 j 指定变量范围的左上角单元格):

Dim cell As Range
Dim x As String
Dim i as integer, j as integer
'The first cell is "A1"
i=1
j=1

For Each cell In Range(Cells(i,j),Cells(i+UBound(DataArray, 1),j+Ubound(DataArray)))
  x = DataArray(cell.Row - i, cell.Column - j)
  cell.Value = x
  'Next line is optional
  If IsNumeric(x) Then cell.NumberFormat = "General"
Next cell

经过测试,它可以工作。如果您省略可选行,Excel 会猜测格式,但它大多是正确的。如果您知道您想要的数字格式,那么一定要在可选行中指定它。如果要指定小数位数,可以尝试其他格式,例如“###,##0.00”。

希望这有帮助。

于 2013-07-25T03:06:14.370 回答
0

适用于下一个修改:

Dim cell As Range
Dim x As String
Dim D as Double
Dim i as integer, j as integer
'The first cell is "A1"
i=1
j=1

For Each cell In Range(Cells(i,j),Cells(i+UBound(DataArray, 1),j+Ubound(DataArray)))
  x = DataArray(cell.Row - i, cell.Column - j)
  If IsNumeric(x) Then 
      D = CDbl(x)
      cell.Value = D
  Else
      cell.value = x
  end if
Next cell

太感谢了!!!:)

于 2013-07-25T07:20:39.330 回答