你如何制作一個宏,如果選擇了一行,它會洗掉一行,否則會提示一個訊息框?我做了一個這樣的代碼,但它總是跳過第一個 if 陳述句并繼續下一個。
Sub DeleteRow()
If ActiveCell.Row = True Then
Selection.EntireRow.Delete
Else: MsgBox "Choose a row first", vbOKOnly, "Delete a row"
End If
End Sub
uj5u.com熱心網友回復:
你可以試試下面的子
Sub DeleteRow()
If ActiveCell.Row = Selection.Row Then
Selection.EntireRow.Delete
Else: MsgBox "Choose a row first", vbOKOnly, "Delete a row"
End If
End Sub
基本上你可以在單行下方運行來洗掉選定的行。
Selection.EntireRow.Delete
這也應該有效
ActiveCell.EntireRow.Delete
uj5u.com熱心網友回復:
任何時候您希望代碼在對作業表執行某些操作時運行,請考慮使用 Worksheet 子例程。
您感興趣的是Worksheet_SelectionChange. 作業表子例程(例如Worksheet_SelectionChange始終存盤在該作業表的代碼中。) 以下是作業表具有的事件串列。
讓我們分解你的問題:
- 當用戶選擇某物時,確定他們選擇了什么
- 如果用戶選擇了整行,請執行某些操作或
- 如果用戶選擇了單個單元格,請執行其他操作或
- 如果用戶選擇了其他任何內容,請忽略
Worksheet_SelectionChange接受一個引數: ,這Target是用戶所做的選擇。
下面是洗掉一行的代碼,如果它被選中。這應該足以讓您朝著正確的方向前進。
再次強調:在回應用戶輸入時,始終考慮內置的 Worksheet 子例程。
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Purpose:
' Delete a row if it is selected
' Check if a row was selected
Dim Address As String
Address = Target.Address
Address = Replace(Address, "$", "")
' Rows and columns have the form #:# when selected
' Where # is either the column letter OR the row number
If InStr(Address, ":") Then
' Since a : was in the address, an entire row or column was selected
' Was a column or row selected?
' Check if the members of AddressArray are numbers
Dim AddressArray As Variant
AddressArray = Split(Address, ":")
If IsNumeric(AddressArray(0)) And IsNumeric(AddressArray(1)) Then
' Both members of the array were numbers, so a row was selected
' Delete the row
Target.Delete
End If
End If
End Sub
uj5u.com熱心網友回復:
我會采用這種方法:
If Selection.Address = Selection.Cells(1).EntireRow.Address Then
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417855.html
標籤:
上一篇:在范圍內查找特定名稱時出現運行時錯誤424“需要物件”
下一篇:拆分包含公式的Excel單元格
