VBA Replace does not work

PedroVision

New Member
Joined
Oct 23, 2024
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
tempRowCounter = Replace(tempRowCounter, " ", "")

Trying to remove all spaces from a text string...

I've tried 67 different ways...and all I keep getting from VBA is...see image...


I just think this is, or should be difficult...but I'm not having any luck getting past this...any help would be humbly appreciated!

Many thanks...
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
There is no image, but neither is there anything wrong with the code you posted - as far as it goes.
 
Upvote 0
There is no image, but neither is there anything wrong with the code you posted - as far as it goes.
I don't there's anything wrong with it either...BUT...it doesn't work...see image...

(Thank you for replying...I'M STUCK!!)
 

Attachments

  • Screenshot 2024-10-24 000830.png
    Screenshot 2024-10-24 000830.png
    26 KB · Views: 16
Upvote 0
The code as posted works for me:
VBA Code:
Sub Test()
Dim tempRowCounter As String: tempRowCounter = "Hello World. Today is a beautiful day"
tempRowCounter = Replace(tempRowCounter, " ", "")
MsgBox tempRowCounter
End Sub
 
Upvote 0
This works for me without error. Change references as required.
Code:
Sub Maybe_So()
Dim c As Range
    For Each c In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        c.Value = Replace(c.Value, " ", "", 1)
    Next c
End Sub

Forgot the F5 again. Sorry Paul.
 
Upvote 0
This works for me without error. Change references as required.
Code:
Sub Maybe_So()
Dim c As Range
    For Each c In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        c.Value = Replace(c.Value, " ", "", 1)
    Next c
End Sub

Forgot the F5 again. Sorry Paul.

That could be done without a loop.

VBA Code:
Sub Maybe_More_So()
Range("A:A").Replace " ", ""
End Sub
 
Upvote 0
Another example...purported to work...It does not work on my computer...Excel 2019...W11...nothing exotic...

Remove all spaces from text​

Trim will only remove extra spaces in between words, but to remove all spaces in a string of text, you can use the Replace Function:

Sub ReplaceExample
MsgBox Replace " I love excel ", " ", "")
'Result is: "Iloveexcel"
End

 

Attachments

  • Screenshot 2024-10-24 005732.png
    Screenshot 2024-10-24 005732.png
    35.7 KB · Views: 2
Upvote 0
I'm in VBA...trying to write data to my spreadsheet...

I need to remove the blank spaces from my data BEFORE it gets written to the worksheet.

MyData = Replace(MyData, " ", "")

What am I missing here??
 
Upvote 0
What is the value of MyData at this line of code when you step through it? Is MyData of String data type?
 
Upvote 0
What is the value of MyData at this line of code when you step through it? Is MyData of String data type?
Here is another example...again, it does not work...

Sub ReplaceExample
MsgBox Replace " I love excel ", " ", "")
'Result is: "Iloveexcel" <----NOPE! IT DOESN'T WORK
End

I am in VBA...data is about to be loaded into a listbox...I am working with row numbers...and preparing to write those row numbers into the first (of three) column...BUT I must make them all the same length by adding leading zeros...when I take Row 1-9, I need to add 3 leading zeros the row number...for some reason when adding the "000" to the row number, Excel insists on inserting a blank space in between...thus, "000 1"...I need to remove this blank space BEFORE writing to my listbox.
 

Attachments

  • Screenshot 2024-10-24 005732.png
    Screenshot 2024-10-24 005732.png
    35.7 KB · Views: 7
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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