简体   繁体   中英

Remove leading zero from a reference

I use LimeSurvey answers as a validation for data I need in Excel.

To check if I have the correct person they enter an IDnumber in the survey.
Then, it goes as follow:
I read an id card and extract their IDnumber from their IDcard and place it in cell "Personalia: B7" using this code (it's a text with the value in it).

=LEFT(PROPER(RIGHT(eIDimport!A2;LEN(eIDimport!A2)-FIND(CHAR(34);eIDimport!A2)));FIND("h=";PROPER(RIGHT(eIDimport!A2;LEN(eIDimport!A2)-FIND(CHAR(34);eIDimport!A2))))-13)

It's value is also linked to "Dataimport: M2 and L7 ( =Personalia!B7 )"

The problem is that the survey doesn't export leading zeroes, while the value from the ID does.
In my code, to check which row of data is necessary, the ID number(from the card) and the number from the survey has to be the same.
If it starts with a zero, logically, the values don't match and I get an error.

Example:
My Id number is 026548321.
The value from the IDcard is 026548321.
The value from the survey is 26548321.

How can, if the IDcard value starts with a 0, the first 0 in the cell "Dataimport M2" be removed?

I tried some codes I found on this Q & A but they don't work.
I also tried some some custom format, but it also doesn't change. I guess it's because it's a reference and not a manual input.

Replace Leading Characters

Context: ... doesn't export leading zeroes...

  • If you need to remove multiple zero's you could use a RegEx 'based' UDF . The image is showing how it works.

在此处输入图像描述

  • In Excel :

     =relaceLeadingChars('DataImport',M2,"0")
  • In VBA :

     Sub replaceLeadingCharsTEST() Dim SearchString As String: SearchString = "000203456" Dim ResultString As String ResultString = replaceLeadingChars(SearchString, "0") Debug.Print ResultString ' Result: "203456" End Sub

The User-Defined Function (UDF) (in a standard module eg Module1 )

Function replaceLeadingChars( _
    ByVal SearchString As String, _
    ByVal FindChar As String, _
    Optional ByVal ReplaceChar As String = "", _
    Optional ByVal doIgnoreCase As Boolean = False) _
As String
    '=replaceLeadingChars(A1,"0")
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = doIgnoreCase
        .Pattern = "^[" & FindChar & "]*"
        replaceLeadingChars = .Replace(SearchString, ReplaceChar)
    End With
End Function

You could of course simplify:

Function removeLeadingZeros( _
    ByVal SearchString As String) _
As String
    '=removeLeadingZeros(A1)
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "^[0]*"
        removeLeadingZeros = .Replace(SearchString, "")
    End With
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM