Replace NumberFormat VBA Code

htim1

New Member
Joined
Sep 17, 2014
Messages
18
Hi - I want to create a code that searches column T for "1" and replace this with "0001" (so it must be custom formatted to "0000", otherwise the number will be shown as "1" instead). I've done a similar replace/format code for "û" which works perfectly, so I don't know why it keeps giving me a debug error for "0001" code. I have 2000 rows.

Works perfectly:
Code:
'In column C, replace û with -. 
Columns("C:C").Select
Application.ReplaceFormat.NumberFormat = "General"
Selection.Replace What:="û", Replacement:="-", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=True
Range("C1").Select

Need help with:
Code:
'In column T, replace 1 with 0001. 
Application.ReplaceFormat.NumberFormat = "0000"
Selection.Replace What:="1", Replacement:="0001", LookAt:=xlWhole, _
      SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
      ReplaceFormat:=True
Range("T1").Select

I get Run-Time error '1004': Application-defined or object-defined error for line item "Application.ReplaceFormat.NumberFormat = "0000"

I tried to change it to "Application.ReplaceFormat.NumberFormat = "@" so it is formatted as text instead of custom, but that did not fix the issue, because the number still shows up as 1, rather than 0001.


Could someone please assist?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

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