type mismatch error question

Erich Duff

Board Regular
Joined
Apr 20, 2005
Messages
129
All,

I'm getting a "Type Mismatch" error trying to run this code...anybody see what's wrong with it?
------------------------
Sub FormatChange()
If Cells("Criteria!E22") = "Sales" Then
Worksheets("Test").Range("C6:J18").NumberFormat = "$#,##0($#,##0)"
Else
Worksheets("Test").Range("C6:J18").NumberFormat = "0.00"
End If
End Sub
-------------------------

I have a list box whose criteria range points to a cell on another page, thus when that cell (on sheet Criteria, cell E22) reads Sales, I want the number format of the range on the page Test to be a dollar format, and otherwise just a 2-decimal number format. Can't seem to figure out what the problem is, though.

Thanks,

Erich
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Erich

Is Criteria the name of a worksheet?
Code:
Sub FormatChange() 
If Worksheets("Criteria").Range("E22").Value = "Sales" Then 
     Worksheets("Test").Range("C6:J18").NumberFormat = "$#,##0($#,##0)" 
Else 
     Worksheets("Test").Range("C6:J18").NumberFormat = "0.00" 
End If 
End Sub
 
Upvote 0
Try Norie's suggestion.

Also, I'm guessing that you probably want $#,##0;($#,##0) instead of $#,##0($#,##0)

(note the semicolon in the former)
 
Upvote 0
Hm. This still isn't quite working...it'll retain the format set first, but if "Sales" is NOT selected, it won't change it to the "0.00" format.

Any ideas?

Erich
 
Upvote 0
Don't know if this is the best way to fix, but it seems to work...I split this into two macros that call each other as their "Else" entries, and now the format changes appropriately.

Erich
 
Upvote 0
Sorry, I cannot replicate your problem.

Sub FormatChange()
If Worksheets("Criteria").Range("E22").Value = "Sales" Then
Worksheets("Test").Range("C6:J18").NumberFormat = "$#,##0;($#,##0)"
Else
Worksheets("Test").Range("C6:J18").NumberFormat = "0.00"
End If
End Sub


Works as intended for me...
 
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,176
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top