2016-08-24 67 views
0

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

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​ 
+0

您使用的是旧版本的excel吗? –

+0

我正在使用2013.它一直在工作,但突然间今天突然在此行上抛出一个错误。 – johndoe253

+0

什么单元格和工作表组成了'rng'对象? –

回答

0

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