从列中删除逗号,空格和“NULL”字符串为0(零)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从列中删除逗号,空格和“NULL”字符串为0(零)相关的知识,希望对你有一定的参考价值。

我是Excel VBA的新手。

需要帮助从ColumnName中删除逗号,空格和NULL字符串为“0”,称为StringName。

首先,我尝试从名为StringName的ColumnName中删除逗号,空格,最后查找并将“NULL”字符串替换为0(零)。

下面是替换为逗号的代码,ColumnName中的空格称为StringName。

Sub ReplaceCharacters()

  Application.ScreenUpdating = False
    Dim lrow As Long
    lrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
  With ActiveSheet.Range("A2:A" & lrow).SpecialCells(xlCellTypeConstants, xlTextValues).Cells
      .Replace What:=",", Replacement:="", LookAt:=xlPart
      .Replace What:=" ", Replacement:="", LookAt:=xlPart
  End With

  Application.ScreenUpdating = True

End Sub

我正在努力寻找并从名为StringName的ColumnName中将字符串“NULL”替换为0。

我需要这方面的帮助,我已经尝试了很多,最终在这里寻求解决方案。

这是我试过的..

' not working
Sub UpdateWhole()
    With ActiveSheet.UsedRange
    .Replace "NULL", "0", xlWhole
    End With
End Sub

' not working
Sub FormulaRng()
    For i = 2 To 10
       Worksheets("Sheet1").Range("A2:A" & i).FormulaR1C1 = "=IF(A" & i & "=""NULL"",0,A" & i & ")"
    Next
End Sub

提前致谢

screenshot

以下是要测试的示例数据

StringName
------------------
NULL
NULL  
null 
nullasdf
cbgrgNULLdf343
, asdfwe 4fdt
456fg , d55nullNULL
sdf34      df,    4fd   
NULLfgf  null
121
22
34545

Required OutPut
------------------------
0
0
0
nullasdf
cbgrgNULLdf343
asdfwe4fdt
456fgd55nullNULL
sdf34df4fd
NULLfgfnull
121
22
34545
答案

以下修订过程应该适合您。在用Clean替换NULL文本之前,我使用0函数从文本中删除所有不可打印的字符。

VBA Code

Public Sub ReplaceCharacters()
On Error GoTo ErrTrap
Const ProcedureName As String = "ReplaceCharacters"
Dim sheet           As Worksheet: Set sheet = ActiveSheet
Dim cell            As Range

    Application.ScreenUpdating = False

    For Each cell In sheet.UsedRange.Cells
        cell = Trim(Application.WorksheetFunction.Clean(cell))
        cell.Replace What:="NULL", Replacement:=0, LookAt:=xlWhole
        cell.Replace What:=",", Replacement:="", LookAt:=xlPart
        cell.Replace What:=" ", Replacement:="", LookAt:=xlPart
    Next cell

ExitProcedure:
    On Error Resume Next
    Application.ScreenUpdating = True
    Set sheet = Nothing
    Exit Sub

ErrTrap:
    Select Case Err.number
        Case Else
            Debug.Print "Procedure: " & ProcedureName & " |Error #: " & Err.number & " |Error Description: " & Err.description
    End Select
    Resume ExitProcedure
    Resume 'for debugging

End Sub

Example Video

screenshot

另一答案

试试这个。

Sub test()
    Dim Ws As Worksheet
    Dim rngDB As Range

    Set Ws = ActiveSheet
    With Ws
        Set rngDB = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
    End With
    With rngDB
        .Replace What:=",", Replacement:="", LookAt:=xlPart
        .Replace What:=" ", Replacement:="", LookAt:=xlPart
        .Replace What:="NULL", Replacement:="0", LookAt:=xlWhole
        .Replace What:="null", Replacement:="0", LookAt:=xlWhole
    End With


End Sub

以上是关于从列中删除逗号,空格和“NULL”字符串为0(零)的主要内容,如果未能解决你的问题,请参考以下文章

根据相邻列值 Pandas 从列中按第一个或第二个空格提取字符串

使用 SSIS 从列中删除特殊字符 " 和 '

SQL 从列中删除字符

从列中的字符串中提取每行的最大值

从列中删除字符串组合

如何从列中删除多余的字符?