Activate as soon as pasted

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
Solution
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.
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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