简体   繁体   English

我想让这个 VBA 代码不区分大小写

[英]I want to make this VBA code non case sensitive

Function SingleCellExtract(LookupValue As String, LookupRange As Range, ColumnNumber As Integer, Char As String)
    'Updateby Extendoffice
    Dim I As Long
    Dim xRet As String
    For I = 1 To LookupRange.Columns(1).Cells.Count
        If LookupRange.Cells(I, 1) = LookupValue Then
            If xRet = "" Then
                xRet = LookupRange.Cells(I, ColumnNumber) & Char
            Else
                xRet = xRet & "" & LookupRange.Cells(I, ColumnNumber) & Char
            End If
        End If
    Next
    SingleCellExtract = Left(xRet, Len(xRet) - 1)
End Function

Two more ways besides the provided answer.除了提供的答案之外,还有两种方法。

WAY 1方式 1

Type Option Compare Text at the very top.在最顶部键入Option Compare Text For example例如

Option Compare Text

Sub Sample()
    Dim stringA As String, stringB As String
    
    stringA = "sid"
    stringB = "SiD"
    
    MsgBox stringA = stringB
End Sub

You can read more about it in Option Compare Statement您可以在Option Compare Statement中了解更多信息

Way 2方式二

Convert it to uppercase or lower case and then compare将其转换为大写或小写,然后进行比较

If UCase(LookupRange.Cells(I, 1).Value) = UCase(LookupValue) Then

'~~> OR

If LCase(LookupRange.Cells(I, 1).Value) = LCase(LookupValue) Then

Change the line If LookupRange.Cells(i, 1) = LookupValue Then to将行If LookupRange.Cells(i, 1) = LookupValue Then更改为

If StrComp(LookupRange.Cells(i, 1), LookupValue, vbTextCompare) = 0 Then

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

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