我有一个工作表,其中有许多格式为货币的单元格,我想通过组合框修改货币格式。
首先,我使用此代码来获取初始货币类型/格式。
Private Sub ComboBox1_DropButtonClick()
inicial = Me.ComboBox1.Value
Select Case inicial
Case "EUR"
oldFormat = "#.##0 €"
Case "GBP"
oldFormat = "[$£-809]#.##0"
Case "USD"
oldFormat = "#.##0 [$USD]"
End Select
End Suboldformat变量是全局变量,
Public oldformat As String之后,我想使用oldformat变量进行find,并使用newformat变量进行替换,
Private Sub ComboBox1_Change()
Dim ws As Worksheet
Dim newFormat As String
'On Error Resume Next
newValue = Me.ComboBox1.Value
Select Case newValue
Case "EUR"
newFormat = "#.##0 €"
Case "GBP"
newFormat = "[$£-809]#.##0"
Case "USD"
newFormat = "#.##0 [$USD]"
End Select
'Set rNextCell = Application.FindFormat
For Each ws In ActiveWorkbook.Worksheets
Application.FindFormat.Clear
Application.FindFormat.NumberFormat = oldFormat
Application.ReplaceFormat.Clear
Application.ReplaceFormat.NumberFormat = newFormat
ws.Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
Next ws
End Sub我通过组合框上的用户选择来读取新值。
但这根本不起作用,变量oldformat和newformat接收正确的值,但我得到了一个错误,
Application.FindFormat.NumberFormat = oldformat
Application.ReplaceFormat.NumberFormat = newFormat运行时错误'1004':应用程序定义或对象定义的错误
有没有办法将newformat和oldformat值传递给Numberformat属性?
还是有人另有打算做这件事?
示例文件的链接,https://www.dropbox.com/s/sdyfbddxy08pvlc/Change_Currency.xlsm
我非常感谢大家的帮助,我对VBA还是个新手。
英语不是我的母语,如果有任何错误我深表歉意。
发布于 2014-08-13 23:44:26
我已经下载了文件。然后将代码更改为:
Public oldFormat As String
Private Sub ComboBox1_Change()
Dim ws As Worksheet
Dim newFormat As String
'On Error Resume Next
newValue = Me.ComboBox1.Value
Select Case newValue
Case "EUR"
newFormat = "#,##0 $"
Case "GBP"
newFormat = "[$£-809]#,##0"
Case "USD"
newFormat = "#,##0 [$USD]"
End Select
'Set rNextCell = Application.FindFormat
For Each ws In ActiveWorkbook.Worksheets
ws.Range("XFD1048576").NumberFormat = oldFormat
ws.Range("XFD1048576").NumberFormat = newFormat
ws.Range("XFD1048576").NumberFormat = "General"
Application.FindFormat.Clear
Application.FindFormat.NumberFormat = oldFormat
Application.ReplaceFormat.Clear
Application.ReplaceFormat.NumberFormat = newFormat
ws.Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
Next ws
End Sub
Private Sub ComboBox1_DropButtonClick()
inicial = Me.ComboBox1.Value
Select Case inicial
Case "EUR"
oldFormat = "#,##0 $"
Case "GBP"
oldFormat = "[$£-809]#,##0"
Case "USD"
oldFormat = "#,##0 [$USD]"
End Select
End Sub现在,它运行时没有错误。
但有一个问题仍然存在。如果用户定义的格式等于默认货币格式,则Excel (Excel!不是VBA)将不设置用户定义的格式,而是设置默认货币格式。这是用于德国的VBA "#,##0 $“。对我来说,#.##0欧元就是这种情况,它是德国的一种货币格式。所以对我来说,在VBA中是EUR "#,##0 $“。
问候
Axel
https://stackoverflow.com/questions/25286636
复制相似问题