A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
The reason for this RTE could be that
a) your format is wrong (because the NumberFormat property must be in English format) and
b) the format did not exist in the file
And your code did not work, because the DropButtonClick event is fired twice, the 1st time when the drop down opens and the 2nd when closes.
Try the code below.
Andreas.
Option Explicit
Dim OldFormat As String, NewFormat As String
Private Function GetFormat(ByVal Value) As String
Select Case UCase(Value)
Case "EUR"
GetFormat = "#,##0 $"
Case "GBP"
GetFormat = "[$£-809]#,##0"
Case "USD"
GetFormat = "#,##0 [$USD]"
End Select
End Function
Private Sub ComboBox1_GotFocus()
'Get the old format
OldFormat = GetFormat(ComboBox1.Value)
End Sub
Private Sub ComboBox1_LostFocus()
'Replace the format when the user leaves the combo box
Dim Ws As Worksheet
'Get the new format
NewFormat = GetFormat(ComboBox1.Value)
'Be sure we valid have a format, otherwise we'll get a RTE
If OldFormat = "" Then Exit Sub
If NewFormat = "" Then Exit Sub
'Maybe the format did not exist and must be added first
On Error GoTo ApplyFormat
'Prepare the search
With Application.FindFormat
.Clear
.NumberFormat = OldFormat
End With
With Application.ReplaceFormat
.Clear
.NumberFormat = NewFormat
End With
'Replace all
For Each Ws In Worksheets
Ws.UsedRange.Replace "", "", SearchFormat:=True, ReplaceFormat:=True
Next
Exit Sub
ApplyFormat:
'Apply each format once
Dim SaveFormat As String
On Error GoTo ExitPoint
SaveFormat = ActiveCell.NumberFormat
ActiveCell.NumberFormat = GetFormat("EUR")
ActiveCell.NumberFormat = GetFormat("GBP")
ActiveCell.NumberFormat = GetFormat("USD")
ActiveCell.NumberFormat = SaveFormat
'Now try again
Resume
ExitPoint:
End Sub