Compile Error

BuffaloGuy

New Member
Joined
Dec 5, 2017
Messages
28
I am not great at coding, so I imagine I missed something simple, but I get a compile error stating "Sub or Function not defined." with the following code. It is located in the Workbook section

Private Sub Workbook_Open()
StartLogging
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
StopLogging
End Sub

1611517203274.png
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Do you have a macro called StartLogging & another one called StopLogging?
 
Upvote 0
VBA Code:
Sub StartLogging()
  Const strSOURCE_SHEET = "Stock Data" ' <-- name of sheet containing stock data
  Const strTARGET_SHEET = "Data Log" ' <-- name of sheet to periodically log data to
  Const strSOURCE_RANGE = "C3:I3" ' <-- cell address of data to be logged
  Dim intNextCol As Integer
  Dim rngSource As Range
 
  On Error GoTo ErrorHandler
  Application.ScreenUpdating = False
  Set rngSource = ThisWorkbook.Sheets(strSOURCE_SHEET).Range(strSOURCE_RANGE).Columns(1)
  With ThisWorkbook.Sheets(strTARGET_SHEET)
    intNextCol = .Cells(5, .Columns.Count).End(xlToLeft).Column + 1
    .Cells(5, intNextCol).Value = Now()
    .Cells(5, intNextCol).Font.Bold = True
    .Cells(6, intNextCol).Resize(rngSource.Cells.Count).Value = rngSource.Value
    .Columns(intNextCol).AutoFit
  End With
  m_dtmNextSchedule = Now() + TimeValue("0:01") ' <-- reschedule for 1 minute time
  Application.OnTime m_dtmNextSchedule, "StartLogging", Schedule:=True
 
ExitHandler:
  On Error Resume Next
  Application.ScreenUpdating = True
  Exit Sub
ErrorHandler:
  MsgBox Err.Description, vbExclamation
  Resume ExitHandler
End Sub

Sub StopLogging()
  On Error GoTo ErrorHandler
  Application.OnTime m_dtmNextSchedule, "StartLogging", Schedule:=False
  Exit Sub
ErrorHandler:
  MsgBox Err.Description, vbExclamation
End Sub
 
Upvote 0
Are those macros in the same workbook & are they in a standard module?
 
Upvote 0
Yes they are in the same workbook, and I believe they are in a standard module, what would I look for for that?
1611518174116.png
 
Upvote 0
They are in a Sheet module, not a standard module. Click on Insert on the menu bar & then Module & put the code into that module
 
Upvote 0
Solution
Ok. If you get stuck just start a new thread.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
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