Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50057

Date-based VBA macro selects wrong cell.

$
0
0
I have a VBA macro in place which should go line by line and check one date against another to check for overdue information. If the first date is more than 30 prior to today's date a message box should say "Overdue" and if it is within 30 days it should say "Okay." However, when it goes through the macro it puts "Okay" on the line below the line where it should appear. The macro is triggered by a button.

I realize the code below has additional variables which are not used, but they are necessary for the remainder of the project.

Code:

Sub CommandButton_Click()
Dim Test As String, Column As String, Row As Integer, Status As String, Name As String, NameRef As String, CompColumn As String, CompTest As String, CompDate As Date, DueDate As Date, DateToday As Date, DateTest As String

Test = ""
Column = "AA"
Row = 13
Test = Test & Column & Row
Status = ""
Name = ""
CompColumn = "Z"
CompTest = CompColumn & CStr(Row)

CompDate = CDate(Sheets("Cleco").Range(CompTest))
DueDate = DateAdd("d", 30, CompDate)

DateToday = Date

Do While Row < 27
    If DateToday > DueDate Then
        DateTest = "Overdue"
       
        CompTest = CompColumn & CStr(Row)
        CompDate = CDate(Sheets("Cleco").Range(CompTest))
        DueDate = DateAdd("d", 30, CompDate)
       
        Status = Status & vbCrLf & DateToday & " " & DueDate & " " & DateTest & " " & Row
       
        Row = Row + 1
     
    Else
        DateTest = "Okay"
       
        CompTest = CompColumn & CStr(Row)
        CompDate = CDate(Sheets("Cleco").Range(CompTest))
        DueDate = DateAdd("d", 30, CompDate)
       
        Status = Status & vbCrLf & DateToday & " " & DueDate & " " & DateTest & " " & Row
       
        Row = Row + 1
    End If
   

Loop


MsgBox Status

End Sub


Viewing all articles
Browse latest Browse all 50057

Trending Articles