Excel / VBA:对话框,选择&删除行> 5,多个工作表

时间:2016-03-18 02:09:36

标签: excel vba

下面的代码适用于删除活动工作表上的行,但我希望有一些更改。我不擅长VBA,这段代码是来自其他地方的样本。我想要两个不同的代码,一个用于当前工作表,另一个用于多个工作表。

  1. 我不希望能够删除此代码的第1-5行。某种对话框上写着:"你只能删除行" 6"或以下"。

    Option Explicit
    
    Sub DeleteMe()
        Dim Ret As Range, Cl As Range
    
        On Error Resume Next
        Set Ret = Application.InputBox("Mark rows to be deleted", "Delete rows", Type:=8)
        On Error GoTo 0
    
        ActiveSheet.Unprotect Password:="password"
    
        If Not Ret Is Nothing Then Ret.EntireRow.Delete
    
        ActiveSheet.Protect Password:="password"
    End Sub
    
  2. 上面的另一个类似代码(删除第6行或更高版本),带有对话框和选择,但是当我在工作表A中选择行时,工作表B和C中的相同行也会被删除。

    < / LI>

2 个答案:

答案 0 :(得分:0)

请尝试以下代码以获得您的要求。 Activesheet的第一个代码和多个工作表的第二个代码

Sub aActivesSheet()
Dim Rownum As Variant
Dim ws As Worksheet
Dim Rng As Range
Dim RngA As Range

Dim lastrow As Long

Set ws = ActiveSheet
 lastrow = ws.UsedRange.Rows(ws.UsedRange.Rows.Count).Row

Rownum = Application.InputBox("Enter row number")

ws.Rows(Rownum & ":" & lastrow).Delete



End Sub


Sub MultipleSheeetss()

Dim wb As Workbook
Dim wsa, wsb, wsc As Worksheet

Set wb = ThisWorkbook

Set wsa = wb.Worksheets("SheetA")
Set wsb = wb.Worksheets("SheetB")
Set wsc = wb.Worksheets("SheetC")

Dim Rownum As Variant

Rownum = Application.InputBox("Enter row number and above to be deleted")



For i = 1 To Rownum

wsa.Rows(i).ClearContents
wsb.Rows(i).ClearContents
wsc.Rows(i).ClearContents

'or use delete

'wsa.Rows(i).delete


Next

End Sub

答案 1 :(得分:-1)

与现在的情况略有不同,但确实有效

Sub DeleteMe()
Dim Ret1 As Long
Dim Ret2 As Long
Dim i As Long

On Error Resume Next
Ret1 = Application.InputBox("Start Row to be Deleted?", "Start Row")
Ret2 = Application.InputBox("End Row to be Deleted?", "End Row")
On Error GoTo 0

If Ret1 > Ret2 Then
MsgBox "Start Row must be lower than End Row"

Else
    If Ret1 > 5 Then
        For i = Ret1 To Ret2
        Rows(Ret1).EntireRow.Delete
        Next i

    ElseIf Ret1 <= 5 Then
    MsgBox "Only Rows 6 and above can be deleted"

    End If

End If

End Sub

这要求开始行和结束行而不是仅选择范围

删除不同工作表上的相同行,将其添加到

For i = Ret1 To Ret2
        Sheets("Sheet1").Rows(Ret1).EntireRow.Delete
        Sheets("Sheet2").Rows(Ret1).EntireRow.Delete
        Sheets("Sheet3").Rows(Ret1).EntireRow.Delete
Next i