My VBA is changing a formula I"m trying to code in, and it's breaking my macro...

Domroy

Board Regular
Joined
Mar 8, 2018
Messages
114
I have a macro that formats a set of data. Part of it is that I this line:

ActiveCell.FormulaR1C1 = "=MID(B4,FIND(" - ",B4)+1,LEN(B4))"

The problem is, I don't want a space before and after the hyphen. When I take it out and save, it puts the spaces back in and it's driving me insane. How can I get it to just type the formula I want? With the spaces in there, it breaks the macro, and I have to debug. It used to work, but I changed the formula, then realized the new formula didn't work, so I went back to this one (which works when I test it - but only WITHOUT the spaces. HELP!

What it does is it removes the prefix on thousands of skus. So the Sku is KNT-154354. The formula finds the dash, and then gives me everything after the first dash (sometimes there are other dashes and things after the dash.

Help?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Here's a different formula that will do that, and it won't break if there's no hyphen in the cell:

Code:
ActiveCell.Formula = "=REPLACE(B4,1,FIND(""-"",B4&""-""),"""")"
 
Last edited:
Upvote 0
YES!! Thank you, Marcelo! That fixed it.

Scott, there's always at least one hyphen, and that's where I want the formula to cut the prefix. Will your formula work for that too?

But thank you both! It's fixed!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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