+ Reply to Thread
Results 1 to 27 of 27

For Each Statement confusion!

Hybrid View

  1. #1
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Thumbs up For Each Statement confusion!

    xpost: http://www.ozgrid.com/forum/showthre...810#post681810
    xpost: http://www.mrexcel.com/forum/excel-q...ml#post3559652

    I'm in the middle of finishing a lengthy macro and cannot get my head around how to make this For Each statement work properly. Any brave souls up for a challenge?

    -----------------------------------------

    Calling all Excel Experts!! Your help is desperately needed!! Will anyone please help me solve this issue?

    Visit the link https://app.box.com/s/r4k7nvgzwk7wqq0ciqjj and read the _README file (it is also below!) It will help to understand how this macro is supposed to work. There are screenshots and descriptions clearly explaining the steps. All files can be previewed in your browser and do not require download.

    I have re-uploaded new files to this post below (labeled with a "ver2"). The source worksheet is a .csv file saved as .xls because ozgrid does not allow .csv files to be uploaded. You will have to resave the source file from ".csv.xls" to ".csv" when you save it to your hard drive locally to make it work.

    The macro is in the target workbook file. Best thing would be to allow all the formatting to run and start stepping through it at the point in the code where the IMPORTING BEGINS with the For Each Statement (near the end of code).
    Last edited by joshisms; 08-28-2013 at 01:48 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: For Each Statement confusion!

    The ExUser3-20130729 (SOURCE WS ver2).csv.xls is not an xls file so can't be opened.

    It does open if the extension is changed from .csv.xls to just .xls.

    As for the problem, any chance you could tell us what it is? Or do we have to wade through the code to find it ourselves.

    Oh, and it might be useful to know how to run the code.

    PS This sort of code,
        ActiveSheet.Range("M2").Select
        Do Until IsEmpty(ActiveCell.Offset(, -1))
            ActiveCell = Format(ActiveCell, "[$-409]h:mm AM/PM;@")
            ' Step down 1 row from present location.
            ActiveCell.Offset(1, 0).Select
        Loop
    isn't a good idea.

    You should avoid using ActiveSheet and looping.
      With sourceWorkbook.Sheets(1) ' it's a CSV so only had one sheet.
        .Range("M2", .Range("M" & Rows.Count).NumberFormat = [$-409]h:mm AM/PM;@"
      End With
    Note I've changed the code to use NumberFormat as if you use Format you could end up with text values in the cells rather than actual time values, and that might cause problems later if doing any calculations, comparisons, searches...
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    Hi Norie, Thank you for replying. If you don't mind, please read the _README PDF document for a second. It should explain a lot of questions you might have, but hopefully my explanation below helps too!

    Quote Originally Posted by Norie View Post
    The ExUser3-20130729 (SOURCE WS ver2).csv.xls is not an xls file so can't be opened. It does open if the extension is changed from .csv.xls to just .xls.
    The source worksheet is a .csv file saved as .xls because ozgrid does not allow .csv files to be uploaded. You will have to resave the source file from ".csv.xls" to ".csv" when you save it to your hard drive locally to make it work.

    WHAT IT DOES:
    The entire macro is designed to import timecard punches from on online system to a workbook that looks like a timecard. the online timecard punches are the source data and the timecard workbook where the data goes is the target.

    The source csv file is formatted before any data is copied/imported to the target worksheet. Basically the source file dates/times are rounded and formatted as dates/times before copy/import process begins. the import process is simply copying all punch in/out times based on matching dates between the worksheets.

    Quote Originally Posted by Norie View Post
    Oh, and it might be useful to know how to run the code.
    The macro is in the target workbook file. Alt-8, Edit, Run? Best thing would be to allow all the formatting to run by putting a stop at the point in the code where the IMPORTING BEGINS with the For Each Statement (near the end of all the code).

    Quote Originally Posted by Norie View Post
    As for the problem...
    The problem is that as the data from the csv files is imported into the target workbook, it stops when it encounters no OutDate in the source file. This portion of the code tests whether or not the indate matches the outdate, if they don't match, the outdate is tested to see if it is a valid date. if outdate is not a valid date there is always a letter in one of the cells to the right of the outdate. i want to read what that code is and copy it and the values to its right to the target workbook. I set up a msgbox for testing purposes to let me know when I started receiving errors.

    Else ' step 2 - b/c InDate.source = OutDate.source on same row is false, goto step 2a
                    ' step 2a - test if OutDate.source is a valid date
                    If IsDate(sourceInDate.Offset(0, 9).Value) = False Then ' if NOT valid date
                        MsgBox "OutDate is NOT valid date" ' DELETE temporary msg when done testing
                        ' Call outNote (placeholder)
     '                   
                        '[outNote]
                        If sourceInDate.Offset(0, 15).Value <> "" Then
                            If sourceInDate.Offset(0, 15).Value = "V" Or sourceInDate.Offset(0, 15).Value = "v" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "S" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "P" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "F" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "H" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "L" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 18).Value * -1 ' copy time in adjLunch.source to OTadjHours.target
                            End If
                        End If
                        '[/outNote]
    Last edited by joshisms; 08-28-2013 at 02:38 PM. Reason: thread clarity

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: For Each Statement confusion!

    Where does that code compare the 2 dates?

  5. #5
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    step 2 - see below. I tried to tie everything together in the comments because there are so many different If's and Else's but its easy to get them confused. The step name is before the If statement and after the else.

     ' step 2 - does InDate.source (sourceSheet.Range("K2").Value) = OutDate.source on same row (sourceSheet.Range("T2").Value)
     If sourceInDate.Value = sourceInDate.Offset(0, 9).Value Then  ' if match goto step 3
    Ive red bolded same code below

    For Each targetDate In targetRange ' selects first date in range in target ws
        For Each sourceInDate In sourceRange ' selects first date in range in source ws
            ' step 1 - does InDate.source (sourceSheet.Range("K2").Value) = Date.target (targetSheet.Range("A14").Value)
            If sourceInDate.Value = targetDate.Value Then ' if match goto step 2
                ' step 2 - does InDate.source (sourceSheet.Range("K2").Value) = OutDate.source on same row (sourceSheet.Range("T2").Value)
                If sourceInDate.Value = sourceInDate.Offset(0, 9).Value Then  ' if match goto step 3                ' step 3 - does InDate.source = PreviousRow.InDate.source
                    If sourceInDate.Value <> sourceInDate.Offset(-1, 0).Value Then ' if true, meaning NOT equal, goto step 3a
                        ' step 3a - compare OutTime.source (sourceInDate.Offset(0, 12).Value) > InTime.source (sourceInDate.Offset(0, 3).Value)
                        If sourceInDate.Offset(0, 12).Value > sourceInDate.Offset(0, 3).Value Then ' if true
                            ' copy same row InTime.source to InTime.target
                            targetDate.Offset(0, 1).Value = sourceInDate.Offset(0, 3).Value
                            ' copy OutTime.source to LunchOut.target
                            targetDate.Offset(0, 3).Value = sourceInDate.Offset(0, 12).Value
                            ' Call outNote (placeholder)
                        Else ' step 3a - because OutTime.source > InTime.source is false
                            ' Call outNote (placeholder)
                        End If ' step 3a
                    Else ' step 3 - b/c InDate.source = PreviousRow.InDate.source is false, meaning they ARE equal, goto step 3b
                        ' step 3b - compare OutTime.source > InTime.source
                        If sourceInDate.Offset(0, 12).Value > sourceInDate.Offset(0, 3).Value Then ' if true goto step 3b1
                            ' step 3b1 - compare CurrentRow.InTime.source > PreviousRow.OutTime.source
                            If sourceInDate.Offset(0, 3).Value > sourceInDate.Offset(-1, 12).Value Then ' if true
                                ' copy same row InTime.source to LunchIn.target
                                targetDate.Offset(0, 5).Value = sourceInDate.Offset(0, 3).Value
                                ' copy OutTime.source to TimeOut.target
                                targetDate.Offset(0, 7).Value = sourceInDate.Offset(0, 12).Value
                                ' Call outNote (placeholder)
                            End If ' step 3b1
                            ' step 3b2 - does CurrentRow.InTime.source = PreviousRow.outTime.source
                            If sourceInDate.Offset(0, 3).Value = sourceInDate.Offset(-1, 12).Value Then ' if true
                                ' copy OutTime.source to LunchOut.target (replacing previous LunchOut value)
                                targetDate.Offset(0, 3).Value = sourceInDate.Offset(0, 12).Value
                                ' Call outNote (placeholder)
                            End If ' step 3b2
                        End If ' step 3b
                    End If ' step 3
                Else ' step 2 - b/c InDate.source = OutDate.source on same row is false, goto step 2a
                    ' step 2a - test if OutDate.source is a valid date
                    If IsDate(sourceInDate.Offset(0, 9).Value) = False Then ' if NOT valid date
                        MsgBox "OutDate is NOT valid date" ' DELETE temporary msg when done testing
                        ' Call outNote (placeholder)
     '                   
                        '[outNote]
                        If sourceInDate.Offset(0, 15).Value <> "" Then
                            If sourceInDate.Offset(0, 15).Value = "V" Or sourceInDate.Offset(0, 15).Value = "v" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "S" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "P" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "F" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "H" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "L" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 18).Value * -1 ' copy time in adjLunch.source to OTadjHours.target
                            End If
                        End If
                        '[/outNote]
    '                    
                    End If ' step 2a
                End If ' step 2
            End If ' step 1
        Next sourceInDate ' move down to check the next date in date.source column
    Next targetDate ' move down to check the next date in date.target column
    Last edited by joshisms; 08-28-2013 at 05:47 PM.

  6. #6
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    maybe seeing this will help to keep the Ifs/Elses straight? Download it or view as actual size otherwise it will be too small to view! https://app.box.com/s/heph5urz64h7os5we7gh
    Last edited by joshisms; 08-28-2013 at 03:16 PM.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: For Each Statement confusion!

    The code never goes past this point.
                If sourceInDate.Value = targetDate.Value Then    ' if match goto step 2
    It steps right to the End If every time, skipping all the code after the above line.

  8. #8
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    Are you asking or telling me? I'm not sure I am following you. See attached file, this is what my target workbook looks like after the macro runs. You can see it stops after correctly copying over the "V" and the 7.5 on 7/18 date. But nothing gets copied over after that ....
    Quote Originally Posted by Norie View Post
    The code never goes past this point.
                If sourceInDate.Value = targetDate.Value Then    ' if match goto step 2
    It steps right to the End If every time, skipping all the code after the above line.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    Did you rename the source file from ".csv.xls" to ".csv" before running the macro?

    Quote Originally Posted by joshisms View Post
    The source worksheet is a .csv file saved as .xls because ozgrid does not allow .csv files to be uploaded. You will have to resave the source file from ".csv.xls" to ".csv" when you save it to your hard drive locally to make it work.
    Quote Originally Posted by Norie View Post
    The code never goes past this point.
                If sourceInDate.Value = targetDate.Value Then    ' if match goto step 2
    It steps right to the End If every time, skipping all the code after the above line.

  10. #10
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    The idea here is to compare the dates in the target worksheet (timecard) with the dates in the source worksheet (data). Is there a better way to compare two dates to see if they match?

    To be clear what I wanted to have happen was to take the first date in the target worksheet ("A14") and compare it against the first date in the source worksheet after formatted ("K2"). If they match it is supposed to copy the Rounded Time In and Rounded Time Out times to the target worksheet. Once done it would keep checking the source sheet column of dates ("K") for matches to the first target date, and so on and so forth. Let me know if you want me to explain this any further.

    I just thought maybe I am comparing the dates incorrectly, which might be why the code is not moving through this section?
    Quote Originally Posted by Norie View Post
    The code never goes past this point.
                If sourceInDate.Value = targetDate.Value Then    ' if match goto step 2
    It steps right to the End If every time, skipping all the code after the above line.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: For Each Statement confusion!

    That's what happened when I stepped through the entire code.

    Yes I did rename it, otherwise it wouldn't have got past trying to open the file.

  12. #12
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    That's odd. I wonder why it doesn't go past that point? Runs fine for me. How do you know that it stops there? What happens? Does it populate your target timesheet at all? Do you get a msgbox saying "OutDate is NOT valid date"?

    Take a look at the file I uploaded: ExTimeCard (TARGET WB after macro ran).xls When I run it, you can see some output. This is where it stops for me. I am using Excel 2k3 on Win7.

    Any ideas what wrong?

    Quote Originally Posted by Norie View Post
    That's what happened when I stepped through the entire code.

    Yes I did rename it, otherwise it wouldn't have got past trying to open the file.

  13. #13
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    Are you using the modified code you wrote in your first post to me? This could be why it stops .....

    Quote Originally Posted by Norie View Post
    PS This sort of code,
        ActiveSheet.Range("M2").Select
        Do Until IsEmpty(ActiveCell.Offset(, -1))
            ActiveCell = Format(ActiveCell, "[$-409]h:mm AM/PM;@")
            ' Step down 1 row from present location.
            ActiveCell.Offset(1, 0).Select
        Loop
    isn't a good idea.

    You should avoid using ActiveSheet and looping.
      With sourceWorkbook.Sheets(1) ' it's a CSV so only had one sheet.
        .Range("M2", .Range("M" & Rows.Count).NumberFormat = [$-409]h:mm AM/PM;@"
      End With
    Note I've changed the code to use NumberFormat as if you use Format you could end up with text values in the cells rather than actual time values, and that might cause problems later if doing any calculations, comparisons, searches...

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: For Each Statement confusion!

    It doesn't actually stop there it never exectutes the code within the If End If block.

    How did I find that out? I stepped through the entire code.

    Can you tell us what exactly is the problem, in words and without uploading more files?

    Are you sure the problem is with the For Next?

    Is it not possible it could be something in the code within the loops?

    By the way, something like this,
                     If sourceInDate.Offset(0, 15).Value <> "" Then
                            If sourceInDate.Offset(0, 15).Value = "V" Or sourceInDate.Offset(0, 15).Value = "v" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "S" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "P" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "F" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "H" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "L" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 18).Value * -1 ' copy time in adjLunch.source to OTadjHours.target
                            End If
                        End If
    can probably be rewritten something like this.
                If sourceInDate.Offset(0, 15).Value <> "" Then
                   Select Case UCase(sourceInDate.Offset(0, 15).Value)
                       Case "V", "S", "F", "H"
                            targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value    ' copy letter in outNote.source to targetType.target
                            targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value    ' copy time in adjHours.source to OTadjHours.target
                       Case "L"
                            targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value    ' copy letter in outNote.source to targetType.target
                            targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 18).Value * -1    ' copy time in adjLunch.source to OTadjHours.target
                    End Select
                 End If

  15. #15
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    Yep, I already rewrote it as a case, but hadn't changed it yet!
    Quote Originally Posted by Norie View Post
    By the way, something like this,
                     If sourceInDate.Offset(0, 15).Value <> "" Then
                            If sourceInDate.Offset(0, 15).Value = "V" Or sourceInDate.Offset(0, 15).Value = "v" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "S" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "P" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "F" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "H" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value ' copy time in adjHours.source to OTadjHours.target
                            ElseIf sourceInDate.Offset(0, 15).Value = "L" Then
                                targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value ' copy letter in outNote.source to targetType.target
                                targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 18).Value * -1 ' copy time in adjLunch.source to OTadjHours.target
                            End If
                        End If
    can probably be rewritten something like this.
                If sourceInDate.Offset(0, 15).Value <> "" Then
                   Select Case UCase(sourceInDate.Offset(0, 15).Value)
                       Case "V", "S", "F", "H"
                            targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value    ' copy letter in outNote.source to targetType.target
                            targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 20).Value    ' copy time in adjHours.source to OTadjHours.target
                       Case "L"
                            targetDate.Offset(0, 11).Value = sourceInDate.Offset(0, 15).Value    ' copy letter in outNote.source to targetType.target
                            targetDate.Offset(0, 12).Value = sourceInDate.Offset(0, 18).Value * -1    ' copy time in adjLunch.source to OTadjHours.target
                    End Select
                 End If

  16. #16
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    The entire macro is designed to import timecard punches from on online system to a workbook that looks like a timecard. the online timecard punches are the source data and the timecard workbook where the data goes is the target.

    The source csv file is formatted before any data is copied/imported to the target worksheet. Basically the source file dates/times are rounded and formatted as dates/times before copy/import process begins. The import process is simply copying all punch in/out times based on matching dates between the worksheets.

    The problem is that ALL the source file punch in and out times should be imported into the target workbook but they arent. It stops after it reads a blank cell in outdate in the source sheet. This first occurs on 7/18/2013. It copies the letter code correctly and the 7.5 afterwards, but no other dates are compared and no punch in/out times are copied to the target file after this.

    I dont know why it stops when it encounters no date in the OutDate cell in the source file.

    The For Each portion of the code tests whether or not the indate matches the outdate, if they don't match, the outDate is tested to see if it is a valid date. if outDate is not a valid date (or whenever if outDate is blank) there will always be a cell to the right that contains a letter code. what it is supposed to do is copy this letter code and the time value to its right into the target worksheet and then continue hunting for matching dates to continue copying all the timeIns and timeOuts to the target worksheet.

    The msgbox is for testing purposes only to let me know when it did not find an outDate and as a way for me to stop the code, nothing more.

    Quote Originally Posted by Norie View Post
    Can you tell us what exactly is the problem, in words and without uploading more files?
    Are you sure the problem is with the For Next?
    Is it not possible it could be something in the code within the loops?
    Last edited by joshisms; 08-28-2013 at 05:56 PM. Reason: added "/"

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: For Each Statement confusion!

    You should probably do that throughout the code, it makes it easier to follow/understand and that should make it easier to debug and find problems.

    PS I don't mean replace everthing with Select Case, just kind of tidy things up.

  18. #18
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    I'll do my best, but its written very basic because I didn't know any fancier ways of doing/combining things than I did and though that if I wrote enough comments it would be easier to follow.
    Quote Originally Posted by Norie View Post
    You should probably do that throughout the code, it makes it easier to follow/understand and that should make it easier to debug and find problems.

    PS I don't mean replace everthing with Select Case, just kind of tidy things up.

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: For Each Statement confusion!

    The code doesn't stop, it keeps going after the message box.

    It might appear to have stopped because it doesn't really do anything from then on.

    Try it yourself.

    Run the code, when the message box appears hit CTRL+BREAK, then Click Debug and start stepping through with F8.

    It just seems to jump from If to If as it continues looping.

    By the way, have you considered a different approach.

    For example loop through the dates in the time card worksheet and use code to find them in the CSV file.

  20. #20
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    Sorry for delayed response, have been in school all day past 2 days.
    Any idea why it doesn't work after the msgbox? Could it have anything to do with use of offsets or a different cell being the active cell?

    Quote Originally Posted by Norie View Post
    The code doesn't stop, it keeps going after the message box.

    It might appear to have stopped because it doesn't really do anything from then on.
    I did look into using the find command when originally writing the loop, but haven't since and think its a good idea and would love some direction on how to do this! Would this be using the .find command?

    Quote Originally Posted by Norie View Post
    By the way, have you considered a different approach.

    For example loop through the dates in the time card worksheet and use code to find them in the CSV file.

  21. #21
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Find could be one way but there are others, eg Application.Match.

    As for the reason it does nothing after the message box that could be something to do with the data.

    Or it could be something to do with how the code is handling the data.

    Did you check what was happening (or not happening) as you stepped through the code?

  22. #22
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    I did step through it. Very odd since it appears to be moving through all the motions and doing what I would expect with regards to the various letter codes, etc in the source data.

    That is just very odd. Why would the code stop working if it worked up to that point? I do not understand this. Its frustrating because it looks like it is behaving/working as it should .....

  23. #23
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: For Each Statement confusion!

    Take a close look at the expressions in the If statements and the values used in them.

  24. #24
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    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

  25. #25
    Registered User
    Join Date
    08-13-2013
    Location
    CV, CA
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: For Each Statement confusion!

    Thanks Cytop, your help was much appreciated!

  26. #26
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: For Each Statement confusion!

    Probably best if you ask the person who supplied that code.

  27. #27
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: For Each Statement confusion!

    Just to clear any confusion about this code suddenly 'appearing'..

    The OP approached me over a week ago, via pm, on OzGrid asking for help with this after I had assisted with other issues.

    I had the same issues with the code as others seem to have had so I re-wrote it as I understood it.

    This was sent to the OP, by email, on Thursday last week (Aug 22nd) with the comment "...what I have done, though, is re-write the thing. Obviously this is not going to work 100% for you but it may show another way to accomplish this", OP read the email on his mobile and, it seems, never noticed the attachment.

    I have watched the various exchanges over the next few days noting that some of the comments had already been raised by me. On Wednesday last, with no reference to the file I sent, I pm'ed the OP on OzGrid saying I was withdrawing from it and would not take any further part. This prompted a reply last night which, in my opinion, was not complimentary to me. Then to see that "a friend just sent me this piece of code..." is a little much.

+ 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. [SOLVED] confusion with IF statement
    By Mortada in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-15-2012, 05:48 PM
  2. Confusion with last row
    By LiLi1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-15-2011, 06:09 PM
  3. IF Statement Confusion
    By penny in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-13-2007, 03:38 PM
  4. If statement confusion
    By DianeR in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-20-2007, 07:48 AM
  5. [SOLVED] Confusion.....
    By Eric @ CMN, Evansville in forum Excel General
    Replies: 3
    Last Post: 12-27-2005, 03:20 PM

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