首页 文章

使用vba填写Excel中的空白

提问于
浏览
0

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

5 回答

  • 0

    您需要遍历范围内的单元格以及遇到空白的任何位置,您需要以下代码

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

    如果您只需要输入 the string "N/A" 而不是相应的 error function =NA() ,请查看Gary的学生提供的代码 .

  • 0

    怎么样:

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

    Select 您的单元格组并运行宏 .

  • 1

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

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

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

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

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

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

    现在输入=和你的占位符 . 因此,对于n / a,您只需键入='n / a

相关问题