Vba 如何将所选内容中的所有空单元格转换为空值?

Vba 如何将所选内容中的所有空单元格转换为空值?,vba,excel,Vba,Excel,我有一个选择,它可以是任何随机范围,例如(“A1:O10”) 我需要确保此范围内所有空白单元格的值均为NULL 我该怎么做 For Each c In Selection.Cells If Abs(c.Value) = "" Then c.Value = "NULL" Next 这应该能奏效 Sub BlankToNull() For Each cell In Selection If Not IsError(cell.Value) Then

我有一个
选择
,它可以是任何随机范围,例如
(“A1:O10”)

我需要确保此范围内所有空白单元格的值均为
NULL

我该怎么做

For Each c In Selection.Cells
        If Abs(c.Value) = "" Then c.Value = "NULL"
Next

这应该能奏效

Sub BlankToNull()
    For Each cell In Selection
        If Not IsError(cell.Value) Then
            If cell.Value = vbNullString Then cell.Value = "NULL"
        End If
    Next cell
End Sub

这应该能奏效

Sub BlankToNull()
    For Each cell In Selection
        If Not IsError(cell.Value) Then
            If cell.Value = vbNullString Then cell.Value = "NULL"
        End If
    Next cell
End Sub

编辑:是作为一个答案提出的,但这里只讨论如何去做(基于simoco对特殊细胞和UsedRange的观察)

修复了“特殊单元格”otchas:

  • 特殊单元格仅在工作表的UsedRange范围内工作(即使您选择的单元格超出该范围)
  • SpecialCells不适用于单个单元格选择:在这种情况下,它将自动扩展到整个工作表
假设选择单个区域:

With Selection
    With .Cells(.Cells.Count)
        If Not .HasFormula And Len(.Value) = 0 Then
            .Value = "NULL"
        End If
    End With
    If .Cells.Count = 1 Then Exit Sub  
    On Error Resume Next
    .SpecialCells(xlCellTypeBlanks).Value = "NULL"
    On Error GoTo 0
End With
来自Siddharth Rout:另一种不使用循环的方法

Sub Sample()
    With ActiveSheet
        '~~> How like is that the bottom right cell is filled????
        '~~> Excel 2007+ - XFD1048576
        '~~> Excel 2003 - IV65536
        .Cells(.Rows.Count, .Columns.Count) = "A"

        If Selection.Cells.Count < 2 Then
            If Selection.Value = "" Then Selection.Value = "NULL"
        Else
            On Error Resume Next
            Selection.SpecialCells(xlCellTypeBlanks).Value = "NULL"
            On Error GoTo 0
        End If
        .Cells(.Rows.Count, .Columns.Count).ClearContents
    End With
End Sub
子样本()
使用ActiveSheet
“~~>右下角的单元格是如何填充的????
'~~>Excel2007+-XFD1048576
'~~>Excel2003-IV65536
.Cells(.Rows.Count、.Columns.Count)=“A”
如果Selection.Cells.Count小于2,则
如果Selection.Value=“”则Selection.Value=“NULL”
其他的
出错时继续下一步
Selection.SpecialCells(xlCellTypeBlanks).Value=“NULL”
错误转到0
如果结束
.Cells(.Rows.Count、.Columns.Count).ClearContents
以
端接头

编辑:是作为一个答案而提出的,但这里只讨论如何而不是如何做到这一点(基于simoco对特殊细胞和UsedRange的观察)

修复了“特殊单元格”otchas:

  • 特殊单元格仅在工作表的UsedRange范围内工作(即使您选择的单元格超出该范围)
  • SpecialCells不适用于单个单元格选择:在这种情况下,它将自动扩展到整个工作表
假设选择单个区域:

With Selection
    With .Cells(.Cells.Count)
        If Not .HasFormula And Len(.Value) = 0 Then
            .Value = "NULL"
        End If
    End With
    If .Cells.Count = 1 Then Exit Sub  
    On Error Resume Next
    .SpecialCells(xlCellTypeBlanks).Value = "NULL"
    On Error GoTo 0
End With
来自Siddharth Rout:另一种不使用循环的方法

Sub Sample()
    With ActiveSheet
        '~~> How like is that the bottom right cell is filled????
        '~~> Excel 2007+ - XFD1048576
        '~~> Excel 2003 - IV65536
        .Cells(.Rows.Count, .Columns.Count) = "A"

        If Selection.Cells.Count < 2 Then
            If Selection.Value = "" Then Selection.Value = "NULL"
        Else
            On Error Resume Next
            Selection.SpecialCells(xlCellTypeBlanks).Value = "NULL"
            On Error GoTo 0
        End If
        .Cells(.Rows.Count, .Columns.Count).ClearContents
    End With
End Sub
子样本()
使用ActiveSheet
“~~>右下角的单元格是如何填充的????
'~~>Excel2007+-XFD1048576
'~~>Excel2003-IV65536
.Cells(.Rows.Count、.Columns.Count)=“A”
如果Selection.Cells.Count小于2,则
如果Selection.Value=“”则Selection.Value=“NULL”
其他的
出错时继续下一步
Selection.SpecialCells(xlCellTypeBlanks).Value=“NULL”
错误转到0
如果结束
.Cells(.Rows.Count、.Columns.Count).ClearContents
以
端接头

ABS(c值)?你是说修剪(c.value)还是空(c.value)对吗?ABS(c.value)?你是说TRIM(c.value)还是isempty(c.value)对吗?当选择中的任何单元格包含错误(如#N/a或#DIV/0!)时,它将触发类型不匹配错误。您还应该添加检查:
如果不是iError(cell),那么…
如果使用iEmpty(cell)@simoco,则不需要该检查。我已经更新了代码以考虑到这一点。如果isempty(cell),那么cell.value=“NULL”-您不需要进行测试iserror@CRondao:如果您认为这也是一种很好的方法,请将您的评论也作为答案发表,这是许多方法中可能的一种。:)当选择中的任何单元格包含错误(如#N/a或#DIV/0!)时,它将触发类型不匹配错误。您还应该添加检查:
如果不是iError(cell),那么…
如果使用iEmpty(cell)@simoco,则不需要该检查。我已经更新了代码以考虑到这一点。如果isempty(cell),那么cell.value=“NULL”-您不需要进行测试iserror@CRondao:如果您认为这也是一种很好的方法,请将您的评论也作为答案发表,这是许多方法中可能的一种。:)Tim,我已经测试过这种方法,不幸的是它只适用于UsedRange中的单元格。试着选择,比如说
A1000:B10000(范围在UsedRange之外),然后计算宏。行nothing@SiddharthRout-没问题-继续+1,因为这比循环快得多!右下角的单元格不太可能被填满。至少到目前为止我还没有见过这种情况。蒂姆,我已经测试过这种方法,不幸的是它只适用于UsedRange的单元格。试着选择,比如说
A10000:B10000
(范围超出UsedRange),然后计算宏。行nothing@SiddharthRout-没问题-继续+1,因为这比循环快得多!右下角的单元格不太可能被填满。至少到目前为止我还没见过这种情况。