从多个表格中删除整行

时间:2016-01-13 12:02:13

标签: excel vba excel-vba

我需要一些vba代码的帮助,该代码将删除当前活动的不同工作表中的整行。

代码使用userform根据输入到文本框中的序列号删除行。要删除的行复制在激活用户窗体的工作表上以及另一个工作表上。下面是我尝试过的一个示例,该示例将删除当前工作表的行,但会在Else命令中发回第二部分代码的错误。

Private Sub ScrapButton_Click()

Dim RTCNumber As String
RTCNumber = RTCTextBox

MSG1 = MsgBox("Remove " + RTCTextBox + " from Lab Stock?", vbYesNo)

If MSG1 = vbNo Then
    Exit Sub
Else
    Dim Row As Integer
    Row = Application.WorksheetFunction.Match(RTCNumber, Sheet6.Range("A:A"), 0)
    Rows(Row).EntireRow.Delete
    Dim Row2 As Integer
    Row2 = Application.WorksheetFunction.Match(RTCNumber, Sheet1.Range("A:A"), 0)
    Sheets("Sheet1").Rows(Row2).EntireRow.Delete

End If

End Sub

任何帮助都会非常感激,我可能会遗漏一些明显的东西,但我对vba相当新。我尝试了几个选项,无法使用Worksheet.Activate函数使其工作。

提前致谢。

詹姆斯

4 个答案:

答案 0 :(得分:0)

试试这个:

Private Sub ScrapButton_Click()

  Dim RTCNumber As Double
  Dim Row2 As Variant
  Dim Row1 As Variant
  Dim wb As Workbook
  Dim ws1 As Worksheet
  Dim ws6 As Worksheet

  Set wb = ThisWorkbook
  Set ws1 = wb.Sheets("Sheet1")
  Set ws6 = wb.Sheets("Sheet6")

RTCNumber = RTCTextBox

MSG1 = MsgBox("Remove " & RTCNumber & " from Lab Stock?", vbYesNo)

If MSG1 = vbNo Then
    Exit Sub
Else

    Row1 = ws6.Application.WorksheetFunction.Match(RTCNumber, ws6.Range("A:A"), 0)
    ws6.Rows(Row1).EntireRow.Delete


    Row2 = ws1.Application.WorksheetFunction.Match(RTCNumber, ws1.Range("A:A"), 0)
    ws1.Rows(Row2).EntireRow.Delete

End If

End Sub

答案 1 :(得分:0)

尝试CLng匹配函数的第一个参数。这对我有用。

不得不在我的Excel版本上删除WorksheetFunction,但我不知道你的机器上是否有这种情况,所以我把它留在了。

然后,正如manu在他的回答中所述,我添加了工作表参考。

Private Sub ScrapButton_Click()

Dim RTCNumber As String
RTCNumber = RTCTextBox

MSG1 = MsgBox("Remove " + RTCTextBox + " from Lab Stock?", vbYesNo)

If MSG1 = vbNo Then
    Exit Sub
Else
    Dim Row As Integer
    Row = Application.WorksheetFunction.Match(Clng(RTCNumber), Sheets("Sheet6").Range("A:A"), 0)
    Rows(Row).EntireRow.Delete
    Dim Row2 As Integer
    Row2 = Application.WorksheetFunction.Match(Clng(RTCNumber), Sheets("Sheets1").Range("A:A"), 0)
    Sheets("Sheet1").Rows(Row2).EntireRow.Delete

End If

End Sub

答案 2 :(得分:0)

您可以使用更简单的方法使用range.find方法获得所需的结果 所以,你的代码看起来像这样:

Private Sub ScrapButton_Click()
    Dim RTCNumber As String
    Dim Cl As Range
    RTCNumber = RTCTextBox

    msg1 = MsgBox("Remove " + RTCTextBox + " from Lab Stock?", vbYesNo)

    If msg1 = vbNo Then
        Exit Sub
    Else
        With Sheets("Sheet1")
           Set Cl = .[A:A].Find(RTCNumber, , xlValues, xlWhole)
           If Not Cl Is Nothing Then Cl.EntireRow.Delete
        End With
        With Sheets("Sheet6")
           Set Cl = .[A:A].Find(RTCNumber, , xlValues, xlWhole)
           If Not Cl Is Nothing Then Cl.EntireRow.Delete
        End With
    End If
End Sub

如果您仍然希望使用worksheetfunction,那么您应该知道如果worksheetfunction.match无法找到搜索值,那么它将返回错误,worksheetfunction s仅用于错误处理

答案 3 :(得分:0)

太棒了,谢谢你的帮助!

通过一些微小的调整来管理Manu的答案。结果是这样的:

Private Sub ScrapButton_Click()

  Dim RTCNumber As String
  Dim Row2 As Variant
  Dim Row1 As Variant
  Dim wb As Workbook
  Dim ws1 As Worksheet
  Dim ws6 As Worksheet

  Set wb = ActiveWorkbook
  Set ws1 = wb.Worksheets("Lab Stock")
  Set ws6 = wb.Worksheets("Scrap")

RTCNumber = RTCTextBox

MSG1 = MsgBox("Remove " & RTCNumber & " from Lab Stock?", vbYesNo)

If MSG1 = vbNo Then
    Exit Sub
Else

    Row1 = ws6.Application.WorksheetFunction.Match(RTCNumber, ws6.Range("A:A"), 0)
    ws6.Rows(Row1).EntireRow.Delete


    Row2 = ws1.Application.WorksheetFunction.Match(RTCNumber, ws1.Range("A:A"), 0)
ws1.Rows(Row2).EntireRow.Delete

End If

End Sub

非常感谢!