VBA does not run

rhmkrmi

Board Regular
Joined
Aug 17, 2012
Messages
190
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,429
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
190
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,429
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
190
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,429
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,081,690
Messages
5,360,614
Members
400,592
Latest member
badgergurl

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top