How to find and remove special characters in Excel sheet? - sql

My Excel sheet contains more than 20000 records in between some where i have values like this सोनिगरा केसर .
How to find these kind of characters ?
I am trying to import this XL Sheet in my Sql server DB but i am getting
"Text was truncated or one or more characters had no match in the target code page.".
How to solve this issue?

Ablebits is a good tool to remove any unwanted special characters from excel, specially which comes over during migration. You just need to provide a list of characters to be removed.
Reference : https://www.ablebits.com/excel-clean-cells/howto-remove-chars.php

Can you try this vba?
Function RemChrs(s As String) As String
Static RegEx As Object
If RegEx Is Nothing Then
Set RegEx = CreateObject("VBScript.RegExp")
RegEx.Global = True
End If
RegEx.Pattern = "à|¤|¸|¥|‹|¨|¿|—|°|¾|•|‡|°"
RemChrs = RegEx.Replace(s, "")
End Function
You can add pattern like |x. Also you can modify code with for loop for run all sheet cell. In this exampe you should use =RemChrs(A1) .
I hope I can help you.
Edit:
If you don't want to use vba, you can edit manually with using ctrl+h and replace all unwanted chars one by one.

Related

How to add multiple words to one line of command

I'm trying to run a function. When you hit the button if the textbox includes the following words below it will display "Please don't swear." The problem is that I'm trying to add multiples words to one line of command using the "or" function.
For some reason it does not work. Here is my code:
If InStr(1, Command.Text, "shit" Or "ass", vbTextCompare) Then
MsgBox("Please don't swear.")
Any help is appreciated! :)
We're not in VB6 anymore Toto. Let's not use InStr or MsgBox.
Dim prohibitedWords = {"bad", "word"}
If prohibitedWords.Any(Function(s) TextBox1.Text.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) <> -1) Then
'...
End If
I used IndexOf rather than Contains there to allow a case-insensitive comparison.
One issue with that is that is will match parts of other legitimate words. To avoid that, you should probably use a Regex and match whole words only. I'll leave it to you to search how to do that.

Extracting substring based on different criterias and placing the extracted string in another cell of same row

I have an excel file, in which there is a column containing a specific string. This string doesn't follow any particular pattern. My requirement is to extract a sub-string (product id) which is a set of 8 consecutive numbers that have to be preceded/followed by any no of characters or must be at the start or end of the string.
Following are some examples.
Scenario 1:
product id is preceded by #
Id#53298632/BS TVR:003519
Function used in excel
=MID(N88,FIND("#",N88)+1,8)
* result : 53298632 *
Scenario 2:
product id is at the beginning
53298632:003519
Function used in excel
=MID(A1,1,8)
* result : 53298632 *
At the beginning I had to deal with only scenario 1 and hence used the specified formula. Now a days the string doesnt follow any particular pattern but my product id still comes as 8 digit consecutive numbers. I searched for a suitable solution and found this formula (which I dont clearly understand).
=LOOKUP(10^8,MID(N132,ROW(INDIRECT("1:"&LEN(N132)-7)),8)+0)
This does work in most of the cases but in some cases it fails
For example
Pdr#53298632/ QTY NOS 1031949
Here the result is 1031949 which is definitely not what I want. The result should have been 53298632
Please help me fix this. Can this be done using VBA macro? I am completely new to such excel functions VBA and macro.
Any help will be highly appreciated!
Thanks in advance.
If you are happy to specifically include the Microsoft RegEx module into your Excel project, regular expressions will solve this reasonably quickly.
To add the RegEx function to use in your Excel Macros, select the Developer menu in Excel and start the Visual Basic editor. Within the VBA for Applications window, Select Tools->References and select Microsoft VBScript Regular Expressions 5.5.
Create a new Module for your VBAProject (right-click on your Excel file name in the project tree and click Insert->Module)
Double click on the newly created Module (within the project tree) and enter the following code in the Module1 (Code) window:
Public Function getProductCode(source As String) As String
Dim strPattern As String: strPattern = "(\d{8})"
Dim result As String: result = ""
Dim results As Object
Dim regEx As New RegExp
With regEx
.Global = True
.MultiLine = False
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(source) Then
Set results = regEx.Execute(source)
If (results.Count <> 0) Then
result = results.Item(0)
End If
End If
getProductCode = result
End Function
From the relevant cell in Excel, you can now call the macro:
=getProductCode(A1)
I guess you could also modify the original formula to pick up the first match of an 8-digit number
=MID(A1,MATCH(TRUE,ISNUMBER(MID(A1,ROW(INDIRECT("1:"&LEN(A1)-7)),8)+0),0),8)
(must be entered as an array formula using CtrlShiftEnter).

Range accepts sometimes only semicolons instead of commas

I have reduced my problem to the following code example. I am using a German Excel version in which separators in normal Excel formulas are semicolons ";" instead of "," (e.g. =SUMME(A1;A3) instead of =SUM(A1,A3)).
Now the code which works different from time to time:
Sub CommasDoNotWorkAnymore()
Dim a()
Dim i%
a = Array("A1,A3,A5", "B1", "B2")
i = 0
Debug.Print Sheets(1).Range(a(i)).Address
End Sub
Normally, when starting Excel, this code works. But sometimes Excel seem to switch the accepted separators used in the Range() to semicolons untill I restart Excel. This occurs most times when rerunning the code after a runtime error.
Is this a general Excel bug? Does anybody know what is behind this behaviour? Is there some Excel-wide "local option" for the Range class?
EDIT: I just tried to convert the a(i) with CStr(a(i) but this does also not work. So no ByRef kind of problem...
If you want to control it, check first what separator is currently in use. What I guess is that you want to know the list separator:
Application.International(xlListSeparator)
Check other separators here:
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-international-property-excel
The other time I had a problem with identifying decimal separator in VBA. Finnally I was able to get it in this way:
Function GetVBAdecimalSep()
Dim a(0) As Variant
a(0) = 1 / 2
GetVBAdecimalSep = Mid(a(0), 2, 1)
End Function
Changing separator not always works. Please see this: Changing decimal separator in VBA (not only in Excel)
The best solution is to check/change locale, even temporary.
Application.LanguageSettings.LanguageID(msoLanguageIDUI)
gives the LCID which would be 1033 for English (US)

Mystery Excel semicolon mode for data validation, stops recognizeing standard comma for unknown reason

In Excel VBA how can I set cell validation so that it works in all locales?
This code works in my locale, semicolon separated
Dim mySheet As Worksheet
Set mySheet = ThisWorkbook.Worksheets("Sheet 1")
Dim myRange As Range
Dim validValues as String
validValues = "a;b;c"
Set myRange = mySheet.Cells(2, 2)
myRange.Validation.Add _
Type:=xlValidateList, _
AlertStyle:=xlValidAlertStop, _
Formula1:=validValues
But I've seen that other locales needs comma separated
validValues = "a,b,c"
Essentially what I need is this, but I don't know how to determine 'id' in way that works in all locales.
' Internationalized delimiter
Dim id as String
id = ???
validValues = "a" & id & "b" & id & "c"
Edit:
There seems to be a "semicolon mode" of Excel that I do not understand and which I cannot reproduce reliably. I had a certain instance of Excel that got into this mode and all code run inside it worked with semicolon and not comma. I wrote a project in this instance and it worked, I would have thought I just made a mistake if not for all the code that I now have to rewrite...
Any hints in order to figure this out is appreciated.
Edit2:
After restarting Excel it changed it's mode to comma, I've been using it for a while but now this problem hit me again, Excel started using ';' (semicolon) instead of ',' (comma) as delimiter for my data validations.
This time I took a screenshot. Data validation is set to List.
Source is set to:
zero,one,both
This is the result, as you can see Excel does not care about my commas and displays all on the same line:
With source set like this it works:
zero;one;both
Result:
It is a mystery to me why Excel does this but I really need to find a way to make sure this never happens to my customer. Any help appreciated!
I have just dealed with a similar problem. I couldn't find a reason for why this happens, so I don't have a solution for good. Yet, I found a way around the problem that could be of use.
The only thing that you would need is to assign a valid value to the validated range before adding the validation.
After running your code, if the .Formula1 argument worked with semicolons, then there is no problem at all. If it didn't, the .Validation.Value property of the range returns False and you can use that as a trigger to rerun your code, replacing the semicolon separated list by a comma separated one. Then just rerun the validation.
For example:
myRange.value = "a" 'Assuming "a" is a valid value contained in the validvalues string
If myRange.Validation.Value = False Then
validvalues = replace(validvalues,";",",")
myRange.validation.modify Formula1:= validvalues
End if
Then you can clear the range value if you do not wish to have a default value shown.
I believe you are looking for a delimiter that would work on multiple scenarios (please let me know if this is not your query).
comma, semicolon, are sometimes good delimiter but often they pose serious syntax and some other string related errors .
Triple question mark ??? is also not a good choice in this case.
I prefer to go with | (the pipe) in these situations.
Sorry for the long answer :)
Microsoft says you need to use commas in VBA
https://support.microsoft.com/en-us/help/299490/data-validation-list-entries-all-on-one-line-in-excel

How to count the number of empty spaces in front of a String in VBA?

I have a YAML file which I am reading using VBA Excel plugin. I am getting a String of each line. I need to count the number of empty spaces in the front of the first line so that it will be a marker to find the next key. Let me know how to do that.
Option Explicit
Function empty_spaces(str_input)
empty_spaces = Len(str_input) - Len(LTrim(str_input))
End Function
harun24hr, your answer does not work with " My String " and similar.