Object VBA货币编号格式错误-Excel
我有一个工作表,其中有许多单元格格式为货币,我想通过组合框修改货币格式 首先,我使用这段代码获取初始货币类型/格式Object VBA货币编号格式错误-Excel,object,currency,number-formatting,Object,Currency,Number Formatting,我有一个工作表,其中有许多单元格格式为货币,我想通过组合框修改货币格式 首先,我使用这段代码获取初始货币类型/格式 Private Sub ComboBox1_DropButtonClick() inicial = Me.ComboBox1.Value Select Case inicial Case "EUR" oldFormat = "#.##0 €" Case "GBP" oldFormat = "[$£-809]#.##0" Case "USD" oldFormat =
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
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变量替换
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
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属性
还是有人让另一个人去做这件事
链接示例文件,
我很感激任何帮助,我对VBA有点陌生
如果英语中有任何错误,我深表歉意,这不是我的母语。我已经下载了该文件。然后将代码更改为:
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中的欧元是“#,##0$”
问候
Axel我已经在“自定义格式”框中设置了格式,但仍然得到错误。您可以在示例文件中看到。在您的系统中设置了什么十进制分隔符和数字组分隔符?VBA将永远使用英文格式代码,因此,如果十进制分隔符为逗号,数字组分隔符为点,则“#.##0,00”将无法正常工作。如果结果为1.000,00,则必须使用“#,###.00”。这里是我的区域设置和VBA上的错误,我将格式更改为“#,##.0€”[$809]#,##.0”和“#,###.0[$USD]”。我将它们添加到自定义格式框中,不,您不应该更改自定义格式框中的格式代码。你应该使用VBA代码中的英文代码。我稍后会用托盘,然后我会说些什么。谢谢你的帮助。很抱歉耽搁了,我在度假。我在我的文件中使用了你的代码,但什么都没有发生。。。没有错误,没有结果,什么都没有。你能帮我吗?我来自葡萄牙##0,00[$€-816],然后尝试用“#,##0[$€-816]”替换旧格式的欧元。或者先用$USD#.##0[$USD]格式化值,然后重试。程序首先查看是否在旧格式设置中找到值。如果找不到,它什么也不做。