• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Copying Data from Time sheet to Summary sheet without opening summary file-Additional checkpoint

prveenk86kumar

New Member
Hi All

I have below code which basically copies the data from one excel sheet to another excel sheet and is working fine in doing its job.I want additional control in the below code.

In my time sheet , I have restrict people from updating more then one day information, example - If I am updating information for May 1st and if excel contains information for multiple days (May,2,3,4) it should update only for 1st may. Any logic i can add like input command or referring to any cell to prefixed date.
Code:
Sub UpdateSummary()

    Dim cn As Object, cm As Object, rs As Object
    Dim dte As Double, nme As String, activity As String, sub_activity As String, upt_time As Integer, comments As String
    Dim lr As Long
    Dim cc As Range

    On Error GoTo err_handler

    Set cn = CreateObject("ADODB.Connection")
    Set cm = CreateObject("ADODB.Command")

    With cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .Properties("Data Source") = ThisWorkbook.Path & "\Summary-TimeSheet.xlsm"
        .Properties("Extended Properties") = "Excel 12.0 Macro; HDR=YES; IMEX=0"
        .Open
    End With

    cm.ActiveConnection = cn
    cm.CommandText = "SELECT Name,Date FROM [Summary$] WHERE Name = '" & ActiveSheet.Range("B2") & "' AND Date = " & CDbl(ActiveSheet.Range("A2"))
    Set rs = cm.Execute

    If Not (rs.BOF And rs.EOF) Then
        MsgBox "Data for this date has already been submitted", vbInformation
        Exit Sub
    End If

    With ActiveSheet
        lr = .Cells(.Rows.Count, "A").End(xlUp).Row

        For Each cc In .Range("A2:A" & lr)
            dte = CDbl(cc.Offset(0))
            nme = cc.Offset(, 1)
            activity = cc.Offset(, 2)
            sub_activity = cc.Offset(, 3)
            upt_time = CDbl(cc.Offset(, 4))
            comments = cc.Offset(, 5)

            cm.CommandText = "INSERT INTO [Summary$] ([Date],[Name],[Activity],[Sub Activity],[UPT Time],[Comments]) VALUES (" & _
                              dte & ", " & _
                              "'" & nme & "', " & _
                              "'" & activity & "', " & _
                              "'" & sub_activity & "', " & _
                              upt_time & ", " & _
                              "'" & comments & "')"
            cm.Execute
        Next cc
    End With

exit_handler:
    Set rs = Nothing
    Set cm = Nothing
    Set cn = Nothing

Exit Sub

err_handler:
    MsgBox "Function UpdateSummary" & vbNewLine & vbNewLine & Err.Number & ": " & Err.Description, vbCritical, "Error in Function UpdateSummary"
    Resume exit_handler

End Sub
 
prveenk86kumar
Please reread Forum Rules
Relevant words in the Title... ( and which will moderate out )
 
Back
Top