2017-04-21 68 views
-1

我在單元格中搜索句號時出現類型不匹配「。」或「p」。當我只使用「。」時它有效。但添加「p」後,我得到一個類型不匹配。 my_txt的Variant/Empty是否只能使用整數和「。」s?我試圖讓我的過濾器使用。's或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 

回答

3

這是一個很大的不相關的代碼,但我設法挖這一個:

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

"." Or "p"表達是VBA不能評價。下面就是我們需要看到:

Debug.Print "." Or "p" 

該指令再現您遇到確切的問題:不匹配類型的錯誤。

Or是一個邏輯二進制運算符,當這樣使用時,計算結果爲TrueFalse。當作爲位運算符可以計算出一個Long使用,但在VBA確實隱式類型轉換,我們的很多,有它的限制,並".""p"不能轉換爲LongBoolean值,所以VBA會拋出這個類型不匹配錯誤,並說「我不知道如何處理這個」。

運行兩個替代sucessively代替:

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

無關,但必須閱讀