简体   繁体   English

VBA Excel循环基于C列中的值删除范围

[英]VBA Excel Loop to Delete Range Based on Value in Column C

I am trying to delete a specific range based on whether or not column "C" meets specific criteria. 我正在尝试根据列“ C”是否满足特定条件来删除特定范围。

Currently I have the following: 目前,我有以下内容:

If Range("C69") = "All values USD Millions." Then
    Range("C69 : H69").Delete shift:=xlUp
Else Range("A3").Select
End If

I want to turn this into a loop that will search cells "C1" through "C100" for the words "All values USD Millions." 我想将其变成一个循环,将在单元格“ C1”至“ C100”中搜索单词“所有值USD百万”。 and delete the corresponding C though H range. 并通过H范围删除相应的C。 For example, if it found the value in "C15", it would delete Range("C15:H15"). 例如,如果在“ C15”中找到该值,则会删除Range(“ C15:H15”)。

Unfortunately, I am still learning and all the loops I try create an error. 不幸的是,我仍在学习,我尝试的所有循环都会产生一个错误。

You can use the filtering capability of Excel: 您可以使用Excel的筛选功能:

With Sheet1.Range("C1:H100")
    .AutoFilter 1, "All values USD Millions."
    .Offset(1).Delete
    .AutoFilter
End With

However if you want to do a "classic" iteration and delete while iterating on the rows, remember always that in these cases you should iterate "backward" : 但是,如果要进行“经典”迭代并在对行进行迭代时删除,请始终记住在这种情况下应对“ backward”进行迭代:

Dim i as long
For i = Range("C999999").End(xlUp).Row to 1 Step -1
    If Cells(i, "C").Value2 = "All values USD Millions." Then Rows(i).Delete
Next

Another fast way, without looping throughout the rows one by one, is using the Find function: 另一种快速的方法,而不是一个接一个地遍历所有行,是使用Find函数:

Option Explicit

Sub UseFindFunc()

Dim FindRng         As Range
Dim Rng             As Range
Dim LastRow         As Long
Dim TexttoFind      As String

TexttoFind = "All values USD Millions." ' <-- try to use variable, easy to modify later

With Sheet1
    LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row '<-- get last row with data in Column C
    Set Rng = .Range("C1:H" & LastRow)

    Set FindRng = Rng.Find(What:=TexttoFind, LookIn:=xlValues, LookAt:=xlWhole)
    While Not FindRng Is Nothing '<-- find was successful
        FindRng.Resize(, 5).Delete xlShiftUp '<-- delete column "C:H" in found row
        Set FindRng = Rng.Find(What:=TexttoFind, LookIn:=xlValues, LookAt:=xlWhole)
    Wend
End With

End Sub

Try using: 尝试使用:

For i=1 to 100
    If Cells (i, 3) = "All values USD Millions." Then

        Rows (i).Delete

    EndIf
Next

If you don't want to delete cells from columns A and B, this works for me: 如果您不想删除A和B列中的单元格,这对我有用:

Sub test()
Dim i As Integer
For i = 1 To 100
    If Range("C" & i) = "All values USD Millions." Then
        Range("C" & i & ":H" & i).Delete
    Else
        Range("A3").Select
    End If
Next
End Sub

If your data is not as static you could do the script till your last row or set i = 1 to 100 to stop at row 100 如果您的数据不是静态的,则可以执行脚本直到最后一行,或者将i = 1设置为100以停止在第100行

    Sub test()
    Dim lRow As Long

     lRow = WorksheetFunction.Max(Range("C65536").End(xlUp).Row, 
     Range("D65536").End(xlUp).Row, Range("E65536").End(xlUp).Row)

    With ActiveSheet
    For i = lRow To 2 Step -1
    If .Cells(i, "C").Value = "All values USD Millions." Then
      Range("C" & i & ":H" & i).ClearContents
    End If
   Next i
   End With


   End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM