Good evening all,
5 days in searching for and attempting on my own, I am tired. My ignorance has overtaken me, I need expert help. My fabricobbling has proven unsuccessful. Short and sweet, I run a report in excel that pulls data from a sql database. The recordset returns info, one column of which contains timestamps formatted as such: MM-DD-YYYY HH:MM:SS. I am attempting to insert a call method into this existing macro that analyzes that column, determines based on the time if the shift start was AM or PM and also change the timestamp to either MM-DD-YYYY 08:00:00 or MM-DD-YYYY 20:00:00. I can do that. Where I am having trouble is if the timestamp is the first day of the month and before 08:00:00, I need to change the timestamp to the last day of the previous month to the 20:00:00 timestamp. I can't for the life of me figure out how to do it. Tried 37000 different ways and can't make it work. My lack of knowledge has brought me to an impasse. Looking forward, I would also need to figure out if it's the first day of the year, less than 08:00:00, how to change it to the last day of the last month of the previous year. You will see from my code that I realize at the end of the for each loop, I need to re-combine the timestamp and the intention is to put it as a variable in the strsql query I have set up. In the end this is all in an effort to return the correct shift to match with the original macros data. I am wide open to any and all suggestions. I will post code below and I apologize in advance, I know it's sloppy, but I did state that I am a fabricobbler.
Sub GetShift()
Dim strsql As String
Dim i As Long
Dim j As Variant
Dim iHour As Variant
Dim iMinute As Variant
Dim iSecond As Variant
Dim iMonth As Variant
Dim iDay As Variant
Dim iYear As Variant
Dim NewDate As Variant
Dim ary As Variant
Dim V_Beg_Date As String
Dim V_End_Date As String
V_Beg_Date = Sheet1.Stime.Value
V_End_Date = Sheet1.Etime.Value
Sheets("Data").Select
Range("C2", Range("C" & Rows.Count).End(xlUp)).Select
ary = Selection
For Each j In ary
iMonth = Month(j)
iDay = Day(j)
iYear = Year(j)
iHour = Hour(j)
iMinute = Minute(j)
iSecond = Second(j)
Debug.Print iMonth, iDay, iYear
Debug.Print iHour, iMinute, iSecond
'NewDate = ("imonth":"iday":"iyear" "ihour":"iminute":"isecond")
'Debug.Print NewDate
'Calculating the correct Month
If iDay = "01" Then
If iHour < "08" Then
iMonth = WorksheetFunction.RoundDown(iMonth() - 1, -1)
Else: iMonth = iMonth
End If
End If
If iDay >= "01" Then
If iHour >= "08" Then
If iDay <= "31" Then
iMonth = iMonth
End If
End If
End If
'Calculating the correct day
If iHour < "08" Then
iDay = iDay - (1440 / 1440)
Else: iDay = iDay
End If
'Calculating the correct year
If iDay = "01" Then
If iMonth = "01" Then
If iHour < "08" Then
iYear = iYear - 1
'iMonth = "12"
Else: iYear = iYear
End If
End If
End If
'Comparing time frames to determine day shift (08:00) and night shift (20:00)
If iHour >= 8 Then
If iHour < 20 Then
iHour = "08"
End If
End If
If iHour <> 8 Then
iHour = 20
End If
'Changing minutes to 00
If iMinute <> "00" Then
iMinute = "00"
End If
'Changing seconds to 00
If iSecond <> "00" Then
iSecond = "00"
End If
Debug.Print j
'Debug.Print k
'Debug.Print NewDate
strsql = "SELECT Hist_Shift_Code as Team"
strsql = strsql & " From 1802_Shift"
strsql = strsql & " where '" & j & "' <= Hist_Shift_Start"
strsql = strsql & " and '" & j & "' >= Hist_Shift_Start"
'strsql = strsql & " where ""Hist_Shift_Start"" >= '" & j & "' "
'strsql = strsql & " and ""Hist_Shift_Start"" <= '" & k & "'"
Debug.Print strsql
Debug.Print iMonth, iDay, iYear
Debug.Print iHour, iMinute, iSecond
server = "ODBC;DSN=AEBRS"
Next
'Next
On Error GoTo ErrorHandler
With ActiveSheet.QueryTables.Add(server, _
Destination:=Range("A1"))
.Sql = (strsql)
.FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
' End If
' Next I
ErrorHandler: ' Error-handling routine.
End Sub
Bookmarks