Norie, a friend just sent me this piece of code. he has helped me out by rewriting everything, but I still need it to do one thing that is missing from the code I wrote ....
can you help me with this last piece? its a bit advanced for me, perhaps you can understand it better than I?
This code copies all the punch in/out times over correctly, but I also need it to copy the LUNCH value or ADJ values when there is an OUTNOTE letter present. The LUNCH and ADJ values need to be copied into the OT/Adjust Hours column in the target timesheet. Do you understand? Could you help?
Sub NewTimecardImport()
'
'
'// Where the various columns are. Done like this for 2 reasons.
'// 1. The code is simpler and easy to follow
'// 2. If the structure changes then only one change in the code needed.
Const cCSV_Indate As Long = 11
Const cCSV_InTime As Long = 13
Const cCSV_InType As Long = 15
Const cCSV_OutDate As Long = 19
Const cCSV_OutTime As Long = 21
Const cCSV_OutType As Long = 23
Const cCSV_OutNote As Long = 24
Const cXLS_Date As Long = 1
Const cXLS_AMIn As Long = 2
Const cXLS_AMOut As Long = 4
Const cXLS_PMIn As Long = 6
Const cXLS_PMOut As Long = 8
Const cXLS_Type As Long = 12
'// A general range to loop down the incoming file using the INDATE column
Dim r As Excel.Range
'// Used to find the incoming date in the XL sheet
Dim rFind As Excel.Range
'// This will hold the row number in the timesheet (XL file) being processed
Dim lXLRow As Long
'// Original variables
Dim filter As String
Dim caption As String
Dim sourceFilename As Variant
Dim targetWorkbook As Workbook
Dim sourceWorkbook As Workbook
Dim targetSheet As Worksheet
Dim sourceSheet As Worksheet
Dim targetRange As Range
Dim sourceRange As Range
'
' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
Set targetSheet = targetWorkbook.Worksheets(ActiveSheet.Name)
Set targetRange = targetSheet.Range("A14").Resize(targetSheet.Range("A14").End(xlDown).Row - 13)
'
' get source workbook ...
filter = "PayChex Data (*.csv),*.csv"
caption = "Select PayChex Data Source to Import"
'// ********************************************
'// Note changes to path - just in case I forget
'// ********************************************
ChDrive ("c")
ChDir ("c:\")
sourceFilename = Application.GetOpenFilename(filter, 1, caption, , False)
If sourceFilename = False Then
' cancel was pressed
MsgBox "Import canceled. No data source was selected.", vbExclamation, "Select PayChex Data Source to Import"
Exit Sub
Else
' make association that newly opened data workbook is the source
Set sourceWorkbook = Application.Workbooks.Open(sourceFilename)
End If
'
'
' IMPORT/COPY DATA INTO TIMECARD WORKBOOK
'
' make source workbook associations
Set sourceSheet = sourceWorkbook.Worksheets(1)
'// The -1 as the RESIZE is starting in Row 2, not Row 1
For Each r In sourceSheet.Cells(2, cCSV_Indate).Resize(sourceSheet.Cells(2, cCSV_Indate).End(xlDown).Row - 1)
'// A general sense check - this seems to be generated by a timeclock so should
'// always be correct
If IsDate(r.Value) Then
'// Find the date in the timesheet
Set rFind = targetRange.Find(What:=r.Value)
If Not rFind Is Nothing Then
'// Store the row number
lXLRow = rFind.Row
'// Check the 'transaction' type. If not 0 then this seems to be an exception
If sourceSheet.Cells(r.Row, cCSV_InType).Value = 0 Then
'// First record always assumed to be AM in...
If targetSheet.Cells(lXLRow, cXLS_AMIn).Value = vbNullString Then
targetSheet.Cells(lXLRow, cXLS_AMIn).Value = Format$(Round(TimeValue(sourceSheet.Cells(r.Row, cCSV_InTime).Value) * 96, 0) / 96, "hh:mm")
targetSheet.Cells(lXLRow, cXLS_AMOut).Value = Format$(Round(TimeValue(sourceSheet.Cells(r.Row, cCSV_OutTime).Value) * 96, 0) / 96, "hh:mm")
Else
targetSheet.Cells(lXLRow, cXLS_PMIn).Value = Format$(Round(TimeValue(sourceSheet.Cells(r.Row, cCSV_InTime).Value) * 96, 0) / 96, "hh:mm")
targetSheet.Cells(lXLRow, cXLS_PMOut).Value = Format$(Round(TimeValue(sourceSheet.Cells(r.Row, cCSV_OutTime).Value) * 96, 0) / 96, "hh:mm")
End If
Else
'// Record the exception
targetSheet.Cells(lXLRow, cXLS_Type).Value = sourceSheet.Cells(r.Row, cCSV_OutNote).Value
End If
End If
End If
Next
MsgBox "PayChex Timecard Data Successfully Imported." & vbNewLine & "Be sure to save your Timecard upon exit." & vbNewLine & vbNewLine & "You may now enter the remainder of your schedule" & vbNewLine & "for this pay period.", vbInformation, "PayChex Data Import Macro"
'
'
' Close source workbook
sourceWorkbook.Close False
End Sub
Bookmarks