Excel文本格式,主键可以同时具有string或数字格式,如何查找

我有一个数字身份证号为每个客户的Excel表:

例

图片上的第三个数字是在数字前面加上一个引号: '4346000273 。 它使excel将其作为文本编号读取。

我使用这些数字作为vba代码中的Application.Vlookup函数的第一个参数。

我将这些数字存储为Stringtypes的Client类属性:

 Private identityNumber As String Public Property Let setIdentityNumber(value As String) identityNumber = value End Property Public Property Get getIdentityNumber() getIdentityNumber = identityNumber End Property 'inside another sub, which creates a list of clients: clientCopy.setIdentityNumber = .Cells(i, column_identity).value 'how I use the numbers in VLookup Function getInfo(clientIdentity As String, infoWorkbook As Workbook) resultValue = Application.VLookup(clientIdentity, _ .Range(rangeString), 4, 0) 

我想知道这是一个正确的方法来处理这种情况。 我担心可能会发生这种情况,因为这种格式的差异, Vlookup将无法find数字。

文本编号不是Excel中存在的东西。 它是TextNumeric 。 VBA中的VLookup显然不喜欢数字值。 为了解决这个“棘手”的部分,将参数声明为一个范围。 喜欢这个:

 Public Function GetInfo(clientIdentification As Range) As Variant GetInfo = Application.WorksheetFunction.VLookup(clientIdentification, _ Range("A1:B3"), 2, False) End Function 

这是你得到的:

在这里输入图像说明