简体   繁体   中英

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 . 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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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