VBA Replace All '/' in a sheet, except for formulas

LarsB24

New Member
Joined
Oct 30, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I am making a tool to convert a messy data sheet with thousands of rows, coming out of an online pdf-to-excel converter, into a series of separate worksheets containing the necessary associated data. Each sheet is named after a section of the data, and this sheet name is used to collect all the corresponding data from the big input sheet.
Unfortunately, the sections can have '/' symbols or other illegal sheet-name characters in them, so I would need to replace all '/' in the document with a '-' symbol.
The problem is that the online tool also creates cells with '/' that start with the symbol '=', so excel thinks they are formulas, even though the cell format is set to text, and will give an error when I try to change them.

Is there a way in VBA to do 'replace all' on millions of cells at once, but skipping all cells that start with the '=' symbol? I can't replace the '=' symbol itself, as my tool uses it as a marker to find the correct values that it needs.

Thanks in advance!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
If you can replace a character in millions of cells "at once" with vba without looping through them I don't know how. Would not be surprised if it's possible and I imagine it would involve building a huge array. This would probably cause you to run out of memory whereas looping would not, but would take some time. So if it's a one-off thing and you are forced to loop, then an option would be to start the loop and go for lunch ;)

I think you're asking how to avoid doing this in cells with a formula. For that you could use Range.HasFormula property on the cells, which should be more efficient than testing if the first character is "=". Replace() function would be the way to replace any part of a value in a cell.
 
Upvote 0
I suspect the performance will be rubbish if there are too many occurrences or too many non-contiguous areas (if it doesn't grind to a halt) but maybe (test on a copy of your data)....

VBA Code:
Cells.SpecialCells(xlCellTypeConstants, 23).Replace "/", "-", xlPart
 
Last edited:
Upvote 0
Solution
I suspect the performance will be rubbish if there are too many occurrences or too many non-contiguous areas (if it doesn't grind to a halt) but maybe (test on a copy of your data)....

VBA Code:
Cells.SpecialCells(xlCellTypeConstants, 23).Replace "/", "-", xlPart
Thank you very much, that actually works! And performance seems good as well after testing it on smaller data sets!
 
Upvote 0

Forum statistics

Threads
1,223,848
Messages
6,174,995
Members
452,599
Latest member
wolf1988

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