Deleting a user selected row / column with a macro

magnoth

New Member
Joined
May 6, 2005
Messages
6
Greetings All,

I have created a spreadsheet with macros that will add predetermined # of Rows or Columns. I have assigned these to Macro Buttons and are working fine.

I would like to create another macro that will delete columns / rows based on the user selection.

Example: I click to select the entire column J, then click on a button that will delete the column.

Any help would be greatly appreciated.

Regards,

Ron Lister
Tampa, FL
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Code:
Selection.EntireRow.Delete xlShiftUp
Code:
Selection.EntireColumn.Delete xlShifttoLeft
 
Upvote 0
one line:
Code:
Selection.Delete
but to ensure they have selected an entire column first:
Code:
If Selection.Address = Selection.EntireColumn.Address Then Selection.Delete
 
Upvote 0
Or, in case they may not know how to select the entire row/column, this one will see whether a single row + multiple columns is selected (delete row), or single column + multiple rows (delete column).

Code:
Sub DeleteSelected()
  Dim rng As Excel.Range
  
  Set rng = Selection
  Debug.Print rng.Address
  
  If rng.Rows.Count = 1 And rng.Columns.Count > 1 Then
    rng.EntireRow.Delete
  ElseIf rng.Columns.Count = 1 And rng.Rows.Count > 1 Then
    rng.EntireColumn.Delete
  End If
End Sub
 
Upvote 0
WOW!!!! So many options!

Thank You jbeaucaire, p45cal & iliace. The quick response is appreciated.

I will attempt each of your solutions to see which best suits my needs.

Again Thank you for your assistance
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top