在单元格内搜索代码中键入不匹配

时间:2017-04-21 16:17:25

标签: vba types

我在搜索单元格一段时间时遇到类型不匹配“。”或“p”。当我只使用“。”时,它工作。但在添加“p”后,我得到了类型不匹配。 my_txt的Variant / Empty只能使用整数和“。”吗?我正在尝试使用过滤器。或者p来确定大纲级别。

Sub ProcessDocV5()
    Dim Level As Range
    Dim i, j, q(1 To 50) As Long
    Dim numofchar As Long
    Dim filepath As String
    Dim filename As String
    Dim LastRow As Long
    Dim rowcallout As Long
    Dim columncallout As Long


    'scanf(Input the correct row and column numbers).
    rowcallout = InputBox("LOCATION ROW OF HEADERS?")
    columncallout = InputBox("LOCATION COLUMN OUTLINE? (A=1, B=2, ect...)")
     Debug.Print "rowcallout value is "; [rowcallout]
     Debug.Print "columncallout value is "; [columncallout]
    'END OF SCAN


    'ADJUST EXCEL SCREEN
    'stop screen updating
    Application.ScreenUpdating = False
    'show gridlines
    ActiveWindow.DisplayGridlines = True
    'remove borders
    ActiveWindow.DisplayGridlines = True
    Cells.Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    Selection.Borders(xlEdgeLeft).LineStyle = xlNone
    Selection.Borders(xlEdgeTop).LineStyle = xlNone
    Selection.Borders(xlEdgeBottom).LineStyle = xlNone
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone


    'group according to level column (Cell(row,column))
    Set Level = Range(Cells(rowcallout, columncallout), Cells(873, 2))
    Debug.Print "The value of Levels is "; Level.Address

    For i = rowcallout To Level.count

        Cells(i, columncallout).Select

        a = Len(Cells(i, columncallout))
        Debug.Print "A value is "; [a]
        my_txt = Replace(Cells(i, columncallout), "." Or "p", "", 1, -1, vbTextCompare)
        b = Len(my_txt)
        Debug.Print "B value is "; [b]
        numb_occur = a - b + 1
        Debug.Print [numb_occur]

        If numb_occur < 8 Then
            Rows(i).OutlineLevel = numb_occur - 1
        Else
            Rows(i).OutlineLevel = 8
        End If

    Next i


    With ActiveSheet.Outline
        .AutomaticStyles = False
        .SummaryRow = xlAbove
        .SummaryColumn = xlRight
    End With

    'Close tabs for neatness
    ActiveSheet.Outline.ShowLevels RowLevels:=8
    ActiveSheet.Outline.ShowLevels RowLevels:=7
    ActiveSheet.Outline.ShowLevels RowLevels:=6
    ActiveSheet.Outline.ShowLevels RowLevels:=5
    ActiveSheet.Outline.ShowLevels RowLevels:=4
    ActiveSheet.Outline.ShowLevels RowLevels:=3
    ActiveSheet.Outline.ShowLevels RowLevels:=2
    ActiveSheet.Outline.ShowLevels RowLevels:=1


    End Sub

1 个答案:

答案 0 :(得分:3)

这是很多不相关的代码,但我设法挖掘了这个代码:

my_txt = Replace(Cells(i, columncallout), "." Or "p", "", 1, -1, vbTextCompare)

"." Or "p"是VBA无法评估的表达式。以下是我们需要看到的全部内容:

Debug.Print "." Or "p"

此说明再现了您遇到的确切问题:类型不匹配错误。

Or是一个逻辑二元运算符,在使用时,会计算为TrueFalse。当用作按位运算符时,它可以计算为Long,但是当VBA为我们进行很多隐式类型转换时,它有一个限制,{{1并且"."无法转换为"p" Long值,因此VBA会抛出此类型不匹配错误,说&#34;我不知道如何处理这个&#34;。

反过来运行两个替换:

Boolean

无关,但必须阅读