Help with VBA code

L

Legacy 227120

Guest
I am new to VBA and am trying to get one to work. I have copied one from a forum and I'm pretty sure it will do what I require. The code is as follows:

Sub test()


Do
myRange.Replace What:=" ", Replacement:=" ", LookAt:=xlPart
Loop Until Application.CountIf(myRange, "* *") = 0



End Sub



However when I test it, an error comes up: "Run-time error 424: Object required". When I debug it highlights the following line (underlined above). I'm pretty sure its an easy fix, however being new to VBA its quite difficult for me!
Any help would be appreciated.

 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
What are you actually trying to do, as the code supplied is incomplete ??
 
Upvote 0
I have an excel worksheet with a table of data, this was converted from a PDF file. However during the conversion it left multiple spaces between characters within each cell (4 spaces between each word to be exact).

I'm trying to get rid of all spaces between characters. I have tried the 'find & replace' and the 'trim' functions, but neither work.

I came across this VBA code on another forum, so wanted to try it out...
 
Upvote 0
Ok, here's acode to remove the excess spaces....but if it's an import from PDF, there may be other issues that are not really spaces


Code:
Sub MM1()
    Dim cell As Range
    For Each cell In ActiveSheet.UsedRange
        If Len(cell) > Len(WorksheetFunction.Trim(cell)) Then
            cell.Value = WorksheetFunction.Trim(cell)
        End If
    Next cell
End Sub
 
Upvote 0
Thanks Michael.
You are spot on. I got a code from another forum and it didnt work the first time due to there being 'non-breaking spaces'. However I got it to work in the end.

Many thanks for your response!
 
Upvote 0
PLeasure, glad you got it sorted....(y)
 
Upvote 0

Forum statistics

Threads
1,216,165
Messages
6,129,242
Members
449,496
Latest member
Patupaiarehe

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