Private Sub not running

gtd526

Well-known Member
Joined
Jul 30, 2013
Messages
657
Office Version
  1. 2019
Platform
  1. Windows
Hello,
My Private Sub in not running when the workbook is opened. I have it stored in "ThisWorkbook".
Thank you.

Here is the code Im using:
VBA Code:
Private Sub PastePotentialValue()
'paste special values in potential hours on Fridays date

    Dim foundRng As Range

    Sheets("BJs Income").Select
    Range("Q1").Value = "HELP"

    Set foundRng = Range("A:A").Find(CStr(Date), LookIn:=xlValues, lookat:=xlWhole)
  
    If Not (foundRng Is Nothing) Then
        foundRng.Select
        ActiveCell.Offset(0, 14).Select
        Selection.Copy
        ActiveCell.PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    Else
    End If
        Application.CutCopyMode = alse

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
My Private Sub in not running when the workbook is opened. I have it stored in "ThisWorkbook".
If you want to run it automatically when opening the workbook, it not only has to be in the "ThisWorkbook" module, but it MUST be named:
VBA Code:
Private Sub Workbook_Open()
so it runs as an event procedure.

This is non-negotiable. You cannot change the name.

If you do not want to rename it, then just call it from that, i.e.
VBA Code:
Private Sub Workbook_Open()
    Call PastePotentialValue
End Sub
 
Upvote 0
Solution
If you want to run it automatically when opening the workbook, it not only has to be in the "ThisWorkbook" module, but it MUST be named:
VBA Code:
Private Sub Workbook_Open()
so it runs as an event procedure.

This is non-negotiable. You cannot change the name.

If you do not want to rename it, then just call it from that, i.e.
VBA Code:
Private Sub Workbook_Open()
    Call PastePotentialValue
End Sub
I renamed to Workbook_Open().
I didn't realize to use this Name for opening a workbook.
Thank you.
 
Upvote 0
I renamed to Workbook_Open().
I didn't realize to use this Name for opening a workbook.
Excel has a number of what are known as "Event Procedures". These are "events" are things that happen in Excel that can automatically kick-off VBA code to run.
However, Excel needs to be able to connect the VBA code with the Event calling it. So each event has its own precise name. Name it anything else, and it isn't looking for the event.

There are two main types of events - those associated with the Workbook (found in the "ThisWorkbook" module), and those associated with Sheets (found in the various "Sheet" modules).

See here for more details on this: Events In Excel VBA
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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