使用vba填写Excel中的空白

时间:2014-07-14 14:08:29

标签: vba excel-vba excel

我正在使用excel中的大型数据集,其边界可能随更新而变化。 我需要一种自动方法,用一个占位符来填充所有空白单元格,例如' n / a'。 有快速的方法吗? 感谢

5 个答案:

答案 0 :(得分:1)

您需要遍历您所在范围内的细胞以及遇到空白的地方,您需要以下代码

' 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()的等效字符看一下加里学生提供的代码。

答案 1 :(得分:0)

怎么样:

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

选择您的单元格组并运行宏。

答案 2 :(得分:0)

不是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

答案 3 :(得分:0)

不确定这是否是您所需要的。我在Excel中使用了宏录制器。

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

答案 4 :(得分:0)

你甚至不需要vba来做这件事。您可以使用特殊单元格。

在功能区的“主页”选项卡中,单击“查找并选择”(在功能区右侧的“编辑”部分中),然后选择“转到特殊”

选择空白单元格。那好吧这将选择所有空白单元格。

现在输入=和你的占位符。因此,对于n / a,您只需输入=&#39; n / a