首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA Currency NumberFormat错误- Excel

VBA Currency NumberFormat错误- Excel
EN

Stack Overflow用户
提问于 2014-08-13 20:48:06
回答 1查看 2.2K关注 0票数 0

我有一个工作表,其中有许多格式为货币的单元格,我想通过组合框修改货币格式。

首先,我使用此代码来获取初始货币类型/格式。

代码语言:javascript
复制
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变量是全局变量,

代码语言:javascript
复制
Public oldformat As String

之后,我想使用oldformat变量进行find,并使用newformat变量进行替换,

代码语言:javascript
复制
 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接收正确的值,但我得到了一个错误,

代码语言:javascript
复制
Application.FindFormat.NumberFormat = oldformat
Application.ReplaceFormat.NumberFormat = newFormat

运行时错误'1004':应用程序定义或对象定义的错误

有没有办法将newformat和oldformat值传递给Numberformat属性?

还是有人另有打算做这件事?

示例文件的链接,https://www.dropbox.com/s/sdyfbddxy08pvlc/Change_Currency.xlsm

我非常感谢大家的帮助,我对VBA还是个新手。

英语不是我的母语,如果有任何错误我深表歉意。

EN

回答 1

Stack Overflow用户

发布于 2014-08-13 23:44:26

我已经下载了文件。然后将代码更改为:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25286636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档