简体   繁体   中英

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".

The pixel-widths for each letter are then summed and displayed in a cell below the text input box - "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.

All the manuals I've seen say VBA is case sensitive on VLookup, and all I can find are ways to get around this.

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.

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.

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.

I simply can't see why VBA is breaking its own rules.

Vlookup isnt case sensitive.

ive found this function that "simulates" a vlookup case sensitive.

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)

more information in here

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!

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