在 Excel 中查找并删除数据列中的某些字符

发布于 2024-07-21 22:26:30 字数 280 浏览 4 评论 0原文

我已将一些调试信息复制并粘贴到 Excel 工作表中。

但是,它在一列的某些单元格中包含一些“奇怪”的字符,否则这些字符应该只包含整数。 使用 VBA 消除此类字符的最简单方法是什么? 下面的列表中显示了一个示例:

1 **'␁'** <- I'm trying to get rid of the part that I have bolded
2 '␂'
3 '␃'
4 '␂'

我想在另一个应用程序中使用该文件作为数据源。 提前致谢。

I have copied and pasted some debugging information into an Excel sheet.

However, it contains some "weird" characters in some cells of one column, that should otherwise contain integers only. What would be the easiest way to eliminate such characters using VBA? An example is shown in the list below:

1 **'␁'** <- I'm trying to get rid of the part that I have bolded
2 '␂'
3 '␃'
4 '␂'

I want to use the file as a data source in another application. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

满天都是小星星 2024-07-28 22:26:30

试试这个(第一次在这里发布代码,所以请耐心等待;o)。 我相信我对 VBA 代码的注释已经足够了,但如有任何问题,请告诉我。

' Replaces all the charaters in vaFind with strReplace
Sub FindAndReplace(ByVal vaFind As Variant, ByVal strReplace As String, ByVal rngToSearch As Range)
' vaFind is an array containing all the strings you want to replace
' strReplace is what you want to replace it with
' rngToSearch is the range you want to Find & Replace your characters
Dim x As Long
For x = LBound(vaFind, 1) To UBound(vaFind, 1)

  rngToSearch.Cells.Replace What:=vaFind(x), Replacement:=strReplace, _
      LookAt:=xlPart, SearchOrder:=xlByRows, _
      MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next x

End Sub

' Now if you want to clean it automatically,
' Place the following code INTO any Sheets that you
' are have the debug data placed into.
' NOTE: prefix Asterick and question mark with a tilde to replace those characters "~*"
Private Sub Worksheet_Change(ByVal Target As Range)
' Calls the FindAndReplace sub, and removes all:
' astericks, apostrophes and "Whatever Else You need cleaned"'s
' In this case, in column A
If Not Intersect(Target, Me.Columns("A:A")) Is Nothing Then
Call FindAndReplace(Array("~*", "'", "Whatever Else You need cleaned"), "", Me.Columns("A:A"))
End If
' NOTE: This sub will be called whenever the sheet changes, and only process column A
' You can customize which columns, too.
End Sub    

Try this (first time posting code here, so please bear with me;o). I believe I commented the VBA code enough, but any questions, just let me know.

' Replaces all the charaters in vaFind with strReplace
Sub FindAndReplace(ByVal vaFind As Variant, ByVal strReplace As String, ByVal rngToSearch As Range)
' vaFind is an array containing all the strings you want to replace
' strReplace is what you want to replace it with
' rngToSearch is the range you want to Find & Replace your characters
Dim x As Long
For x = LBound(vaFind, 1) To UBound(vaFind, 1)

  rngToSearch.Cells.Replace What:=vaFind(x), Replacement:=strReplace, _
      LookAt:=xlPart, SearchOrder:=xlByRows, _
      MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next x

End Sub

' Now if you want to clean it automatically,
' Place the following code INTO any Sheets that you
' are have the debug data placed into.
' NOTE: prefix Asterick and question mark with a tilde to replace those characters "~*"
Private Sub Worksheet_Change(ByVal Target As Range)
' Calls the FindAndReplace sub, and removes all:
' astericks, apostrophes and "Whatever Else You need cleaned"'s
' In this case, in column A
If Not Intersect(Target, Me.Columns("A:A")) Is Nothing Then
Call FindAndReplace(Array("~*", "'", "Whatever Else You need cleaned"), "", Me.Columns("A:A"))
End If
' NOTE: This sub will be called whenever the sheet changes, and only process column A
' You can customize which columns, too.
End Sub    
彼岸花ソ最美的依靠 2024-07-28 22:26:30

我认为正则表达式可能是最简单的。 我将使用 ADO 记录集并查看它。

关于 RegEx 的一些注释

''http://msdn.microsoft.com/en-us/library/ms974570.aspx
Dim objRegEx As RegExp
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True

''This is a sample string, the field would go here
strText = "q@12""£c" 

''Find anything not a-z, 0-9
objRegEx.Pattern = "[^a-z0-9]"

''Replace with a dash, "" is fine too. 
strOut = objRegEx.Replace(strText, "-")

关于 ADO 和 Excel 的一些注释

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

strFile = ActiveWorkbook.FullName

strcn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& strFile & ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';"

cn.Open strcn

rs.Open "SELECT * FROM [Sheet1$]", cn, 3 'adOpenStatic'

I think regular expressions might be easiest. I would use an ADO recordset and look through that.

A few notes on RegEx

''http://msdn.microsoft.com/en-us/library/ms974570.aspx
Dim objRegEx As RegExp
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True

''This is a sample string, the field would go here
strText = "q@12""£c" 

''Find anything not a-z, 0-9
objRegEx.Pattern = "[^a-z0-9]"

''Replace with a dash, "" is fine too. 
strOut = objRegEx.Replace(strText, "-")

A few notes on ADO and Excel

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

strFile = ActiveWorkbook.FullName

strcn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& strFile & ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';"

cn.Open strcn

rs.Open "SELECT * FROM [Sheet1$]", cn, 3 'adOpenStatic'
腹黑女流氓 2024-07-28 22:26:30

这是一个对我有用的解决方案,尽管它可能不是很优雅:

Sub Macro1()
    ' Macro1 Macro
    Dim temp As String

    For u = 1 To 28000
        If Cells(u, 4) <> 0 Then
            Cells(u, 4).Select
            temp = Mid(ActiveCell.Text, 1, InStr(1, ActiveCell.Text, "'") - 1)
            Cells(u, 4) = temp
        End If
    Next
End Sub

Here's a solution that worked for me, although it may not be very elegant:

Sub Macro1()
    ' Macro1 Macro
    Dim temp As String

    For u = 1 To 28000
        If Cells(u, 4) <> 0 Then
            Cells(u, 4).Select
            temp = Mid(ActiveCell.Text, 1, InStr(1, ActiveCell.Text, "'") - 1)
            Cells(u, 4) = temp
        End If
    Next
End Sub
疯狂的代价 2024-07-28 22:26:30

当您调试时 - 您真的确定要删除它们吗? 它们是 ASCII 控制字符。 但话又说回来,我不知道你在调试什么......

你看到的字符是代表 ascii 控制字符的 unicode 字符 - 所以无论你从哪里复制数据,都已经为你翻译了它。

标准 Excel 函数 Clean 旨在删除 ASCII 控制字符,因此在这里不起作用。

但这会删除 CONTROL PICTURES 范围内的所有 unicode 字符

Sub Macro1()
  ' Macro1 Macro
  '
  For u = 9210 To 9216 
     a = Cells.Replace(ChrW(u), "") ' replaces values in whole Worksheet
  Next
End Sub

' use this to replace the values in a single column only
' i cant test this at the moment as i don't have Excel handy...
...
   a = Range("A1:A2800").Replace(ChrW(u), "") ' replaces values in Col A
...

As you are debugging - Are you really sure you want to remove them? They are ASCII control characters. But then again i don't know what you are debugging....

The characters you are seeing are unicode characters that represent the ascii control character - so wherever you have copied the data from has translated it for you.

standard excel function Clean is designed remove the ASCII control characters so wont work here.

But this will, it removes all the unicode characters in the CONTROL PICTURES range

Sub Macro1()
  ' Macro1 Macro
  '
  For u = 9210 To 9216 
     a = Cells.Replace(ChrW(u), "") ' replaces values in whole Worksheet
  Next
End Sub

' use this to replace the values in a single column only
' i cant test this at the moment as i don't have Excel handy...
...
   a = Range("A1:A2800").Replace(ChrW(u), "") ' replaces values in Col A
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文