vba编程-删除整行,不包含

vba编程-删除整行,不包含,vba,excel,Vba,Excel,我不熟悉宏和VBA,所以这可能很容易 我需要筛选公司列表,以便从excel选项卡中删除与branchcode不匹配的公司 更准确地说: company.xls包含公司的完整列表,其中第N列为 包含分支代码的 branch.xls,A列包含相关的branchcodes,它 必须用于筛选company.xls中的公司 company.xls中没有匹配的公司 branchcode应从company.xls的tab1到tab2中删除 我希望它有意义 提前感谢您的回复 Function Search_

我不熟悉宏和VBA,所以这可能很容易

我需要筛选公司列表,以便从excel选项卡中删除与branchcode不匹配的公司

更准确地说:

  • company.xls包含公司的完整列表,其中第N列为 包含分支代码的
  • branch.xls,A列包含相关的branchcodes,它 必须用于筛选company.xls中的公司
  • company.xls中没有匹配的公司
    branchcode应从company.xls的tab1到tab2中删除
我希望它有意义

提前感谢您的回复

Function Search_String(x As String) As Boolean
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim Contained As Boolean
Contained = True

'We use the ActiveSheet but you can replace this with
'Sheets("MySheet")if you want
With Sheets("codes")

    'We select the sheet so we can change the window view
    .Select

    'Set the first and last row to loop through
    Firstrow = .UsedRange.Cells(1).Row
    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

    'We loop from Lastrow to Firstrow (bottom to top)
    For Lrow = Lastrow To Firstrow Step -1

        'We check the values in the A column in this example
        With .Cells(Lrow, "A") 'Column letter for codes sheet

            If Not IsError(.Value) Then
                If InStr(x, .Value) Then Contained = False
            End If
        End With
    Next Lrow
End With

Search_String = Contained
End Function


Sub Filtrer()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long

With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
End With

With Sheets("search (14)") 'Sheet name with rows to be deleted
    'We select the sheet so we can change the window view
    .Select

    'If you are in Page Break Preview Or Page Layout view go
    'back to normal view, we do this for speed
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView

    'Turn off Page Breaks, we do this for speed
    .DisplayPageBreaks = False

    'Set the first and last row to loop through
    Firstrow = .UsedRange.Cells(1).Row
    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

    'We loop from Lastrow to Firstrow (bottom to top)
    For Lrow = Lastrow To Firstrow Step -1

        With .Cells(Lrow, "N") 'Change this to the correct Sheets column that needs deleting
            If Not IsError(.Value) Then
                If Search_String(.Value) Then .EntireRow.Delete
            End If
        End With
    Next Lrow

End With

ActiveWindow.View = ViewMode
With Application
    .ScreenUpdating = True
    .Calculation = CalcMode
End With

End Sub

如果我错了,请纠正我。现在您想让用户输入sheetnamecolumnName

那么下面的代码将帮助您

Dim SheetName As String
Dim ColumnName As String

SheetName = InputBox("Enter the Sheet Name ?") ' Ex Sheet1
ColumnName = InputBox("Enter the column Name ?") ' Ex N

With Sheets(SheetName) ' Replace this line
With .Cells(Lrow, ColumnName) ' Replace this line 

你能展示一下你已经写了什么代码吗?我很乐意帮助您调试代码,并回答有关如何在代码中引用工作簿、工作表和范围的问题。然而,如果你只想“你能为我写代码吗?”答案是否定的。如果我要解决这个问题,我会让两个数据集都进入访问。使用查询解决此问题最多需要10分钟。感谢您的回复。我有一些密码。但是我怎么把它贴在这里呢?已超过最大字符数。您可以单击问题下方的“编辑”链接并在此处添加代码,而不是将其粘贴到注释中。应该有足够的空间,但实际上你所需要做的只是一个你正在做的最起码的例子。例如,您是否正在使用自动筛选功能?你是否在每一行中循环检查,以对照公司名单?是否将列表加载到数据结构(如数组或集合)中?如果您包含一小段代码来回答这类问题,那就足够了。再次感谢您的回复。我粘贴了整个代码。实际上,我让它工作了,所以事实上我现在的问题是关于这行代码和表单(“搜索(14)”。问题是,宏将用于多个不同工作簿中的一个数字,这就是为什么上述代码每次都需要更改,具体取决于工作表的名称。因此,我想知道是否可以编写某种对话框,要求宏用户在运行e宏?同样的属性也适用于这行代码。Cells(Lrow,“N”)谢谢Sathish。这正是我想要的,而且效果很好!谢谢againI,我很愿意接受它,但是,因为我是“新来的”,在达到15岁之前我不能……如果我理解正确的话?