vba IF Accounting format Then "0"

gtd526

Well-known Member
Joined
Jul 30, 2013
Messages
657
Office Version
  1. 2019
Platform
  1. Windows
Hello,
Im using the following code to remove text if a desired value is found (N:N="sold"). It will delete the values for that particular row.
E:E and F:F are accounting formats and I would rather insert "0" to those cells rather than delete the value. Thank you.
Here is the code and the worksheet:
VBA Code:
Sub ClearSold()
'ClearContents w/o removing formula

    Dim Rng As Range
    Dim Cel As Range
    Dim Ws As Worksheet
   
        Set Rng = Range("N9:N26")
        For Each cell In Rng
            'select the cells First
            If cell.Value = "sold" Then
                cell.Select
                ActiveCell.Offset(0, -13).Select
                Range(ActiveCell, Cells(ActiveCell.Row, ActiveCell.Column + 14)).Select '.ClearContents
            'how to confirm 'accounting format'?
            '.NumberFormat = "_($*#,##0.00_);_($*(#,##0.00);_($*""_""??_);_(@_)"
            'change E:E, F:F to zero, dont delete
'''''''''''''''''''''''''''''''''''''''''''''''''
            For Each Cel In Selection
            If Cel.NumberFormat = "$*#,##0.00" Then  'not recognizing account format
                Cel.Value = 0
            End If
'''''''''''''''''''''''''''''''''''''''''''''''''
            'clearcontents if not a formula
            'change to "0" if contains "="
            If InStr(Cel.Formula, "=") = 0 And Cel.Value <> "" Then
                Cel.ClearContents
            End If
            Next Cel
            End If
        Next
       
End Sub


Worksheet:
Cell Formulas
RangeFormula
G9:G26G9=IF($F9=0,0,MAX($F9,$G9))
H9:H26H9=IF($H9=0,$F9,MEDIAN($H9,$F9,0))
I9:I26I9=(E9*D9)*100
J9:J26J9=(F9*D9)*100
K9:K26K9=J9-I9
L9:L26L9=IF(K9<>0,K9/I9,"")
M9:M26M9=IF(I9<>0,I9/$I$27,"")
P9:P26P9=IF(OR($N9="",$N9="sold"),"",$N9-TODAY())
Q9:Q26Q9=IF(OR($N9="",$N9="sold"),"","day(s)")
O22:O26,O19:O20,O10:O12O10=IF($D10>0,$I10/(5*100),"")
B24:B26,B9:B21B9=IF(COUNTIF(Sectors!$A$3:$V$50,A9)=1,INDEX(Sectors!$A$2:$V$2,MAX((Sectors!$A$3:$V$50=A9)*(COLUMN(Sectors!$A$2:$V$2)))),"")
I27:K27I27=SUM(I9:I26)
L27L27=IFERROR((K27/I27),"")
Press CTRL+SHIFT+ENTER to enter array formulas.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
@gtd526 Try replacing the line:

VBA Code:
If Cel.NumberFormat = "$*#,##0.00" Then  'not recognizing account format

with the following line:

VBA Code:
If Mid(Range(Cel).NumberFormat, 1, 15) = "_($* #,##0.00_)" Then
 
Upvote 0
@gtd526 Try replacing the line:

VBA Code:
If Cel.NumberFormat = "$*#,##0.00" Then  'not recognizing account format

with the following line:

VBA Code:
If Mid(Range(Cel).NumberFormat, 1, 15) = "_($* #,##0.00_)" Then
I entered your line but received the following error:
Method 'Range' of object '_Global' failed
 
Upvote 0
How about this as a replacement:

VBA Code:
If Mid(Cel.NumberFormat, 1, 15) = "_($* #,##0.00_)" Then
 
Upvote 0
Solution

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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