0

いくつかのデバッグ情報をコピーして Excel シートに貼り付けました。

ただし、1 つの列の一部のセルに「奇妙な」文字が含まれており、それ以外の場合は整数のみを含む必要があります。VBA を使用してそのような文字を削除する最も簡単な方法は何でしょうか? 以下のリストに例を示します。

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

このファイルを別のアプリケーションでデータ ソースとして使用したいと考えています。前もって感謝します。

4

4 に答える 4

1

これを試してみてください(ここに初めてコードを投稿するので、我慢してください; 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    
于 2009-05-31T19:16:21.657 に答える
0

正規表現が一番簡単だと思います。ADO レコードセットを使用して、それを調べます。

正規表現に関するいくつかの注意事項

''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'
于 2009-05-29T08:12:51.653 に答える
0

あまりエレガントではないかもしれませんが、これは私のために働いた解決策です:

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
于 2009-05-29T22:00:51.477 に答える
-1

デバッグ中 - それらを削除してもよろしいですか? これらは 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
...
于 2009-05-29T09:16:42.570 に答える