md_iffu,
Welcome to the MrExcel forum.
1. What version of Excel, and, Windows are you using?
2. Are you using a PC or a Mac?
You have not given us any examples of what is in column R, and, you have not displayed what the results should look like.
Here is a macro solution for you to consider that does not do any looping.
Sample raw data in the active worksheet:
Excel 2007 |
---|
|
---|
| R |
---|
1 | this is a test |
---|
2 | this is not a test |
---|
3 | this is a test |
---|
4 | this is not a test |
---|
5 | this is a test |
---|
6 | this is not a test |
---|
7 | |
---|
|
---|
After the macro:
Excel 2007 |
---|
|
---|
| R |
---|
1 | this is a test |
---|
2 | this is not a test |
---|
3 | this is a test |
---|
4 | this is not a test |
---|
5 | this is a test |
---|
6 | this is not a test |
---|
7 | |
---|
|
---|
Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).
1. Copy the below code
2. Open your NEW workbook
3. Press the keys
ALT +
F11 to open the Visual Basic Editor
4. Press the keys
ALT +
I to activate the Insert menu
5. Press
M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys
ALT +
Q to exit the Editor, and return to Excel
8. To run the macro from Excel press
ALT +
F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
Code:
Sub TrimColumnR()
' hiker95, 05/20/2015, ME83611
With ActiveSheet.Range("R1:R" & Cells(Rows.Count, "R").End(xlUp).Row)
.Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
End With
End Sub
Before you use the macro with
Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension
.xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
Then run the
TrimColumnR macro.