Hyperlink to delete column

happyhungarian

Active Member
Joined
Jul 19, 2011
Messages
252
Office Version
  1. 365
Platform
  1. Windows
Hi, I was hoping to figure out a way to easily add a "delete column" function to my worksheet. I was going to have, in row 2 of each column, a hyperlink that's mapped to (what i'm hoping to be) the same macro. That macro would recognize what cell the clicked hyperlink is sitting in and then proceed to delete the entire column that's associated with that cell. Hope that makes sense!!! :eeek:
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
What about putting this in the Sheet module,

Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("A2:M2")) Is Nothing Then
            ActiveCell.EntireColumn.Delete (xlLeft)
        End If
    End If
End Sub


If need be, entering the delete info could be done via a macro such as the following

Code:
Option Explicit


Sub AddDelInfo()
ActiveSheet.Range("A2:M2").Value = "Click to Delete Column"
End Sub

Obviously, amend the ranges as per your needs, or even use clear contents instead of entirecolumn.delete as a possibility
 
Upvote 0
Here's an example... the "Delete" you see in each column would all be linked to the same macro. If the link in G1 is clicked then all of column G is deleted

[TABLE="width: 540"]
<colgroup><col><col span="8"></colgroup><tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Delete[/TD]
[TD]Delete[/TD]
[TD]Delete[/TD]
[TD]Delete[/TD]
[TD]Delete[/TD]
[TD]Delete[/TD]
[TD]Delete[/TD]
[TD]Delete[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Task 1[/TD]
[TD]Task 2[/TD]
[TD]Task 3[/TD]
[TD]Task 4[/TD]
[TD]Task 5[/TD]
[TD]Task 6[/TD]
[TD]Task 7[/TD]
[TD]Task 8[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Rich (BB code):
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("A1:M1")) Is Nothing Then
            ActiveCell.EntireColumn.Delete (xlLeft)
        End If
    End If 

End Sub

Just add this VBA to your sheet module, and amend the M to whichever is your last column of data.
When ever the user clicks a cell in row 1 that column will be deleted.

If you wish I can amend it to ask the user if they're sure they want to delete the column. Just let me know.
 
Last edited:
Upvote 0
Alternately you could use a double-click event
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   If Target.Row = 1 Then Target.EntireColumn.Delete
   Cancel = True
End Sub
Double-click any cell in row 1 & that column will be deleted.
 
Upvote 0
I took the liberty to write in a user check before deleting just incase you or anyone searching for this in future needs it.

Code:
Option Explicit


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Ans As Integer
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("A1:M1")) Is Nothing Then
            Ans = MsgBox("Are you sure you wish to delete column " & Chr(ActiveCell.Column + 64), vbYesNo + vbExclamation, "Delete Column?")
                If Ans = vbNo Then
                    ActiveCell.Offset(1).Select
                    Exit Sub
                Else
                    ActiveCell.EntireColumn.Delete (xlLeft)
                End If
        End If
    End If
End Sub
 
Upvote 0
if you wanted

your post here. https://www.mrexcel.com/forum/excel-questions/1092186-copying-inserting-column.html

could be added into this macro as per:

Code:
Option Explicit


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Ans As Integer, [COLOR=#ff0000]LastCol As Long[/COLOR]


[COLOR=#ff0000]LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column - 1[/COLOR]


    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("A1:M1")) Is Nothing Then
            Ans = MsgBox("Are you sure you wish to delete column " & Chr(ActiveCell.Column + 64), vbYesNo + vbExclamation, "Delete Column?")
                If Ans = vbNo Then
                    ActiveCell.Offset(1).Select
                    Exit Sub
                Else
                    ActiveCell.EntireColumn.Delete (xlLeft)
                    [COLOR=#ff0000]Columns(LastCol).Copy Columns(LastCol + 1)[/COLOR]
                End If
        End If
    End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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