簡體   English   中英

Excel VBA:應用程序定義或對象定義的錯誤

[英]Excel VBA: Application defined or Object defined error

我已經編寫了一些代碼以在excel文件中查找方括號組,並清​​除它們之間單元格的內容。 在收到錯誤消息之前,我擁有的代碼適用於26-27行。

這是代碼:

Sub macro()
Dim white As Long
Dim rowIndex As Long
Dim colIndex As Long
Dim lastRow As Long
Dim lastCol As Long

white = RGB(Red:=255, Green:=255, Blue:=255)

With ActiveSheet

lastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
lastCol = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

For rowIndex = 1 To lastRow
        For colIndex = 1 To lastCol
            If .Cells(rowIndex, colIndex).Text = "[" Then
                colIndex = colIndex + 1
                Do While .Cells(rowIndex, colIndex).Value <> "]"
                    .Cells(rowIndex, colIndex).Font.Color = white
                    colIndex = colIndex + 1
                Loop
            End If
        Next colIndex
Next rowIndex
End With
End Sub

該行發生錯誤:

Do While Cells(rowIndex, colIndex).Value <> "]"

我嘗試添加:

With ActiveSheet

隨着 。 在每個Cell命令之前,但沒有任何區別。 任何幫助是極大的贊賞。

如果包含[]的單元格之一可能具有流氓前導尾隨空格/不間斷空格,則應進行通配符比較。 此外,工作表的“ 匹配”功能可以比使用逐行循環遍歷每個單元格更有效地使用通配符搜索來定位包圍式單元格。

Sub hide_cell_values()
    Dim whiteOut As String   '<~~ using alternate method .NumberFormat ;;;
    Dim rw As Long, n As Long, f As Long, l As Long

    whiteOut = ";;;"    'custom cell number format to show nothing in cell

    With ActiveSheet
        'process row by row in the .UsedRange
        With .Range(.Cells(1, 1), .Cells.SpecialCells(xlCellTypeLastCell))
            For rw = 1 To .Rows.Count
                ' check for existance of matching pairs
                If Not IsError(Application.Match("*[*", .Rows(rw), 0)) And _
                               Application.CountIf(.Rows(rw), "*[*") = _
                               Application.CountIf(.Rows(rw), "*]*") Then
                    ' [ and ] pairs exist and match in row.
                    f = 0: l = 0
                    For n = 1 To Application.CountIf(.Rows(rw), "*[*")
                        'this looks complicated but it just references the cells between [ & ]
                        f = Application.Match("*[*", .Rows(rw).Cells.Offset(0, l), 0) + l + 1
                        ' last safety check to ensure that [ comes before ]
                        If Not IsError(Application.Match("*]*", .Rows(rw).Cells.Offset(0, f), 0)) Then
                            l = Application.Match("*]*", .Rows(rw).Cells.Offset(0, f), 0) + f - 1
                            With .Range(.Cells(rw, f), .Cells(rw, l))
                                'this is a better method of not displaying text in a cell
                                .NumberFormat = whiteOut    '<~~ e.g. ;;;
                                'the old method of white-text-on-white-background (not reliable as .Interior.Color can change)
                                '.Font.Color = vbWhite
                            End With
                        End If
                    Next n
                Else
                    ' [ and ] pairs do not match or do not exist in row. do nothing.
                End If
            Next rw
        End With
    End With

End Sub

我選擇了自定義單元格格式;;; 而不是將字體顏色更改為RGB(255, 255, 255) (請參閱腳注¹)。 連續三個分號的Range.NumberFormat屬性僅顯示任何內容; 白色字體的明顯可見性取決於單元格的Range.Interior.Color屬性 ,計算機系統設置中的工作表背景 ,甚至是“窗口背景”。

隱藏子內容之前的單元格內容
在運行子之前

隱藏子內容后的單元格內容
跑完后

在上面的之前之后的圖像中,您可以看到D2保留其Range.Value屬性 (在編輯欄中可見),而在工作表上不顯示任何內容。 注意:仍然可以從不顯示任何內容的單元格復制單元格值,但這也說明了使用vbWhite方法。


¹ 基本VBA調色板有預定義的RGB長型常量。 RGB(255, 255, 255)等於vbWhite 完整列表可在“ 顏色常數”中找到

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM