簡體   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

除了提供的答案之外,還有兩種方法。

方式 1

在最頂部鍵入Option Compare Text 例如

Option Compare Text

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

您可以在Option Compare Statement中了解更多信息

方式二

將其轉換為大寫或小寫,然后進行比較

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

'~~> OR

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

將行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