Forum Discussion
VBA Formating macro help
- Apr 10, 2023
Rukmag I ran your code and didn't get any error. That said you are in a .csv file which can't have or run macros so that may be the problem. You can save the code in an .xlsm file and as long as the .xlsm file is open you can run the code (i.e. you don't need to copy the code into the other file).
As for the code, it doesn't change anything because the cell value isn't = to that only the left portion. I modified the code as follows to actually use the color that was put into that cell and keep the text:
Sub ChangeColor() Dim cell As Range For Each cell In Selection If UCase(Left(cell.Value, 12)) = "<FONT COLOR=" Then 'cell.Interior.Color = RGB(255, 165, 0) cell.Interior.Color = "&h" & Mid(cell.Value, 15, 8) cell.Value = Mid(cell.Value, 24, Len(cell.Value)) End If Next cell End Subalso, make sure you add a module and put the code into a module instead of the worksheet or workbook pages. Open the Project window if not open, right click and add module:
My code just results in a big ol crash haha
- mtarlerApr 10, 2023Silver Contributor
Rukmag I ran your code and didn't get any error. That said you are in a .csv file which can't have or run macros so that may be the problem. You can save the code in an .xlsm file and as long as the .xlsm file is open you can run the code (i.e. you don't need to copy the code into the other file).
As for the code, it doesn't change anything because the cell value isn't = to that only the left portion. I modified the code as follows to actually use the color that was put into that cell and keep the text:
Sub ChangeColor() Dim cell As Range For Each cell In Selection If UCase(Left(cell.Value, 12)) = "<FONT COLOR=" Then 'cell.Interior.Color = RGB(255, 165, 0) cell.Interior.Color = "&h" & Mid(cell.Value, 15, 8) cell.Value = Mid(cell.Value, 24, Len(cell.Value)) End If Next cell End Subalso, make sure you add a module and put the code into a module instead of the worksheet or workbook pages. Open the Project window if not open, right click and add module:
- RukmagApr 11, 2023Copper Contributor
mtarler
Hey. Thank you so much! I had no clue that CSV couldn't run any macro's by itself I would not have even considered that was an issue. Also thank you for fixing the code. I was thinking it would only affect the parts in <> since it was in the quotes.
I added the code to a blank workbook and used that as a shortcut to put on the ribbon so it opens the .xlsm file whenever the macro is pressed hopefully avoiding any crashes.
Thanks again for the help!