VBA: Issue with capturing cell formatting using Range.DisplayFormat.NumberFormat

ez08mba

Board Regular
Joined
Nov 1, 2011
Messages
225
Office Version
  1. 2016
Platform
  1. Windows
I'm experiencing an issue where the manual check of formatting for a particular cell does not match what the Range.DisplayFormat.NumberFormat returns (I've tried .NumberFormatLocal also). It appears it is replacing parenthesis characters () with a hyphen -.


What I'm trying to do:
A client has provided a Template workbook in which we are to automate to open and compare other workboooks they want to standardize to the template workbook. They want a Cell-For-Cell comparision in things such as formatting, borders, values, etc. At this stage, what I'm trying to do is compare cell formatting between the same cell position (e.g. N11) between the workbooks for standardization.


My Problem:
In this case as an example, using cell N11, when I check the custom formatting manually by right clicking the cells I see that they match:


Workbook A cell N11: _($* #,##0_ );_($* (#,##0);_($* "-"??_);_(@_)
Workbook B cell N11: _($* #,##0_ );_($* (#,##0);_($* "-"??_);_(@_)


When I run the code in VBA for comparison in the automation, using the Locals window, it produces the following with the formatting for Workbook A (template) drastically different. It is replacing parenthesis with hyphens and adding ".00":


Workbook A cell N11: "_-$* #,##0.00_-;-$* #,##0.00_-;_-$* "-"??_-;_-@_-" <--- this is wrong and not what is in formatting window
Workbook B cell N11: "_($* #,##0_ );_($* (#,##0);_($* "-"??_);_(@_)"

(note: I don't know why above the color formatting here is orange or blue. I only used red to highlight the differences)


This is the function code I'm using to capture the formatting of a cell for comparison and the call to the function into a string variable
Code:
Public Function CheckFormatting(fmt As Range)
    CheckFormatting = CStr(fmt.[B]DisplayFormat.NumberFormat[/B])
End Function

' the call.....
Dim formatT As String: formatT = CheckFormatting(fmtT)
Dim formatC As String: formatC = CheckFormatting(fmtC)
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Does the cell contain any conditional formatting?
 
Upvote 0
Does the cell contain any conditional formatting?

Good question. I selected the cell for each workbook but also neither workbook has any Conditional Formatting rules. I guess the answer is "no". :)
 
Upvote 0
What happens if you use
Code:
    CheckFormatting = fmt.NumberFormat
or
Code:
    CheckFormatting = CStr(fmt.NumberFormat)
 
Upvote 0
Hey Fluff! Your suggestions worked! I'm slapping myself for not thinking of it.

Problem solved. - Thanks!

Code:
 CheckFormatting = (CStr(fmt.NumberFormat)

: CheckFormatting : "_($* #,##0_ );_($* (#,##0);_($* "-"??_);_(@_)" : Variant/String
 
Upvote 0
Glad it's sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,144
Members
453,021
Latest member
Justyna P

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