简体   繁体   English

从VBA格式获取货币

[英]Get Currency from format VBA

we are facing a little problem right now. 我们现在面临一个小问题。 We would like to get the currency from a cell and write it in the cell to the right. 我们想从一个单元格中获取货币并将其写在右侧的单元格中。 The code we got so far is the following: 到目前为止,我们得到的代码如下:

Public Sub GETWährung()
Dim i As Integer
For i = 6 To 9
Dim w As String
w = Right(Cells(i, "I"), 3)
Cells(i, "I").Offset(0, 1).Value = w
Next i
End Sub

The problem is, that the currency isn't included in the cell, it's a custom format. 问题在于,货币不包含在单元格中,而是一种自定义格式。 For example: 例如: 在此处输入图片说明 On the left you can see the formated cell, on the right the output is shown. 在左侧您可以看到格式化的单元格,在右侧则显示输出。 We appreciate any help. 感谢您的帮助。 Thank you a lot. 非常感谢。

In your code, w will get the last three characters of the value . 在您的代码中, w将获取value的最后三个字符。 The currency code is not part of the value but of the representation . 货币代码不是值的一部分,而是表示的一部分 If you want to get the text that is displayed in a cell, you can use something like cells(i, "I").text - however, I will not recommend to do so. 如果要获取显示在单元格中的文本,则可以使用诸如cells(i, "I").text -但是,我不建议这样做。

If you just want to have the same format, you can use 如果您只想使用相同的格式,则可以使用

For i = 6 To 9
    Cells(i, "I").Offset(0, 1).NumberFormat = Cells(i, "I").NumberFormat
Next i

Try to change the format of those cells, add this line (the format is just an example): 尝试更改这些单元格的格式,添加以下行(格式仅是示例):

Cells(i, "I").Offset(0, 1).NumberFormat = "$#,##0.00"

Or outside of the loop: 或循环外:

Range("E:F").NumberFormat = "$#,##0.00"

实现此目的的另一种方法是使用样式方法,该方法将根据您的“区域设置”将单元格设置为“货币”,如下所示:

Cells(i, "I").Offset(0, 1).Style = "Currency"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM