我有一个工作表,其中许多单元格形成货币,我想通过组合框修改货币格式,
首先,我使用此代码获取实体货币类型/格式,
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
oldformat变量是一个全局变量
Public oldformat As String
之后我想使用oldformat变量进行查找,并使用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有点新意。
如果英语中有任何错误,我道歉,这不是我的自然语言。
答案 0 :(得分:0)
我已经下载了该文件。然后将代码更改为:
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€的情况,这是德国货币格式之一。所以对我来说是EUR"#,## 0 $"在VBA。
问候
阿克塞尔