+ Reply to Thread
Results 1 to 11 of 11

For Next Loop not Working

  1. #1
    Registered User
    Join Date
    12-14-2015
    Location
    Bellingham, WA
    MS-Off Ver
    2013
    Posts
    27

    For Next Loop not Working

    Hi Folks.

    Here is some VBA code that I actually received from someone on the excel forum. I've had to change references and what not as I'm using a different workbook now. I'm not sure what I have wrong but I wonder if you can tell what I'm trying to do from the code I wrote? I'm trying to get a date stamp (just the date) to code in Column U if the cell in Column J says "Withdrawn" or "Transferred". See the workbook attached. Any idea where I'm going wrong?

    Thanks.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column <> 11 Or Target.Row = 1 Or Target.Value = "Verified" Or Target.Value = "In Process" Then Exit Sub
    Dim wsPD As Worksheet: Set wsPD = Worksheets("Product Data")
    Dim r As Range, lr As Long, rngData As Range, blnFnd As Boolean
    With wsPD
    lr = .Cells(Rows.Count, "J").End(xlUp).Row
    Set rngData = .Range("J2:J" & lr)
    End With
    For Each r In rngData
    If r.Value = "Withdrawn" Or "Transferred" Then
    r(1, 21).Value = Date
    blnFnd = True
    End If
    Next r

    End Sub

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: For Next Loop not Working

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    12-14-2015
    Location
    Bellingham, WA
    MS-Off Ver
    2013
    Posts
    27

    Re: For Next Loop not Working

    Re-submitting this code with the proper tags...

    Please Login or Register  to view this content.
    Attached Files Attached Files

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

    Re: For Next Loop not Working

    Try this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: For Next Loop not Working

    First of all Column J = 10 not 11.
    Secondly why go through the entire range every time you make a change in column J ?
    Do you want to check an entire existing range or do you want to put the Datestamp every time you make a change in column J ?
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  6. #6
    Registered User
    Join Date
    12-14-2015
    Location
    Bellingham, WA
    MS-Off Ver
    2013
    Posts
    27

    Re: For Next Loop not Working

    Ah thanks I forgot VBA starts with 0.

    Yes, I want to put a timestamp in Column U IF Column J is "Withdrawn" or "Transferred"...if not those two values...then no date.

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

    Re: For Next Loop not Working

    Column numbering doesn't start at 0.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: For Next Loop not Working

    My point is that a Worksheet_change event is only used when you want something to happen every time you change a single value. Otherwise you write code in a normal module to check an entire column and make the necessary changes. This code is then run only once every time you see fit.
    Please Login or Register  to view this content.
    Or to check entire column once.
    Please Login or Register  to view this content.
    Last edited by bakerman2; 05-31-2016 at 09:06 PM.

  9. #9
    Registered User
    Join Date
    12-14-2015
    Location
    Bellingham, WA
    MS-Off Ver
    2013
    Posts
    27

    Re: For Next Loop not Working

    Ah, ok. So you're saying because there are two possible values "Withdrawn" and "Transferred" the worksheet_change doesn't work because it's only looking for one value to change?

    I'll try what you wrote in a module.

  10. #10
    Registered User
    Join Date
    12-14-2015
    Location
    Bellingham, WA
    MS-Off Ver
    2013
    Posts
    27

    Re: For Next Loop not Working

    I'm getting this error message in the module I copied from your thread:

    Compile Error: Next without For

    There is obviously a For in the code...what does this mean?

  11. #11
    Registered User
    Join Date
    12-14-2015
    Location
    Bellingham, WA
    MS-Off Ver
    2013
    Posts
    27

    Re: For Next Loop not Working

    I figured it out...it was missing an End If clause...

    Thank you for your help!

+ 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] Do Until Loop Not Working
    By jjl287 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-30-2015, 04:49 PM
  2. For loop within loop not working correctly
    By HalPlz in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-25-2015, 12:06 AM
  3. [SOLVED] For each loop not working
    By schzuki in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-31-2014, 09:54 AM
  4. Loop not working...
    By yunesm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2011, 08:12 AM
  5. Loop not working
    By blankus in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-14-2009, 04:16 AM
  6. [SOLVED] Why is this Do Loop not working right?
    By evilrtc in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-21-2007, 01:52 AM
  7. [SOLVED] Do...Loop not working
    By Sunny Lin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2005, 09:06 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