无法获取字符类的文本属性

这是一个摘录forms,我在VBA工作的Excel 2010macros,我遇到的问题是DelStrikethroughs

每当单元格中的值为“TRUE”,“FALSE”或“#N / A”(当其“#N / A”macros崩溃时),函数返回一个空string。 进一步调查时,它看起来像variablesx.text总是空白,并有错误"Unable to get the text property of the characters class"当我尝试debugging它。

有想法该怎么解决这个吗? (我很高兴函数返回原始文本,如果它不能通过文本删除罢工,但一个适当的解决scheme是首选)

以下是代码示例:

 Sub testx() Dim testRange As Range Set testRange = selection Call DelStrikethroughs(testRange.Cells(1, 1)) End Sub Function DelStrikethroughs(Cell As Range) As String 'Returns the text value of a cell with strikethrough characters removed Dim NewText As String Dim iCh As Integer For iCh = 1 To Len(Cell) Dim x As Characters Set x = Cell.Characters(iCh, 1) On Error Resume Next '"On Error" is here to deal with blank characters If x.Font.Strikethrough = False Then NewText = NewText & x.text End If If Err.Number = 0 Then NewText = NewText Else NewText = NewText & x.text End If Next iCh DelStrikethroughs = NewText End Function 

尝试这个:

 Sub testx() Dim testRange As Range, c As Range Set testRange = Selection For Each c In testRange c.Offset(0, 1).Value = DelStrikethroughs(c) Next c End Sub Function DelStrikethroughs(Cell As Range) As String 'Returns the text value of a cell with strikethrough characters removed Dim NewText As String Dim iCh As Long, l As Long, ch As Characters On Error Resume Next l = Cell.Characters.Count On Error GoTo 0 If l = 0 Then NewText = Cell.Text Else For iCh = 1 To l Set ch = Cell.Characters(iCh, 1) NewText = NewText & IIf(ch.Font.Strikethrough, "", ch.Text) Next iCh End If DelStrikethroughs = NewText End Function 

如果你想要做的只是返回单元格中的文本,没有任何删除线,然后尝试:

 Function DelStrikethroughs(Cell As Range) As String DelStrikethroughs = Cell.Text End Function