+ Reply to Thread
Results 1 to 2 of 2

vba to analyze timestamp, make changes to timestamp, run sql query inside for each loop

  1. #1
    Registered User
    Join Date
    04-14-2013
    Location
    henderson, tennessee
    MS-Off Ver
    365
    Posts
    42

    vba to analyze timestamp, make changes to timestamp, run sql query inside for each loop

    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

  2. #2
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    280

    Re: vba to analyze timestamp, make changes to timestamp, run sql query inside for each loo

    So if a time stamp is after shift start you move it back to the last shift start?

    Please Login or Register  to view this content.
    If it'd make you feel better using my answer because of my street cred, then you can go ahead and click Add Reputation below to improve it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. MS Power Query timestamp
    By AlanC95 in forum Excel General
    Replies: 0
    Last Post: 03-01-2021, 12:48 PM
  2. Timestamp macro that cannot be overwritten by another timestamp
    By xgingerkingx in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-12-2020, 01:06 PM
  3. Replies: 2
    Last Post: 11-09-2016, 02:23 PM
  4. SKydrive Excel Last modified Timestamp Query
    By manishgadia in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 0
    Last Post: 01-15-2014, 02:28 AM
  5. When a timestamp doesn't behave as a timestamp
    By mredekopp in forum Excel General
    Replies: 3
    Last Post: 03-07-2011, 03:39 PM
  6. How to save web query including timestamp
    By h41cyon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-15-2007, 12:13 AM
  7. [SOLVED] Need a 'last updated' timestamp for a web query
    By [email protected] in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-23-2006, 02:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1