Activate as soon as pasted

Livin404

Active Member
Joined
Jan 7, 2019
Messages
396
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings, I have this basic code. I would like it to be activated as soon as I paste my data to my worksheet. I know it works, jus want the macro to activate as soon as I paste it. I would like to avoid having buttons. The code is
VBA Code:
Sub delCol()
        Dim sourceSheet As Worksheet
        Set sourceSheet = Sheet1
     sourceSheet.Range("A:A, G:H, J:L, N:X, Z:AD").EntireColumn.Delete
    End Sub


The following code is what I have in one of my modules. If i am going to post as a "private" Sub I already have
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Columns.AutoFit
End Sub

Thank you!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,510
Office Version
  1. 365
Platform
  1. Windows
Just call it from your "Worksheet_Change" event procedure, i.e.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Call delCol
    Columns.AutoFit
End Sub
 

Livin404

Active Member
Joined
Jan 7, 2019
Messages
396
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Just call it from your "Worksheet_Change" event procedure, i.e.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Call delCol
    Columns.AutoFit
End Sub
Thanks for the prompt response, I get a error box up stating "Out of Stack Space". I put exactly what you typed in Sheet 1 while I left my original code to remove the columns in the module. Not sure what is happening.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,510
Office Version
  1. 365
Platform
  1. Windows
It is probably invoking itself and getting caught in a loop. Try this updated version:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    Call delCol
    Application.EnableEvents = True
    Columns.AutoFit
End Sub
 
Solution

Livin404

Active Member
Joined
Jan 7, 2019
Messages
396
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
It is probably invoking itself and getting caught in a loop. Try this updated version:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    Call delCol
    Application.EnableEvents = True
    Columns.AutoFit
End Sub
Beautiful! Thank you! exactly what I wanted.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,510
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
Glad I was able to help!
 

Watch MrExcel Video

Forum statistics

Threads
1,132,685
Messages
5,654,746
Members
418,149
Latest member
amamiche67

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
Top