translate procedure into function

neveu

Board Regular
Joined
Jan 27, 2009
Messages
225
hello forum,

i've created a procedure that would set the correct number format for raw data extracted from a system, but which is not recognized by Excel as number due to different pb (extra spaces, different decimal symbol ).

i would like to translate this into a function so i can use it in my code for different columns


here is the code. appreciate any inputs/help. thank you

Code:
For Each cell In Columns(Sheets("SETUP").Range("B21"))
    If cell <> "" Then
        If Application.WorksheetFunction.IsNumber(cell) = False Then
        cell.Value = Trim(cell.Text) 'delete spaces in numbers
        cell.Value = Application.WorksheetFunction.Substitute(cell.Text, Chr(32), "") 'delete unnecessary spaces
        cell.Value = Application.WorksheetFunction.Substitute(cell.Text, ",", "x") 'replace coma to x
        cell.Value = Application.WorksheetFunction.Substitute(cell.Text, ".", "x") 'replace dot to x
            If cell.Text Like "*x*" Then
                If Application.International(xlThousandsSeparator) <> " " Then
                    cell.Value = Application.WorksheetFunction.Substitute(Left(cell.Text, Len(cell.Text) - 3), "x", _
                    Application.International(xlThousandsSeparator)) & _
                    Application.WorksheetFunction.Substitute(Right(Rng.Text, 3), "x", _
                    Application.International(xlDecimalSeparator))
                    Else
                    cell.Value = Application.WorksheetFunction.Substitute(Left(cell.Text, Len(cell.Text) - 3), "x", "") & _
                    Application.WorksheetFunction.Substitute(Right(Rng.Text, 3), "x", _
                    Application.International(xlDecimalSeparator))
                End If
            End If
        cell.Value = CDbl(Rng.Value) * (1) 'double type
        cell.NumberFormat = "#,##0.00"
        End If
    End If
Next cell
 

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.
Something like:

Code:
Function RunMe()

Call CleanNo("A")
Call CleanNo("C")
Call CleanNo("E")

End Function


Function CleanNo(strCol As String)

Dim cell As Range, rng As Range

For Each cell In Columns(strCol)
    If cell <> "" Then
        If Application.WorksheetFunction.IsNumber(cell) = False Then
        cell.Value = Trim(cell.Text) 'delete spaces in numbers
        cell.Value = Application.WorksheetFunction.Substitute(cell.Text, Chr(32), "") 'delete unnecessary spaces
        cell.Value = Application.WorksheetFunction.Substitute(cell.Text, ",", "x") 'replace coma to x
        cell.Value = Application.WorksheetFunction.Substitute(cell.Text, ".", "x") 'replace dot to x
            If cell.Text Like "*x*" Then
                If Application.International(xlThousandsSeparator) <> " " Then
                    cell.Value = Application.WorksheetFunction.Substitute(Left(cell.Text, Len(cell.Text) - 3), "x", _
                    Application.International(xlThousandsSeparator)) & _
                    Application.WorksheetFunction.Substitute(Right(rng.Text, 3), "x", _
                    Application.International(xlDecimalSeparator))
                    Else
                    cell.Value = Application.WorksheetFunction.Substitute(Left(cell.Text, Len(cell.Text) - 3), "x", "") & _
                    Application.WorksheetFunction.Substitute(Right(rng.Text, 3), "x", _
                    Application.International(xlDecimalSeparator))
                End If
            End If
        cell.Value = CDbl(rng.Value) * (1) 'double type
        cell.NumberFormat = "#,##0.00"
        End If
    End If
Next cell

End Function

Note, there is a reference to a variable rng, which isnt anywhere in your code.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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