2017-02-08 22 views
1

我有大量的數據與一些混合了數字,句號和下劃線的單元格。但是,我想創建一個宏,它將刪除包含數字等的單元格,以便只留下包含字母表中的字母的單元格。以下是我現有的代碼,但無法正常工作。我如何解決它?如何根據一定數量的值刪除單元格?

Sub Sample() 
Dim ws As Worksheet 
Dim strSearch As String 
Dim Lrow As Long 


strSearch = "." 
strSearch = "0" 
strSearch = "1" 
strSearch = "2" 
strSearch = "3" 
strSearch = "4" 
strSearch = "5" 
strSearch = "6" 
strSearch = "7" 
strSearch = "8" 
strSearch = "9" 
strSearch = "." 


Set ws = Sheets("Sheet1") 

With ws 
    Lrow = .Range("A" & .Rows.Count).End(xlUp).Row 

    '~~> Remove any filters 
    .AutoFilterMode = False 

    '~~> Filter, offset(to exclude headers) and delete visible rows 
    With .Range("A1:A" & Lrow) 
     .AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*" 
     .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete 
    End With 

    '~~> Remove any filters 
    .AutoFilterMode = False 
End With 
End Sub 

我也有這個代碼不能正常工作。我應該使用哪一種,我該如何解決這些問題?另外,我應該使用哪一個?

Sub Test() 
Dim cell As Range 

For Each cell In Selection 
If InStr(1, cell, "1", vbTextCompare) > 0 Then 
    cell.EntireRow.Delete 
    End If 
Next 
For Each cell In Selection 
If InStr(1, cell, "2", vbTextCompare) > 0 Then 
    cell.EntireRow.Delete 
    End If 
Next 
For Each cell In Selection 
If InStr(1, cell, "3", vbTextCompare) > 0 Then 
    cell.EntireRow.Delete 
End If 
Next 
For Each cell In Selection 
If InStr(1, cell, "4", vbTextCompare) > 0 Then 
    cell.EntireRow.Delete 
End If 
Next 
For Each cell In Selection 
If InStr(1, cell, "5", vbTextCompare) > 0 Then 
    cell.EntireRow.Delete 
End If 
Next 
For Each cell In Selection 
If InStr(1, cell, "6", vbTextCompare) > 0 Then 
    cell.EntireRow.Delete 
End If 
Next 
For Each cell In Selection 
If InStr(1, cell, "7", vbTextCompare) > 0 Then 
    cell.EntireRow.Delete 
End If 
Next 
For Each cell In Selection 
If InStr(1, cell, "8", vbTextCompare) > 0 Then 
    cell.EntireRow.Delete 
End If 
Next 
For Each cell In Selection 
If InStr(1, cell, "9", vbTextCompare) > 0 Then 
    cell.EntireRow.Delete 
End If 
Next 
For Each cell In Selection 
If InStr(1, cell, "0", vbTextCompare) > 0 Then 
    cell.EntireRow.Delete 
End If 
Next 
For Each cell In Selection 
If InStr(1, cell, ".", vbTextCompare) > 0 Then 
    cell.EntireRow.Delete 
End If 
Next 
End Sub 
+0

您將需要***數組*** ***'strSearch' –

+0

這裏有一個很好的問題,只包括搜索字母字符(http://stackoverflow.com/questions/29633517/how-can-i-check - 如果-A-字符串只,包含-字母)。如果你願意重構,這幾乎肯定會簡化你的代碼。 – Joe

+0

加里 - 對不起,我對此非常有經驗,但我確實聽到陣列被提及很多。我將在何處以及如何整合它? – Imperdiet

回答

0

你可以試試這個:

Sub Sample() 
    Dim strSearch As Variant 

    strSearch = Array("*.*", "*0*", "*1*", "*2*", "*3*", "*4*", "*5*", "*6*", "*7*", "*8*", "*9*", "*_*") 
    With Sheets("Sheet01") 
     With .Range("A1", .Cells(.Rows.count, 1).End(xlUp)) 
      .AutoFilter Field:=1, Criteria1:=strSearch, Operator:=xlFilterValues 
      If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then .Offset(1).Resize(.Rows.count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete 
     End With 
     .AutoFilterMode = False 
    End With 
End Sub 
+0

不是,它不工作:( – Imperdiet

+0

太模糊的句子...更詳細地描述什麼是不詳細的工作 – user3598756

+0

我跑了宏,但沒有發生任何事情,但我沒有收到任何錯誤信息 – Imperdiet

0

這取決於你希望用這個宏來完成的。下面的宏將滿足你在找什麼:

Sub CleanNumerics() 
Application.ScreenUpdating = False 

Dim ws As Worksheet 
Dim r As Range 
Dim cell As Range 

Dim i As Long 
Dim j As Long 

Dim args() As Variant 

' Load your arguments into an array to allow looping 
args() = Array(".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "_") 

' Load your selection into a range variable 
Set r = Selection 

' By stepping backwards we wont skip cells as we delete rows. 
For i = r.Cells.Count To 1 Step -1 
    ' Loop through the number of arguments in our array. 
    For j = 0 To UBound(args()) 
     ' If one of the noted characters is in the cell, the row 
     ' is deleted and the loop exits. 
     If InStr(1, r.Cells(i), args(j)) > 0 Then 
      r.Cells(i).EntireRow.Delete 
      Exit For 
     End If 
    Next 
Next 


End Sub 

這種方法的問題是,您要刪除整個行可能導致取決於您的應用程序的問題。此外,如果您使用大型數據集進行此操作,則可能需要很長時間。你可以使用數組來克服這個問題,但這些可能會變得複雜。

與數組做它會是這個樣子:

Sub ArrayWithoutNumbers() 
Application.ScreenUpdating = False 

Dim ws As Worksheet 
Dim r As Range 
Dim cell As Range 

Dim i As Long 
Dim j As Long 
Dim k As Long 
Dim m As Long 

Dim args() As Variant 

Dim array_1() As Variant 
Dim array_2() As Variant 

Dim flag As Boolean 

' Load your arguments into an array to allow looping 
args() = Array(".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "_") 

' Load your selection into a range variable 
On Error GoTo Err 
array_1() = Selection.Value 
On Error GoTo 0 

' First determine if a two dimensional array has created. If so, loop through rows 
' and columns. If not, go to the other loop. 
If UBound(array_1, 2) > 1 Then 
    For i = 1 To UBound(array_1, 1) 
     For j = 1 To UBound(array_1, 2) 
      flag = False 
      For k = 0 To UBound(args()) 
       If InStr(1, array_1(i, j), args(k)) > 0 Then 
        flag = True ' Sets a flag so that the item is not added. 
        Exit For ' Exit the loop 
       End If 
      Next 

      ' If the flag hasn't been raised, resize the array and add the item. 
      If flag = False Then 
       m = m + 1 
       ReDim Preserve array_2(1 To m) 
       array_2(m) = array_1(i, j) 
      End If 
     Next 
    Next 

' Loops through only the rows of the array. 

ElseIf UBound(array_1, 2) = 1 Then 
    For i = 1 To UBound(array_1, 1) 
     For k = 0 To UBound(args()) 
      If InStr(1, array_1(i), args(k)) > 0 Then 
       flag = True 
       Exit For 
      End If 
     Next 
     If flag = False Then 
      m = m + 1 
      ReDim Preserve array_2(1 To m) 
      array_2(m) = array_1(i) 
     End If 
    Next 
End If 

' Adds a worksheet to output to. You can adjust this as needed. 

ActiveWorkbook.Sheets.Add 
ActiveSheet.Range("A1").Resize(UBound(array_2, 1), 1).Value = array_2() 

Exit Sub 

Err: 

End Sub 

的好處,這是你可以一次清理多行和列,吐了回去。

+0

噢,我的天哪,這是一個終於有效的工作!謝謝!需要一段時間,但我不介意,因爲它實際上工作! !:D – Imperdiet

+0

不是問題!根據你在做什麼,任何循環都會花費一些時間,特別是當你選擇更大的範圍時,需要記住的一點是把你的值加載到內存中(就像我對數組做的那樣)比試圖編輯工作更快等編輯工作表之後,還會附加其他事件。 –

相關問題