简体   繁体   English

有什么想法为什么VBA在VLookup中不区分大小写?

[英]Any ideas why VBA isn't being case-sensitive in a VLookup?

I've created a VBA Macro to look at a string of input text in the cell E3, split it into individual characters and then VLookup those characters against a table of individual character pixel widths, which is added to the code using a named range, "pw_Table". 我创建了一个VBA宏,以查看单元格E3中的输入文本字符串,将其拆分为单个字符,然后根据单个字符像素宽度的表VLookup这些字符,并使用命名范围将其添加到代码中, “ pw_Table”。

The pixel-widths for each letter are then summed and displayed in a cell below the text input box - "Cells(4,5)". 然后,将每个字母的像素宽度相加并显示在文本输入框下方的单元格“ Cells(4,5)”中。 Hitting return is meant to show the combined pixel-width total for the complete string. 击中返回是为了显示完整字符串的组合像素宽度总和。

The problem is that it is not being case sensitive and is using the same VLookup value for both upper and lower case characters. 问题在于它不区分大小写,并且对大写和小写字符使用相同的VLookup值。

All the manuals I've seen say VBA is case sensitive on VLookup, and all I can find are ways to get around this. 我见过的所有手册都说VBA在VLookup上区分大小写,而我所能找到的就是解决此问题的方法。

For my issue, however, the VLookup must be case sensitive to make sure I get the correct pixel width for each letter, for example, "c" is 9 pixels wide, "C" is 13. 但是,对于我的问题,VLookup必须区分大小写,以确保每个字母的像素宽度正确,例如,“ c”为9像素宽,“ C”为13。

I have tried reordering the upper and lower case characters in the table to see if that made a difference, but it only uses the first values it encounters for each letter of the alphabet, whether they be upper- or lower-case. 我尝试对表中的大写和小写字符进行重新排序,以查看是否有区别,但是它仅使用遇到的每个字母的第一个值,无论它们是大写还是小写。

I thought that I might use INDEX, MATCH, and EXACT, but couldn't see how to implement that in VBA. 我以为我可能会使用INDEX,MATCH和EXACT,但看不到如何在VBA中实现它。

This is the Macro code ... 这是宏代码...

Private Sub ReadCharacter()    
    cell_value = ThisWorkbook.Sheets("Pixel-widths").Range("E3")    
    Character_Value = 0    
    For rep = 1 To Len(cell_value)
        Character = Mid(cell_value, rep, 1)        
        On Error GoTo MyErrorHandler        
        Character_Value = Application.WorksheetFunction.VLookup(Character, [pw_Table], 2, 0)          
        Pixel_Width = Pixel_Width + Character_Value        
MyErrorHandler:
        Character_Value = 10
        Resume Next        
    Next rep        
        Cells(4, 5) = Pixel_Width    
End Sub

I had some issues with numbers, with VBA reporting Run-time Error 1004, but I bodged this by adding an error trap because all the numerals from 0-9 are 10 pixels wide. 我在数字方面遇到了一些问题,VBA报告了运行时错误1004,但是我添加了一个错误陷阱,因为所有0到9的数字都是10像素宽,因此我对此有所怀疑。

I simply can't see why VBA is breaking its own rules. 我简直不明白为什么VBA违反了自己的规则。

Vlookup isnt case sensitive. Vlookup不区分大小写。

ive found this function that "simulates" a vlookup case sensitive. 我发现该函数“模拟”一个vlookup,区分大小写。

Function CaseVLook(FindValue, TableArray As Range, Optional ColumnID As Integer = 1) As Variant
    Dim xCell As Range
    Application.Volatile
    CaseVLook = "Not Found"
    For Each xCell In TableArray.Columns(1).Cells
        If xCell = FindValue Then
            CaseVLook = xCell.Offset(0, ColumnID - 1)
            Exit For
        End If
    Next
End Function

to use it just call it CaseVLook(F1,A1:C7,3) 要使用它,只需将其命名为CaseVLook(F1,A1:C7,3)

more information in here 更多信息在这里

https://www.extendoffice.com/documents/excel/3449-excel-vlookup-case-sensitive-insensitive.html https://www.extendoffice.com/documents/excel/3449-excel-vlookup-case-sensitive-insensitive.html

good luck 祝好运

Here's another way... 这是另一种方式

Character_Value = Evaluate("INDEX(" & Range("pw_Table").Address(, , , True) & _
    ",MATCH(TRUE,EXACT(INDEX(" & Range("pw_Table").Address(, , , True) & ",0,1),""" & Character & """),0),2)")

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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