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!!! :eek:
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
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

ABCDEFGH
1DeleteDeleteDeleteDeleteDeleteDeleteDeleteDelete
2Task 1Task 2Task 3Task 4Task 5Task 6Task 7Task 8
3

<colgroup><col><col span="8"></colgroup><tbody>
</tbody>
 
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,214,822
Messages
6,121,770
Members
449,049
Latest member
greyangel23

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