Trovare e la rimozione di alcuni caratteri in una colonna di dati in Excel
-
06-09-2019 - |
Domanda
Ho copiato e incollato alcune informazioni di debug in un foglio di Excel.
Tuttavia, contiene alcuni caratteri "strano" in alcune celle di una colonna, che dovrebbe altrimenti contenere solo numeri interi. Quale sarebbe il modo più semplice per eliminare tali personaggi utilizzando VBA? Un esempio è mostrato nella lista qui sotto:
1 **'␁'** <- I'm trying to get rid of the part that I have bolded
2 '␂'
3 '␃'
4 '␂'
Voglio utilizzare il file come origine dati in un'altra applicazione. Grazie in anticipo.
Soluzione 3
Ecco una soluzione che ha funzionato per me, anche se non può essere molto elegante:
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
Altri suggerimenti
Prova questo (codice distacco prima volta qui, quindi per favore abbiate pazienza con me; o). Credo che ho commentato il codice VBA abbastanza, ma tutte le domande, fammelo sapere.
' 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
Credo che le espressioni regolari potrebbe essere più semplice. Vorrei utilizzare un recordset ADO e guardare attraverso questo.
Alcune note su 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, "-")
Alcune note su ADO e 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'
Come si esegue il debug - Sei davvero sicuro di voler rimuovere loro? Sono caratteri di controllo ASCII. Ma poi di nuovo non so che cosa si esegue il debug ....
I personaggi che si vedono sono i caratteri Unicode che rappresentano il carattere di controllo ASCII - così, ovunque aver copiato i dati ha tradotto per voi
.funzione standard di Excel Clean è progettato rimuovere i caratteri di controllo ASCII lavoro così abituato qui.
Ma questa volontà, rimuove tutti i caratteri Unicode nelle immagini controllo della gamma
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
...