VBA does not run

rhmkrmi

Active Member
Joined
Aug 17, 2012
Messages
348
Office Version
  1. 365
Platform
  1. Windows
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!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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
 
Upvote 0
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.
 
Upvote 0
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:
Upvote 0
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.
 
Upvote 0
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).
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top