如何删除所选行,但如果选中的行是第一行或最后一行,则不要在Excel VBA(宏)中删除 [英] how to delete Selected row , but if selected row is First row or last row then dont delete in Excel VBA (Macro)
本文介绍了如何删除所选行,但如果选中的行是第一行或最后一行,则不要在Excel VBA(宏)中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何删除所选行,但如果选中的行是第一行或最后一行,则不要在Excel中删除VBA(宏)
Sub deleterow()
ActiveSheet.Unprotect
Dim MyRange 作为 对象
' 将所选范围存储在变量中。
设置 MyRange =选择
' 选择整个列。
Selection.EntireRow。选择
' 在所有选定工作表中插入列。
Selection.delete
跨度> <跨度class =code-comment>重新选择以前选择的单元格。
ActiveSheet.Protect DrawingObjects:= False,Contents:= True,Scenarios:= _
False ,AllowFormattingCells:= True,AllowFormattingColumns:= True,_
AllowFormattingRows:= True,AllowInsertingColumns:= True,AllowInsertingRows _
:= True,AllowInsertingHyperlinks: = True,AllowDeletingColumns:= True,_
AllowDeletingRows:= True,AllowSorting:= True,AllowFiltering:= True,_
AllowUsingPivotTables:= True
< span class =code-keyword> End Sub
解决方案
我将你的代码提高到3行:
ActiveSheet.Unprotect
ActiveCell.EntireRow.Delete Shift := xlShiftUp
ActiveSheet.Protect ...
详情请见: Range.Delete Method(Excel) [ ^ ]
注意:您的代码是有潜在危险的,因为它无论在什么情况下都有效!这是什么意思?它从当前活动的工作表中删除行。如果您想删除所需表格中的行,请使用以下内容:
Dim wsh 作为工作表
' ...
设置 wsh = ThisWorkbook.Worksheets( Sheet1)
wsh.ActiveCell.EntireRow.Delete Shift:= xlShiftUp
' ... 跨度>
how to delete Selected row , but if selected row is First row or last row then dont delete in Excel VBA (Macro)
Sub deleterow()
ActiveSheet.Unprotect
Dim MyRange As Object
' Store the selected range in a variable.
Set MyRange = Selection
' Select the entire column.
Selection.EntireRow.Select
' Insert Columns in all selected sheets.
Selection.delete
' Reselect the previously selected cells.
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
End Sub
解决方案
I'll improve your code to 3 lines:
ActiveSheet.Unprotect ActiveCell.EntireRow.Delete Shift:=xlShiftUp ActiveSheet.Protect ...
For further information, please see: Range.Delete Method (Excel)[^]
Note: your code is potentially dangerous, because it works no matter of context! What it means? It deletes row from currently active sheet. If you would like to delete row from desired sheet, use something like this:
Dim wsh As Worksheet '... Set wsh = ThisWorkbook.Worksheets("Sheet1") wsh.ActiveCell.EntireRow.Delete Shift:=xlShiftUp '...
这篇关于如何删除所选行,但如果选中的行是第一行或最后一行,则不要在Excel VBA(宏)中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文