2017-08-02 77 views
0

問題:刪除當前行VBA

我需要刪除不在我的條件的行。

代碼:

假定所有的值被初始化。

For ind = 2 To lrow 
    reportProcess = Trim(LTrim(RTrim(ws.Range("G" & ind).Text))) 
    If reportProcess = "Fulfillment - H/W Direct Customers (Operational)" Or reportProcess = "Revenue - Hardware" Then 
     brand = "HW" 
    ElseIf reportProcess = "Revenue - Software" Then 
     ' ~~ Check Control Point Number 
     subReportProcess = Mid(ws.Range("I" & ind).Text, 12, 3) 
     If subReportProcess = "201" Or subReportProcess = "202" Or subReportProcess = "203" Or subReportProcess = "204" Or subReportProcess = "205" Then 
      brand = "PBS" 
     Else 
      brand = "SWG" 
     End If 
    ElseIf reportProcess = "Revenue - GBS" Or reportProcess = "Revenue - GTS IS" Or reportProcess = "Fulfillment - Services(Operational)" Then 
     brand = "PBS" 
    ElseIf reportProcess = "Revenue - TSS" Then 
     brand = "TSS" 
    ElseIf reportProcess = "Accounts Receivable" Or reportProcess = "IBM Credit LLC - Accounts Receivable" Then 
     brand = "AR" 
    End If 

    country = Trim(LTrim(RTrim(ws.Range("V" & ind).Text))) 
    If country = "Taiwan" Then 
     geo = "Taiwan" 
    ElseIf country = "India" Then 
     geo = "India" 
    ElseIf country = "New Zealand" Or country = "Australia" Then 
     geo = "ANZ" 
    ElseIf country = "Hong Kong" Then 
     geo = "Hong Kong" 
    ElseIf country = "Philippines" Or country = "Malaysia" Or country = "Singapore" Or country = "Thailand" Or country = "Vietnam" Or country = "Indonesia" Then 
     geo = "ASEAN" 
    Else 
    ' ~~ INSERT DELETE ROW HERE 
    End If 
    ws.Range("B" & ind) = geo 
    ws.Range("A" & ind) = brand 
Next ind 

我的嘗試:

ActiveCell.EntireRow.Delete - 不過這將刪除所有行

Selection.EntireRow.Delete - 如上

我如何使用循環來刪除特定的行號相同的結果?

+0

你想刪除哪一行? –

+0

@RachelChia循環中當前的一個,所以如果'ind = 2'且條件不匹配,請將其刪除。 – Hibari

+0

你想刪除整個第二行或第一列第二行?如果你想刪除特定的行例子:ws.Range(「A」&ind)。刪除或整行:行(ind).EntireRow.Delete –

回答

2

您將需要遍歷列表從最後一行到第一行。如果你不這樣做,你會跳過刪除行後的下一行。注意:這也適用於從列表框,組合框和集合中刪除項目。

For ind = lrow To 2 Step -1 
    reportProcess = Trim(LTrim(RTrim(ws.Range("G" & ind).Text))) 
    If reportProcess = "Fulfillment - H/W Direct Customers (Operational)" Or reportProcess = "Revenue - Hardware" Then 
     brand = "HW" 
    ElseIf reportProcess = "Revenue - Software" Then 
     ' ~~ Check Control Point Number 
     subReportProcess = Mid(ws.Range("I" & ind).Text, 12, 3) 
     If subReportProcess = "201" Or subReportProcess = "202" Or subReportProcess = "203" Or subReportProcess = "204" Or subReportProcess = "205" Then 
      brand = "PBS" 
     Else 
      brand = "SWG" 
     End If 
    ElseIf reportProcess = "Revenue - GBS" Or reportProcess = "Revenue - GTS IS" Or reportProcess = "Fulfillment - Services(Operational)" Then 
     brand = "PBS" 
    ElseIf reportProcess = "Revenue - TSS" Then 
     brand = "TSS" 
    ElseIf reportProcess = "Accounts Receivable" Or reportProcess = "IBM Credit LLC - Accounts Receivable" Then 
     brand = "AR" 
    End If 

    country = Trim(LTrim(RTrim(ws.Range("V" & ind).Text))) 
    If country = "Taiwan" Then 
     geo = "Taiwan" 
    ElseIf country = "India" Then 
     geo = "India" 
    ElseIf country = "New Zealand" Or country = "Australia" Then 
     geo = "ANZ" 
    ElseIf country = "Hong Kong" Then 
     geo = "Hong Kong" 
    ElseIf country = "Philippines" Or country = "Malaysia" Or country = "Singapore" Or country = "Thailand" Or country = "Vietnam" Or country = "Indonesia" Then 
     geo = "ASEAN" 
    Else 
     ' ~~ INSERT DELETE ROW HERE 
     ws.Rows(ind).Delete Shift:=xlUp 
    End If 
    ws.Range("B" & ind) = geo 
    ws.Range("A" & ind) = brand 
Next ind 

我重構每@ShaiRado諮詢OP的代碼。

For ind = lrow To 2 Step -1 
    reportProcess = Trim(LTrim(RTrim(ws.Range("G" & ind).Text))) 
    Select Case reportProcess 
    Case "Fulfillment - H/W Direct Customers (Operational)", "Revenue - Hardware" 
     brand = "HW" 
    Case "Revenue - Software" 
     ' ~~ Check Control Point Number 
     subReportProcess = Mid(ws.Range("I" & ind).Text, 12, 3) 
     If subReportProcess = "201" Or subReportProcess = "202" Or subReportProcess = "203" Or subReportProcess = "204" Or subReportProcess = "205" Then 
      brand = "PBS" 
     Else 
      brand = "SWG" 
     End If 
    Case "Revenue - GBS", "Revenue - GTS IS", "Fulfillment - Services(Operational)" 
     brand = "PBS" 
    Case "Revenue - TSS" 
     brand = "TSS" 
    Case "Accounts Receivable", "IBM Credit LLC - Accounts Receivable" 
     brand = "AR" 
    End Select 

    country = Trim(LTrim(RTrim(ws.Range("V" & ind).Text))) 
    Select Case country 
    Case "Taiwan" 
     geo = "Taiwan" 
    Case "India" 
     geo = "India" 
    Case "New Zealand", "Australia" 
     geo = "ANZ" 
    Case "Hong Kong" 
     geo = "Hong Kong" 
    Case "Philippines", "Malaysia", "Singapore", "Thailand", "Vietnam", "Indonesia" 
     geo = "ASEAN" 
    Case Else 
     ' ~~ INSERT DELETE ROW HERE 
     ws.Rows(ind).Delete Shift:=xlUp 
    End Select 

    ws.Range("B" & ind) = geo 
    ws.Range("A" & ind) = brand 
Next ind 
+0

哦,我明白了,所以'行(ind).EntireRow.Delete'將不起作用。謝謝!這解決了我的問題:) – Hibari

+1

@Thomas Inzina歡迎回來:)但是,你比這更好,這個代碼是「尖叫」升級到「Select Case」(而不是這個混亂的'如果'和'Or' )。 –

+1

@ShaiRado哈哈..你是對的。我應該使用「Select Case」重構它,或者只是顯示修改。代碼已更新。再次感謝 – 2017-08-02 07:14:44

0

試試下面的代碼:

For ind = lrow To 2 Step -1 
ws.Range("G" & ind).select 
reportProcess = Trim(LTrim(RTrim(ws.Range("G" & ind).Text))) 
If reportProcess = "Fulfillment - H/W Direct Customers (Operational)" Or reportProcess = "Revenue - Hardware" Then 
    brand = "HW" 
ElseIf reportProcess = "Revenue - Software" Then 
    ' ~~ Check Control Point Number 
    subReportProcess = Mid(ws.Range("I" & ind).Text, 12, 3) 
    If subReportProcess = "201" Or subReportProcess = "202" Or subReportProcess = "203" Or subReportProcess = "204" Or subReportProcess = "205" Then 
     brand = "PBS" 
    Else 
     brand = "SWG" 
    End If 
ElseIf reportProcess = "Revenue - GBS" Or reportProcess = "Revenue - GTS IS" Or reportProcess = "Fulfillment - Services(Operational)" Then 
    brand = "PBS" 
ElseIf reportProcess = "Revenue - TSS" Then 
    brand = "TSS" 
ElseIf reportProcess = "Accounts Receivable" Or reportProcess = "IBM Credit LLC - Accounts Receivable" Then 
    brand = "AR" 
End If 

country = Trim(LTrim(RTrim(ws.Range("V" & ind).Text))) 
If country = "Taiwan" Then 
    geo = "Taiwan" 
ElseIf country = "India" Then 
    geo = "India" 
ElseIf country = "New Zealand" Or country = "Australia" Then 
    geo = "ANZ" 
ElseIf country = "Hong Kong" Then 
    geo = "Hong Kong" 
ElseIf country = "Philippines" Or country = "Malaysia" Or country = "Singapore" Or country = "Thailand" Or country = "Vietnam" Or country = "Indonesia" Then 
    geo = "ASEAN" 
Else 
    'Delete the row 
    activecell.entirerow.delete 
    ind = ind - 1 
    goto GotoNextRecord 
End If 
ws.Range("B" & ind) = geo 
ws.Range("A" & ind) = brand 
    GotoNextRecord: 
    Next ind