+ Reply to Thread
Results 1 to 5 of 5

Tiny macro won't write cell value based on comparing dates

Hybrid View

  1. #1
    Registered User
    Join Date
    10-05-2010
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    23

    Tiny macro won't write cell value based on comparing dates

    Hi,

    This is a tiny macro but I can't get it to work.

    It's trying to compare if the months in columns C & D are the same. If TRUE then if column F > 0 then write 1 into column G on the relevant row. If FALSE do nothing.

    I've attached a sample file with the code and some data to help you to see what I am doing.

    Appreciate if anyone can find the problem with my newbie macro.

    thanks
    Attached Files Attached Files
    Last edited by Poulan; 11-09-2010 at 01:02 PM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Tiny macro won't write cell value based on comparing dates

    Hi Poulan

    Try this
    Sub Test()
    Dim y As Long, LastRow As Long
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        For y = LastRow To 2 Step -1
            Select Case Month(Cells(y, "C").Value)
                Case Is = Month(Cells(y, "D").Value)
                    If Cells(y, "F").Value > 0 Then
                        Cells(y, "AG").Value = 1
                    End If
            End Select
        Next y
    End Sub
    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    10-05-2010
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    23

    Re: Tiny macro won't write cell value based on comparing dates

    Quote Originally Posted by jaslake View Post
    Hi Poulan

    Try this
    Sub Test()
    Dim y As Long, LastRow As Long
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        For y = LastRow To 2 Step -1
            Select Case Month(Cells(y, "C").Value)
                Case Is = Month(Cells(y, "D").Value)
                    If Cells(y, "F").Value > 0 Then
                        Cells(y, "AG").Value = 1
                    End If
            End Select
        Next y
    End Sub
    John
    I tried it but it didn't seem to do anything although it didn't give any errors. Did it work for you in the sample sheet?

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Tiny macro won't write cell value based on comparing dates

    Hi Poulan

    Yes it worked for me. You have one record that meets the criteria. See attached.

    Let me know of issues.

    John
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Tiny macro won't write cell value based on comparing dates

    How about this formula in all of the rows of column F?
    =IF(MONTH(C1)=MONTH(D1),1,"")
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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