VBA does not run

rhmkrmi

Board Regular
Joined
Aug 17, 2012
Messages
164
Hello, I am using this VBA code but it does not run automatically when I enter data in the sheet, I have to run it manually!

Sub Pop_Up()
Dim cell As Range
For Each cell In Range("c1:U439")
If IsDate(cell.Value) Then
If cell.Value < Now() Then
MsgBox "Holdback Expired:" & vbCrLf & cell.Value & " " & cell.Offset(0, -1).Value
End If
End If
Next cell
End Sub

Thank you for your help!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,135
Office Version
365
Platform
Windows
In order to run a macro automatically upon entering data in a sheet, you would need to use an Event Procedure, specifically, a Worksheet_Change event procedure in this case.
(see here: http://www.cpearson.com/excel/Events.aspx).

So, adapting your code, I think this will do what you want. Note that you MUST put this in the proper sheet module.
To do that, go to the Sheet you want this to run against, right-click on the sheet tab name at the bottom of the screen, select View Code, and paste this code in the resulting VB Editor window:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range

    Set rng = Intersect(Range("C1:U439"), Target)
    If rng Is Nothing Then Exit Sub
    
    For Each cell In Intersect(Range("C1:U439"), Target)
        If IsDate(cell.Value) Then
            If cell.Value < Now() Then
                MsgBox "Holdback Expired:" & vbCrLf & cell.Value & " " & cell.Offset(0, -1).Value
            End If
        End If
    Next cell

End Sub
 

rhmkrmi

Board Regular
Joined
Aug 17, 2012
Messages
164
Thank you.

Could you please help me add two more ranges in addition to "C1:U439"? I need to add "A1:A439" and "Z1:Z439" to the range that this code covers.

Also, could you please explain to me what this bit does in the code
& vbCrLf & cell.Value & " " & cell.Offset(0, -1).Value

Thanks again.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,135
Office Version
365
Platform
Windows
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range, rng1 As Range
    Dim cell As Range

    Set rng1 = Range("A1:A439, Z1:Z439, C1:U439")
    Set rng = Intersect(rng1, Target)
    If rng Is Nothing Then Exit Sub
    
    For Each cell In Intersect(Range("A1:A439, Z1:Z439, C1:U439"), Target)
        If IsDate(cell.Value) Then
            If cell.Value < Now() Then
                MsgBox "Holdback Expired:" & vbCrLf & cell.Value & " " & cell.Offset(0, -1).Value
            End If
        End If
    Next cell

End Sub
Also, could you please explain to me what this bit does in the code & vbCrLf & cell.Value & " " & cell.Offset(0, -1).Value
I just pulled that from your original code (you were using that in your original code)!

Note that "&" just combines different pieces of a string together.
"vbCrLf" is a carriage return and line feed
"cell.Value" is just the value of the cell you just updated
"cell.Offset(0,-1).Value" is the value of the cell one column to the left of the cell you just updated
 
Last edited:

rhmkrmi

Board Regular
Joined
Aug 17, 2012
Messages
164
Thank you so much.
I am a new starter and my knowledge is very limited so thanks for your explanation.
I copied the code from another post on a different web site, I did not write it.

Thanks again.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,135
Office Version
365
Platform
Windows
You are welcome.

I copied the code from another post on a different web site, I did not write it.
Ah, OK.

I do not know if you are or aren't, but just note that if you are also using other sites, if you do happen to post the same questions on other sites, we ask that you please follow our guidelines on Cross-Posting, as defined here in rule 13: https://www.mrexcel.com/forum/board-announcements/99490-forum-rules.html (note that almost all other forums have similar rules pertaining to Cross-Posting too).
 

Forum statistics

Threads
1,077,918
Messages
5,337,180
Members
399,131
Latest member
Vinnyjuice

Some videos you may like

This Week's Hot Topics

Top