【问题标题】:Unable to get the characters run time error vba无法获取字符运行时错误 vba
【发布时间】:2016-08-25 16:43:24
【问题描述】:

这段代码一直在工作,但我不确定为什么 vba 似乎无法再通过这条线了。我收到运行时错误“1004”:无法获取 Range 类的字符属性。任何帮助表示赞赏。

Option Explicit

Sub proj()
    Dim dataRng As range, cl As range
    Dim arr As Variant

    Set dataRng = Worksheets("ItalicSourceSheet").range("C1:C5") '<--| change "ItalicSourceSheet" with your actual source sheet name
    With Worksheets("ItalicOutputSheet") '<--|change "ItalicOutputSheet" with your actual output sheet name
        For Each cl In dataRng
            arr = GetItalics(cl) '<--| get array with italic words
            If IsArray(arr) Then .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(UBound(arr) + 1) = Application.Transpose(arr) '<--| if array is filled then write it down to output sheet first blank cell in column "A"
        Next
    End With
End Sub

Function GetItalics(rng As range) As Variant
    Dim strng As String
    Dim iEnd As Long, iIni As Long, strngLen As Long

    strngLen = Len(rng.Value2)
    iIni = 1
    Do While iEnd <= strngLen
        Do While rng.Characters(iEnd, 1).Font.Italic And rng.Characters(iEnd, 1).Font.Underline
            If iEnd = strngLen Then Exit Do
            iEnd = iEnd + 1
        Loop
        If iEnd > iIni Then strng = strng & Mid(rng.Value2, iIni, iEnd - iIni) & "|"
        iEnd = iEnd + 1
        iIni = iEnd
    Loop
    If strng <> "" Then GetItalics = Split(Left(strng, Len(strng) - 1), "|")
End Function​

【问题讨论】:

  • 您使用的是旧版本的 excel 吗?
  • 我正在使用 2013。它一直在工作,但今天突然在这条线上抛出一个错误。
  • rng 对象由哪些单元格和工作表组成?
  • iEnd 是什么?也许它会帮助你看到更多的代码。
  • iEnd 没有初始值。

标签: vba excel


【解决方案1】:

我猜 ItalicSourceSheet!C1:C5 包含公式(计算值),因此 Characters 属性不可用。在这种情况下,您应该检查整个单元格是否为斜体(部分不能是斜体)或跳过这些单元格(取决于业务逻辑)。

【讨论】:

    猜你喜欢
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多