返回列表 上一主題 發帖

可否簡化 VBA 語法

可否簡化 VBA 語法

大家好,我寫一串語法
但自覺得過程太冗長,是否可以簡化程式語法


條件1.在A欄位,只要出現是"空白",就整列移除
條件2.在A欄位,只要出現這些英文,就整列移除 : TPD . TPD-both . TPD-viewing . GSA

Sub Step1()

On Error Resume Next
[A:A].SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
End Sub

Sub Step2()

Dim yy
yy = "TPD"
For I = [a65536].End(xlUp).Row To 1 Step -1
If WorksheetFunction.CountIf(Rows(I), yy) > 0 Then Rows(I).Delete
Next I
End Sub

Sub Step3()

Dim yy
yy = "TPD-both"
For I = [a65536].End(xlUp).Row To 1 Step -1
If WorksheetFunction.CountIf(Rows(I), yy) > 0 Then Rows(I).Delete
Next I
End Sub

Sub Step4()

Dim yy
yy = "TPD-viewing"
For I = [a65536].End(xlUp).Row To 1 Step -1
If WorksheetFunction.CountIf(Rows(I), yy) > 0 Then Rows(I).Delete
Next I
End Sub

Sub Step5()

Dim yy
yy = "GSA"
For I = [a65536].End(xlUp).Row To 1 Step -1
If WorksheetFunction.CountIf(Rows(I), yy) > 0 Then Rows(I).Delete
Next I
End Sub

在麻煩各位先進!
Just do it.

回復 22# Kubi

感謝你的教導!
Just do it.

TOP

回復 21# jsc0518
. 是用於標識子字串邊界的字串字元,以產生各字串所組成的一維陣列。
你也可以隨意定義區隔字串字元,例如改用逗號(,)也可以。
若改成逗號會像如下陳述:
Split("TPD,TPD-both,TPD-viewing,GSA", ",")

TOP

回復 18# Kubi


請問
1.這段語法 --> For Each x In Split("TPD.TPD-both.TPD-viewing.GSA", ".")
  內的 "." 是表是甚麼? 是不是移除A欄空白列?

Thanks!
Just do it.

TOP

回復 17# 准提部林


   了解,感謝您的回覆!
Just do it.

TOP

本帖最後由 ML089 於 2017-2-9 14:30 編輯

回復 13# jsc0518

最後一列 Range([A1], ActiveSheet.UsedRange).Rows.Count 參考14樓

Sub ex()
    For i = Range([A1], ActiveSheet.UsedRange).Rows.Count To 1 Step -1
        For Each x In Array("", "TPD", "TPD-both", "TPD-viewing", "GSA")
            If Cells(i, 1) = x Then Cells(i, 1).EntireRow.Delete
        Next
    Next
End Sub
{...} 表示需要用 CTRL+SHIFT+ENTER 三鍵輸入公式

TOP

不用範圍物件變數,應可加快執行速度。

Sub test()
    Dim d As Object
    Dim arr
    st = Timer
    Set d = CreateObject("Scripting.Dictionary")
    For Each x In Split("TPD.TPD-both.TPD-viewing.GSA", ".")
        d(x) = ""
    Next x
    arr = Range("A1:A" & ActiveSheet.UsedRange.Rows.Count)
    For i = 1 To UBound(arr)
        If d.exists(arr(i, 1)) Then arr(i, 1) = ""
    Next i
    [A1].Resize(UBound(arr), 1) = arr
    Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    Set d = Nothing
    arr = ""
    MsgBox Format(Timer - st, "0.0秒")
End Sub
[b]Kubi[/b]

TOP

回復 16# jsc0518


If xR = "" Or InStr(T, "_" & xR & "_") Then Set xE = Union(xE, xR)
就已包含"空白"及"關鍵字",
真正問題是, H欄最後一格與A欄不一致(較多),
所以只用A欄去檢測最後一格, 就漏了H欄,
用UsedRange可以包含所有已使用區塊,
但若前幾列是完全空白, 將漏抓這前幾列,
故, 用 Range([A1], UsedRange) 來函蓋整個區塊!

TOP

回復 14# 准提部林


Dear 准提部林
測試成功
請問哪一個語法是刪除A欄空白列阿?
Just do it.

TOP

其實准提部林大的已解決了, 這衹是另一方法:
  1. Sub zz()
  2. Application.ScreenUpdating = 0
  3. Dim x(), rng As Range, ar, Myr&
  4. x = Array("TPD", "TPD-both", "TPD-viewing", "GSA")
  5. Myr = IIf([a65536].End(3).Row > [h65536].End(3).Row, [a65536].End(3).Row, [a65536].End(3).Row)
  6. ar = Range("a1:a" & Myr)
  7. Set rng = Cells(Myr + 1, 1)
  8. For i = 1 To UBound(ar)
  9.     If Len(ar(i, 1)) = 0 Then
  10.         Set rng = Union(rng, Cells(i, 1))
  11.     Else
  12.         For j = 0 To UBound(x)
  13.             If InStr(ar(i, 1), x(j)) Then Set rng = Union(rng, Cells(i, 1))
  14.         Next
  15.     End If
  16. Next
  17. rng.EntireRow.Delete
  18. Application.ScreenUpdating = 1
  19. End Sub
複製代碼

TOP

        靜思自在 : 發脾氣是短暫的發瘋。
返回列表 上一主題