簡體   English   中英

用VBA填補Excel中的空白

[英]Fill in blanks in excel with vba

我在excel中使用大型數據集,其邊界可能隨着更新而變化。 我需要一種自動化的方法來用占位符(例如“ n / a”)填充所有空白單元格。 有快速的方法嗎? 謝謝

您將需要遍歷您范圍內的單元格,並且無論何時遇到空白,您都需要以下代碼

' e.g. you need to make cell A2 read #N/A, i.e. the error value
ActiveSheet.Range("A2").Value = CVErr(xlErrNA)

如果只需要輸入字符串“ N / A”而不是錯誤函數 =NA()的等效項,請查看Gary's Student提供的代碼。

怎么樣:

Sub NoBlanka()
    For Each r In Selection
        If r.Text = "" Then
            r.Value = "n/a"
        End If
    Next r
End Sub

選擇您的單元格組並運行宏。

並非100%明確要求,但可以嘗試一下。 應將其輸入到工作表代碼模塊中,並將注釋的工作表名稱更改為您在工作表中使用的任何名稱。

Private Sub Worksheet_Change(ByVal Target As Range)

    Static r As Range

    With Worksheets("mySheet")

        If .UsedRange Is Nothing Then Exit Sub

        If r Is Nothing Then
            Set r = .UsedRange
            On Error Resume Next
            r.SpecialCells(xlBlanks).Value = CVErr(xlErrNA)
            On Error GoTo 0
            Exit Sub
        End If

        On Error GoTo ERREUR
        If r.Address <> .UsedRange.Address Then
            On Error goto 0
            Set r = .UsedRange
            On Error Resume Next
            r.SpecialCells(xlBlanks).Value = CVErr(xlErrNA)
            On Error GoTo 0
        End If

    End With

ERREUR:

Set r = Nothing

End Sub

不知道這是否是您所需要的。 我在Excel中使用了宏錄制器。

Sub Macro1()
    Cells.Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.FormulaR1C1 = "na"
End Sub

您甚至不需要vba即可執行此操作。 您可以使用特殊單元格。

在功能區的“主頁”選項卡中,單擊“查找並選擇”(在功能區右側的“編輯”部分中),然后選擇“轉到特殊位置”。

選擇空白單元格。 那好吧 這將選擇所有空白單元格。

現在鍵入=和您的占位符。 因此,對於n / a,您只需鍵入='n / a

暫無
暫無

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

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