Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel清理代码VBA_Excel_Excel 2007_Vba - Fatal编程技术网

Excel清理代码VBA

Excel清理代码VBA,excel,excel-2007,vba,Excel,Excel 2007,Vba,我有一些代码可以很好地工作,但我知道它可以写得比我有更干净 Sub DeleteField() Range("A6").Select Do If ActiveCell.Value = "Actual Conveyable Cases" Or _ ActiveCell.Value = "Projected Non Con" Or _ ActiveCell.Value = "Actual Non Con Cas

我有一些代码可以很好地工作,但我知道它可以写得比我有更干净

Sub DeleteField()
Range("A6").Select

        Do
            If ActiveCell.Value = "Actual Conveyable Cases" Or _
            ActiveCell.Value = "Projected Non Con" Or _
            ActiveCell.Value = "Actual Non Con Cases" Or _
            ActiveCell.Value = "Projected CPT" Or _
            ActiveCell.Value = "Actual CPT" Or _
            ActiveCell.Value = "Projected Store Loads" Or _
            ActiveCell.Value = "Actual Store Loads" Or _
            ActiveCell.Value = "Projected Pull Ahead" Or _
            ActiveCell.Value = "Actual Pull Ahead" Or _
            ActiveCell.Value = "Projected Loads at 08:00" Or _
            ActiveCell.Value = "Actual Loads at 08:00" Then
        ActiveCell.EntireRow.Select
        Selection.Delete
        ActiveCell.Select
            Else: ActiveCell.Offset(1, 0).Select
        End If

    Loop Until ActiveCell.Value = "" And ActiveCell.Offset(-5, 0).Value = ""

MsgBox "Done!"

End Sub
我正在寻找一种方法来清理多个Or语句。是否有一种方法可以通过阵列或某种方法来清理所有“ActiveCell”段来实现这一点

您可以使用来清理:

Select Case ActiveCell.Value 
    Case "Actual Conveyable Cases", "Projected Non Con", _
         "Actual Non Con Cases", "Projected CPT", "etc..."

            ActiveCell.EntireRow.Select
            Selection.Delete
            ActiveCell.Select

    Case Else

            ActiveCell.Offset(1, 0).Select

End Select
您可以使用以下方法来清理:

Select Case ActiveCell.Value 
    Case "Actual Conveyable Cases", "Projected Non Con", _
         "Actual Non Con Cases", "Projected CPT", "etc..."

            ActiveCell.EntireRow.Select
            Selection.Delete
            ActiveCell.Select

    Case Else

            ActiveCell.Offset(1, 0).Select

End Select

这就是我将如何编程来清理它。希望能有帮助

此外,我删除了以下条件: ActiveCell.Offset(-5,0).Value=“”

因为向下移动5个额外的单元格似乎很奇怪

Option Explicit

Public Sub DeleteField()
    Dim sh As Excel.Worksheet
    Dim iLastRow As Long, iFirstRow As Long, i As Long

    ' define the array here so we don't have to create it over
    ' and over again in the function toDelete()
    Dim list(11) As String
    list(0) = "Actual Conveyable Cases"
    list(1) = "Projected Non Con"
    list(2) = "Actual Non Con Cases"
    list(3) = "Projected CPT"
    list(4) = "Actual CPT"
    list(5) = "Projected Store Loads"
    list(6) = "Actual Store Loads"
    list(7) = "Projected Pull Ahead"
    list(8) = "Actual Pull Ahead"
    list(9) = "Projected Loads at 08:00"
    list(10) = "Actual Loads at 08:00"

    Set sh = ActiveSheet
    iFirstRow = 6
    iLastRow = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row

    ' loop through the cells from bottom to top (because if we
    ' delete the row it's easier to maintain where we are)
    For i = iLastRow To iFirstRow Step -1
        If (toDelete(list, sh.Cells(i, 1))) Then
            sh.Cells(i, 1).EntireRow.Delete
        End If
    Next

    MsgBox "Done!"
End Sub

Private Function toDelete(ByRef list() As String, ByRef r As Excel.Range) As Boolean
    Dim i As Long
    For i = 0 To UBound(list)
        If (r.Value = list(i)) Then
            toDelete = True
            Exit Function
        End If
    Next i

    toDelete = False
End Function

这就是我将如何编程来清理它。希望能有帮助

此外,我删除了以下条件: ActiveCell.Offset(-5,0).Value=“”

因为向下移动5个额外的单元格似乎很奇怪

Option Explicit

Public Sub DeleteField()
    Dim sh As Excel.Worksheet
    Dim iLastRow As Long, iFirstRow As Long, i As Long

    ' define the array here so we don't have to create it over
    ' and over again in the function toDelete()
    Dim list(11) As String
    list(0) = "Actual Conveyable Cases"
    list(1) = "Projected Non Con"
    list(2) = "Actual Non Con Cases"
    list(3) = "Projected CPT"
    list(4) = "Actual CPT"
    list(5) = "Projected Store Loads"
    list(6) = "Actual Store Loads"
    list(7) = "Projected Pull Ahead"
    list(8) = "Actual Pull Ahead"
    list(9) = "Projected Loads at 08:00"
    list(10) = "Actual Loads at 08:00"

    Set sh = ActiveSheet
    iFirstRow = 6
    iLastRow = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row

    ' loop through the cells from bottom to top (because if we
    ' delete the row it's easier to maintain where we are)
    For i = iLastRow To iFirstRow Step -1
        If (toDelete(list, sh.Cells(i, 1))) Then
            sh.Cells(i, 1).EntireRow.Delete
        End If
    Next

    MsgBox "Done!"
End Sub

Private Function toDelete(ByRef list() As String, ByRef r As Excel.Range) As Boolean
    Dim i As Long
    For i = 0 To UBound(list)
        If (r.Value = list(i)) Then
            toDelete = True
            Exit Function
        End If
    Next i

    toDelete = False
End Function

看看这个问题,可以使用

Dim value As String
Dim matched As Boolean
Dim criteria As String

'** Put all possible strings separated by comma here
criteria = "Actual Conveyable Cases, Projected Non Con, Actual Non Con Cases"

do
   matched = (InStr(1, criteria, ActiveCell.Value) <> 0)

   If matched then

   End If
...

看看这个问题,可以使用

Dim value As String
Dim matched As Boolean
Dim criteria As String

'** Put all possible strings separated by comma here
criteria = "Actual Conveyable Cases, Projected Non Con, Actual Non Con Cases"

do
   matched = (InStr(1, criteria, ActiveCell.Value) <> 0)

   If matched then

   End If
...

我知道你有很好的答案,但作为字典对象的推销员,我有义务展示在这里使用它是多么有趣。它的工作速度也非常快,所以如果你有一长串的条件,这可能是你想要的方式。享受吧

Sub DeleteField()

Dim dict As Object, i As Long
Set dict = CreateObject("scripting.dictionary")

dict.Add "Actual Conveyable Cases", 1
dict.Add "Projected Non Con", 1
dict.Add "Actual Non Con Cases", 1
dict.Add "Projected CPT", 1
dict.Add "Actual CPT", 1
dict.Add "Projected Store Loads", 1
dict.Add "Actual Store Loads", 1
dict.Add "Projected Pull Ahead", 1
dict.Add "Actual Pull Ahead", 1
dict.Add "Projected Loads at 08:00", 1
dict.Add "Actual Loads at 08:00", 1

For i = range("A" & Rows.count).End(xlUp).Row To 1 Step -1
    If dict.exists(range("A" & i).Value) Then
        Rows(i).Delete
        i = i + 1
    End If
Next

MsgBox "Done!"

End Sub

我知道你有很好的答案,但作为字典对象的推销员,我有义务展示在这里使用它是多么有趣。它的工作速度也非常快,所以如果你有一长串的条件,这可能是你想要的方式。享受吧

Sub DeleteField()

Dim dict As Object, i As Long
Set dict = CreateObject("scripting.dictionary")

dict.Add "Actual Conveyable Cases", 1
dict.Add "Projected Non Con", 1
dict.Add "Actual Non Con Cases", 1
dict.Add "Projected CPT", 1
dict.Add "Actual CPT", 1
dict.Add "Projected Store Loads", 1
dict.Add "Actual Store Loads", 1
dict.Add "Projected Pull Ahead", 1
dict.Add "Actual Pull Ahead", 1
dict.Add "Projected Loads at 08:00", 1
dict.Add "Actual Loads at 08:00", 1

For i = range("A" & Rows.count).End(xlUp).Row To 1 Step -1
    If dict.exists(range("A" & i).Value) Then
        Rows(i).Delete
        i = i + 1
    End If
Next

MsgBox "Done!"

End Sub


+1,但是用工作正常的
ActiveCell.EntireRow.Delete
替换第一种情况下的3行怎么样。我必须记得把精选的箱子加到我的魔术袋里。谢谢你+1,但是用工作正常的
ActiveCell.EntireRow.Delete
替换第一种情况下的3行怎么样。我必须记得把精选的箱子加到我的魔术袋里。非常感谢。嗨,只是好奇,为什么有这行:ActiveCell.Offset(-5,0).Value=“”?似乎你不需要检查5个额外的空单元格。OP显然希望在他的数据表中保留5行不包含任何已检查字符串的行。我将其作为循环停止,因为从开始到结束的范围内都有中断。数据的单元格是1:10,然后是空单元格,然后是下一个10。我知道停止循环的唯一方法是使用列出的代码。嗨,我只是好奇,为什么有这一行:ActiveCell.Offset(-5,0)。Value=“”?似乎你不需要检查5个额外的空单元格。OP显然希望在他的数据表中保留5行不包含任何已检查字符串的行。我将其作为循环停止,因为从开始到结束的范围内都有中断。数据的单元格是1:10,然后是空单元格,然后是下一个10。我知道停止循环的唯一方法是使用列出的代码。顺便说一句,我觉得很有趣,我试着在答案的开头加上“嗨”,但它一直被删除。因为这是一个编程问题的网站,不是IM:)@ZevSpitz,touche:)我只是想表现得风度翩翩。顺便说一句,我觉得我试着加上“嗨”很有趣“您好,”在我回答的开始,但它一直被删除。因为这是一个用于编程问题的网站,而不是IM:)@ZevSpitz,touche:)我只是想表现得更具个性。因为我们在VBA中,所以没有必要使用后期绑定(
CreateObject…
)。添加对Microsoft Scripting Runtime的引用,并将变量声明为
Dictionary
Scripting.Dictionary
。还要记住,默认情况下,字典区分大小写。可以通过设置
dict.CompareMode=TextCompare
来更改此设置,如果使用后期绑定和无法访问脚本运行时contstants.Zev,如果您想与其他用户共享此VBA代码,则让他们添加引用对用户不友好。如果只使用“创建对象”,则更为灵活。对于使用Office对象模型,对象模型可能有多个版本,并且名称从一个版本更改为另一个版本版本,您可能是对的。但自Windows XP和Office 2000以来,Microsoft脚本运行时已在所有版本的Windows上可用。由于我们使用VBA,因此无需使用后期绑定(
CreateObject…
)。添加对Microsoft Scripting Runtime的引用,并将变量声明为
Dictionary
Scripting.Dictionary
。还要记住,默认情况下,字典区分大小写。可以通过设置
dict.CompareMode=TextCompare
来更改此设置,如果使用后期绑定和无法访问脚本运行时contstants.Zev,如果您想与其他用户共享此VBA代码,则让他们添加引用对用户不友好。如果只使用“创建对象”,则更为灵活。对于使用Office对象模型,对象模型可能有多个版本,并且名称从一个版本更改为另一个版本版本,您可能是对的。但自Windows XP和Office 2000以来,Microsoft脚本运行时已在所有版本的Windows上可用。