Results 1 to 6 of 6

Thread: Fix a For Each statement :please:

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Question Fix a For Each statement :please:

    How do I tied this If Else statements into the For Each? This is what I want to do but am unable to write it correctly.. Any help would be appreciated.

    VB Code:
    1. For Each MyCell In Sheet1.Columns("J").Cells
    2.         With MyCell
    3.             If .Value < "04/30/2006" Then
    4.                 For Each MyCell In Sheet1.Columns("A").Cells
    5.                 With MyCell
    6.                     If .Value = "" Then Exit For
    7.                     If .Value = 574 _
    8.                     Or .Value = 555 _
    9.                     Or .Value = 510 Then
    10.                     .Offset(0, 12).Value = .Offset(0, 8).Value * 1.1
    11.                     Else
    12.                         For Each MyCell In Sheet1.Columns("D").Cells
    13.                         With MyCell
    14.                             If .Value <> "OMI" Then
    15.                             .Offset(0, 12).Value = .Offset(0, 8).Value * 1.4503
    16.                             Else
    17.                             .Offset(0, 12).Value = .Offset(0, 8).Value
    18.                             End If
    19.                         End With
    20.                         Next MyCell
    21.                     End If
    22.                 End With
    23.                 Next MyCell
    24.             Else
    25.             If .Value > "04/30/2006" Then
    26.                 For Each MyCell In Sheet1.Columns("A").Cells
    27.                 With MyCell
    28.                     If .Value = "" Then Exit For
    29.                     If .Value = 574 _
    30.                     Or .Value = 555 _
    31.                     Or .Value = 510 Then
    32.                     .Offset(0, 12).Value = .Offset(0, 8).Value * 1.2 'change later
    33.                     Else
    34.                         For Each MyCell In Sheet1.Columns("D").Cells
    35.                         With MyCell
    36.                             If .Value <> "OMI" Then
    37.                             .Offset(0, 12).Value = .Offset(0, 8).Value * 1.5 ' change later
    38.                             Else
    39.                             .Offset(0, 12).Value = .Offset(0, 8).Value
    40.                             End If
    41.                         End With
    42.                         Next MyCell
    43.                     End If
    44.                 End With
    45.                 Next MyCell
    46.             End If
    47.         End With
    48.         Next MyCell
    49. End Sub

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Fix a For Each statement :please:

    Von
    You can't use the MyCell Variable in nested loops, as you have here.

    Here's a simple example of what works and doesn't work in loops
    VB Code:
    1. 'This doesn't work
    2. For x = 1 To 2
    3.     For x = 10 To 20
    4.         'some code here
    5.     Next x
    6. Next x
    7.  
    8. 'This works
    9. For x = 1 To 2
    10.     For Y = 10 To 20
    11.         'some code here
    12.     Next Y
    13. Next x

    In your code you will need to replace the "inner" occurances of MyCell with a new object variable. In the same way that I have replaced the "inner" occurances of X with Y in the above.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Fix a For Each statement :please:

    Hmm...Like this maybe?
    VB Code:
    1. For Each MyCell1 In Sheet1.Columns("J").Cells
    2.         With MyCell1
    3.             If .Value < "04/30/2006" Then
    4.                 For Each MyCell2 In Sheet1.Columns("A").Cells
    5.                 With MyCell2
    6.                     If .Value = "" Then Exit For
    7.                     If .Value = 574 _
    8.                         Or .Value = 555 _
    9.                         Or .Value = 510 Then
    10.                         .Offset(0, 12).Value = .Offset(0, 8).Value * 1.1
    11.                     End If
    12.                     End With
    13.                 Next MyCell2
    14.             Else
    15.                 For Each MyCell3 In Sheet1.Columns("D").Cells
    16.                 With MyCell3
    17.                     If .Value <> "OMI" Then
    18.                         .Offset(0, 12).Value = .Offset(0, 8).Value * 1.4503
    19.                     Else
    20.                         .Offset(0, 12).Value = .Offset(0, 8).Value
    21.                     End If
    22.                     End With
    23.                 Next MyCell3
    24.             End If
    25.             End With
    26.         Next MyCell1
    27.             Else
    28.             If .Value > "04/30/2006" Then
    29.                 For Each MyCell2 In Sheet1.Columns("A").Cells
    30.                 With MyCell2
    31.                     If .Value = "" Then Exit For
    32.                     If .Value = 574 _
    33.                         Or .Value = 555 _
    34.                         Or .Value = 510 Then
    35.                         .Offset(0, 12).Value = .Offset(0, 8).Value * 1.2 'change later
    36.                     End If
    37.                     End With
    38.                 Next MyCell2
    39.             Else
    40.                 For Each MyCell3 In Sheet1.Columns("D").Cells
    41.                 With MyCell3
    42.                     If .Value <> "OMI" Then
    43.                         .Offset(0, 12).Value = .Offset(0, 8).Value * 1.5 'change later
    44.                     Else
    45.                         .Offset(0, 12).Value = .Offset(0, 8).Value
    46.                     End If
    47.                     End With
    48.                 Next MyCell3
    49.             End If
    50.             End With
    51.         Next MyCell1

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Fix a For Each statement :please:

    The following code appears about midway down in your listing ...
    Code:
            Next MyCell1
                Else
    Where is the "If" that is associated with this "Else"? You need to straighten out your indentation, and perhaps comment the levels of "If" and "With".
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Fix a For Each statement :please:

    I took that Else out (it was there by mistake) I will comment on the If and With and maybe you can help Please..

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Fix a For Each statement :please:

    Hope this helps:
    VB Code:
    1. ' use this statement for all values in column J less then the date of 04/30/2006
    2.     For Each MyCell1 In Sheet1.Columns("J").Cells
    3.         With MyCell1
    4.             If .Value < "04/30/2006" Then
    5.             ' If less then 04/30/2006 then use this statement otherwise End If
    6.                 For Each MyCell2 In Sheet1.Columns("A").Cells
    7.                 With MyCell2
    8.                 ' If value is blank Exit For
    9.                     If .Value = "" Then Exit For
    10.                     ' If value is 574, 555, or 510 then times value by 1.1
    11.                     If .Value = 574 _
    12.                         Or .Value = 555 _
    13.                         Or .Value = 510 Then
    14.                         .Offset(0, 12).Value = .Offset(0, 8).Value * 1.1
    15.                     End If
    16.                     End With
    17.                 Next MyCell2
    18.                 'If the value is not 574,555,or 510 then do this statement
    19.             Else
    20.                 For Each MyCell3 In Sheet1.Columns("D").Cells
    21.                 With MyCell3
    22.                 'If the value of column D is not equal to OMI then times value by 1.4503
    23.                     If .Value <> "OMI" Then
    24.                         .Offset(0, 9).Value = .Offset(0, 5).Value * 1.4503
    25.                         'If it is equal to OMI then do this
    26.                     Else
    27.                         .Offset(0, 9).Value = .Offset(0, 5).Value
    28.                     End If
    29.                     End With
    30.                 Next MyCell3
    31.             End If
    32.             End With
    33.         Next MyCell1
    34.  
    35. ' ______________________________________________________________________________________
    36.        
    37. ' use this statement for all values in column J greater then the date of 04/30/2006
    38.     For Each MyCell1 In Sheet1.Columns("J").Cells
    39.         With MyCell1
    40.             If .Value > "04/30/2006" Then
    41.                 For Each MyCell2 In Sheet1.Columns("A").Cells
    42.                 With MyCell2
    43.                     If .Value = "" Then Exit For
    44.                     If .Value = 574 _
    45.                         Or .Value = 555 _
    46.                         Or .Value = 510 Then
    47.                         .Offset(0, 12).Value = .Offset(0, 8).Value * 1.2 'change later
    48.                     End If
    49.                     End With
    50.                 Next MyCell2
    51.             Else
    52.                 For Each MyCell3 In Sheet1.Columns("D").Cells
    53.                 With MyCell3
    54.                     If .Value <> "OMI" Then
    55.                         .Offset(0, 9).Value = .Offset(0, 5).Value * 1.5 'change later
    56.                     Else
    57.                         .Offset(0, 9).Value = .Offset(0, 5).Value
    58.                     End If
    59.                     End With
    60.                 Next MyCell3
    61.             End If
    62.             End With
    63.         Next MyCell1
    64. End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width