VBA search for spaces in a string - conversion from european to american decimal notation

exceltime

New Member
Joined
Aug 19, 2014
Messages
29
Hi Folks,
I have a badly structured CSV file that I have to scan as input to spreadsheet. Is there a way in VBA to scan for a string of spaces using a string function like Instr. Also in VBA I am looking for a function to convert numbers that are formatted using the european system with a comma as decimal separator to the american system using a dot. Help from experts is appreciated. Thanks in advance.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The replace multiple spaces with a single space you could use a quick (and dirty) VBA recursive function, e.g.

Code:
Sub test()
x = RemoveSpaces("f   g     h")
MsgBox x
End Sub

Function RemoveSpaces(sInput As String)
Dim Temp As String
Temp = Replace(sInput, "  ", " ")
If Len(sInput) <> Len(Temp) Then
    Temp = RemoveSpaces(Temp)
End If
RemoveSpaces = Temp
End Function

To change Euro to US decimal you could use a worksheet function:

=SUBSTITUTE(C1,",",".") to change from comma to decimal place then copy|paste special|values to turn the results into numbers.
 
Upvote 0
Your program works very well thanks a lot. As to the conversion is there no VBA function that may help. Thanks.
 
Upvote 0
The replace multiple spaces with a single space you could use a quick (and dirty) VBA recursive function, e.g.

Code:
Sub test()
x = RemoveSpaces("f   g    h")
MsgBox x
End Sub

Function RemoveSpaces(sInput As String)
Dim Temp As String
Temp = Replace(sInput, "  ", " ")
If Len(sInput) <> Len(Temp) Then
    Temp = RemoveSpaces(Temp)
End If
RemoveSpaces = Temp
End Function
There is no need to use a separate function to reduce multiple spaces to single spaces... just call out to the worksheet's TRIM function instead.
Code:
Sub test()
  Dim SingleSpaced As String
  SingleSpaced = [COLOR=#0000FF][B]WorksheetFunction.Trim[/B][/COLOR]("f   g      h")
  MsgBox SingleSpaced
End Sub
 
Upvote 0
Thanks Rick. I was under the impression that Trim only handled leading and trailing spaces. I should have experimented with it :confused:.

At least I got a reason to play with recursion; my mind didn't quite hurt enough :laugh:.
 
Upvote 0
Thanks Rick. I was under the impression that Trim only handled leading and trailing spaces. I should have experimented with it :confused:.
VB's Trim function only does leading and trailing spaces, but WorksheetFunction.Trim calls the TRIM function that Excel uses for worksheet formulas and that removes leading and traiing spaces AND collapses multiple adjacent spaces down to single spaces.
 
Upvote 0
Sorry Folks. I missed the point that I can use SUBSTITUTE with the prefix WorkSheet function. That solves he problem. Thanks.
 
Upvote 0

Forum statistics

Threads
1,223,250
Messages
6,171,036
Members
452,374
Latest member
keccles

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