BuffaloGuy
New Member
- Joined
- Dec 5, 2017
- Messages
- 28
I am attempting to log stock data every 'n' minutes (1 in this example), I bring in the information via data in excel data types.
This code is supposed to refresh data every minute and copy the data from cells C3 through I3 and copy it into row 103 and keep the data and add the new data on top.
I copied this code from someone else and tried to modify it to my needs. I likely butchered it.
I get a application-defined or object-defined error message.
This code is supposed to refresh data every minute and copy the data from cells C3 through I3 and copy it into row 103 and keep the data and add the new data on top.
I copied this code from someone else and tried to modify it to my needs. I likely butchered it.
I get a application-defined or object-defined error message.
VBA Code:
Sub StartLogging()
Const strSOURCE_SHEET = "Data" ' <-- name of sheet containing stock data
Const strTARGET_SHEET = "Data" ' <-- name of sheet to periodically log data to
Const strSOURCE_RANGE = "C3:I3" ' <-- cell address of data to be logged
Dim intNextRow As Integer
Dim rngSource As Range
On Error GoTo ErrorHandler
Application.ScreenUpdating = False
Set rngSource = ThisWorkbook.Sheets(strSOURCE_SHEET).Range(strSOURCE_RANGE).Rows(1)
With ThisWorkbook.Sheets(strTARGET_SHEET)
intNextRow = .Cells(103, .Rows.Count).End(xlDown).Row + 1
.Cells(103, intNextRow).Value = Now()
.Cells(103, intNextRow).Font.Bold = True
.Cells(104, intNextRow).Resize(rngSource.Cells.Count).Value = rngSource.Value
.Rows(intNextRow).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
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub