我有一列包含文本的单元格,我想按字母顺序排序。但并非所有单元格都应按其第一个字符排序。在这些单元格中,应按其排序的字符的格式为(单)下划线。 现在我想创建一个辅助列,其中仅包含
示例列
我收集了一些 VBA 代码,对我来说看起来很合理:
Function GetUnderlinedText(inputText As String) As String
Dim cell As Range
Dim underlinedCharIndex As Long
Dim underlinedText As String
' Get the reference to the cell
Set cell = Application.Caller
' Find the index of the first underlined character
underlinedCharIndex = FindUnderlinedChar(inputText)
' If underlined character found, return the portion of text starting from that character
If underlinedCharIndex > 0 Then
underlinedText = Mid(inputText, underlinedCharIndex)
Else
underlinedText = inputText
End If
' Return the underlined portion of the text
GetUnderlinedText = underlinedText
End Function
Function FindUnderlinedChar(inputText As String) As Long
Dim i As Long
Dim char As String
' Iterate through each character of the text
For i = 1 To Len(inputText)
char = Mid(inputText, i, 1)
' Check if the character is underlined
If IsUnderlined(inputText, i) Then
' Return the index of the underlined character
FindUnderlinedChar = i
Exit Function
End If
Next i
' If no underlined character found, return 0
FindUnderlinedChar = 0
End Function
Function IsUnderlined(inputText As String, position As Long) As Boolean
' Check if the character at the specified position is underlined
If Range("A1").Characters(position, 1).Font.Underline = xlUnderlineStyleSingle Then
IsUnderlined = True
Else
IsUnderlined = False
End If
End Function
我现在陷入困境的是
Function IsUnderlined
,它总是返回TRUE。
在调试器中,我可以验证 Range("A1").Characters(position, 1).Font.Underline
永远不会更改其值 -4142 (= xlUnderlineStyleNone),即使我迭代字符也是如此。
谁能帮助这里出了什么问题吗?
IsUnderlined
函数始终使用以下代码检查单元格A1
的格式。因此,返回值始终相同。If Range("A1").Characters(position, 1).Font.Underline = xlUnderlineStyleSingle Then
If
子句来增强代码Option Explicit
Function GetUnderlinedText(inputText As Range) As String
Dim cell As Range
Dim underlinedCharIndex As Long
Dim underlinedText As String
If inputText.Font.Underline = xlUnderlineStyleSingle Or inputText.Font.Underline = xlUnderlineStyleNone Then
GetUnderlinedText = inputText.Value
Else
' Find the index of the first underlined character
underlinedCharIndex = FindUnderlinedChar(inputText)
' If underlined character found, return the portion of text starting from that character
If underlinedCharIndex > 0 Then
underlinedText = Mid(inputText, underlinedCharIndex)
Else
underlinedText = inputText
End If
' Return the underlined portion of the text
GetUnderlinedText = underlinedText
End If
End Function
Function FindUnderlinedChar(inputText As Range) As Long
Dim i As Long
Dim char As String
' Iterate through each character of the text
For i = 1 To Len(inputText.Value)
' char = Mid(inputText, i, 1)
' Check if the character is underlined
If IsUnderlined(inputText, i) Then
' Return the index of the underlined character
FindUnderlinedChar = i
Exit Function
End If
Next i
' If no underlined character found, return 0
FindUnderlinedChar = 0
End Function
Function IsUnderlined(inputText As Range, position As Long) As Boolean
' Check if the character at the specified position is underlined
IsUnderlined = (inputText.Characters(position, 1).Font.Underline = xlUnderlineStyleSingle)
End Function