How to clear the unwanted characters from my rows in excel sheet?

how to delete the unwanted characters like !,`,-,. etc. from my excel rows and obtain a clean sheet?here i have attached my test excel
exceldt.xlsx (9.4 KB)

Hi @ydash999,

  1. Read Excel with the readragen activity.

  2. Convert the datatable to a string with outdatatable.

  3. Clean up the entire result string with regex.

  4. You can convert it back to arraylist and turn it into a datatable.


1 Like
  1. put below code in text file e.g. vba.txt

  2. in excel application scope call the vba function

Function removeSpecial()
    Dim r As Range
    Set r = ActiveSheet.UsedRange
    Dim cellRange As Range
    For Each cellRange In r
        Debug.Print "r"
        cellRange.Value = FindReplaceRegex(cellRange, "([\" & Chr(34) & "<>\(\):\!\`\-\.])", "")
    Next cellRange

End Function 

Function FindReplaceRegex(rng As Range, reg_exp As String, replace As String)
   'Dim regex As Object
    Set myRegExp = CreateObject("VBScript.RegExp")
    'Set myRegExp = New RegExp
    myRegExp.IgnoreCase = False
    myRegExp.Global = True
    myRegExp.Pattern = reg_exp
    FindReplaceRegex = myRegExp.replace(rng.Value, replace)
End Function



(From r In DT_Data.AsEnumerable Let ra = r.ItemArray.Select(Function (x) x.ToString.Trim.Replace("your target ",β€œ ”)).toArray() Select DT_Data.Rows.Add(ra)).CopyToDataTable()

try this after read excelfile

1 Like

how can i replace all the characters ,placing those in β€œyour target” ?i can only give one character right?


May i know from where you are getting this excel data

I am just giving a scenario, if these kinds of data come, and we have to clean them and feed to queue, then how to do

yesh you can give which one you want to replace

example for if you want to replace β€œ@” with β€œ#” , @ is your target value and # will be destination

1 Like

in this one code can i replace all the unwanted characters with β€œβ€?

Hey @ydash999

Read the Excel data into a table.

Replace the unwanted symbols with empty string for each row.

Write the table back to the Excel.

Hope this helps.


yeah it will

how to use replace method within for each?

in β€œtarget” i can only give one character like # or @ or any other right? how to replace more than one character with"" within β€œtarget”?

try this
d.xaml (10.5 KB)

if you dont use regex you can also use multiple assign statements to replace the special characters e.g.

  1. CurrentRow(col.ColumnName) = CurrentRow(col.ColumnName).toString.replace(β€œ<”, β€œβ€)
  2. CurrentRow(col.ColumnName) = CurrentRow(col.ColumnName).toString.replace(β€œ>”, β€œβ€)
    and so on …
1 Like

Hello @ydash999

you can do as below.

All the unwanted symbols are added in the below regex. If you want to add more symbols you can directly add it in the expression.

System.Text.RegularExpressions.Regex.Replace(Str, β€œ([`…:>(!β€œβ€/-])”, β€œβ€)

image (77.1 KB)

1 Like

you can give whatever character,number, string or anything

thankss bro!seems good solution to me

1 Like

thankss a lot !

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.