Pull values from For/Next statement?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
485
Office Version
  1. 365
Platform
  1. Windows
Hope this makes sense.

I use the code below to search the CalendarNotesTriggerColumn range of the Calendar sheet and look for a duplicate value. If it finds a match it displays a MsgBox with relevant information from another sheet. What I can't figure out is how to add an additional value (in my case a date) from another column on the Calendar sheet (Column B, which is a date). It should read something similar to: "8254-55 (Lakeview Estates - Lot 55) already exists on 1/15/25. Do you want to create a duplicate?"

The 1/15/25 is what I need to pull from the row that has the duplicate.

VBA Code:
For Each rngCELL In Worksheets("Calendar").Range("CalendarNotesTriggerColumn").Cells
  If rngCELL.Value = Worksheets("JobGrid").Range("J1").Value Then
  Ans = MsgBox(Worksheets("JobGrid").Range("J1").Value & " (" & Worksheets("JobGrid").Range("C1").Value & " - Lot " & Worksheets("JobGrid").Range("E1").Value & ") already exists.  Do you want to create a duplicate?", vbYesNo + vbQuestion)
  If Ans = vbNo Then
  End If
Next
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I don't see where you are comparing dates. When the message box pops up it asks the user about a duplicate on a certain date.

Anyway, give this a try

VBA Code:
Sub IDontKnow()

  Dim Ans As Variant
  Dim rngCell
  Dim JobGridVal As String
  Dim RowDate As Date
  Dim cSht As Worksheet
  Dim jgSht As Worksheet

  Set cSht = Worksheets("Calendar")
  Set jgSht = Worksheets("JobGrid")
  JobGridVal = jgSht.Range("J1").Value
  
  For Each rngCell In cSht.Range("CalendarNotesTriggerColumn").Cells
    If rngCell.Value = JobGridVal Then
      RowDate = Intersect(rngCell.EntireRow, cSht.Range("B:B")).Value
      Ans = MsgBox(jgSht.Range("J1").Value & " (" & jgSht.Range("C1").Value & " - Lot " & jgSht.Range("E1").Value & ") already exists on " & Format(RowDate, "mm/dd/yy") & ".  Do you want to create a duplicate?", vbYesNo + vbQuestion)
      If Ans = vbNo Then
        'What?
      End If
    End If
  Next

End Sub
 
Upvote 0
Solution
I don't see where you are comparing dates. When the message box pops up it asks the user about a duplicate on a certain date.

Anyway, give this a try

VBA Code:
Sub IDontKnow()

  Dim Ans As Variant
  Dim rngCell
  Dim JobGridVal As String
  Dim RowDate As Date
  Dim cSht As Worksheet
  Dim jgSht As Worksheet

  Set cSht = Worksheets("Calendar")
  Set jgSht = Worksheets("JobGrid")
  JobGridVal = jgSht.Range("J1").Value
 
  For Each rngCell In cSht.Range("CalendarNotesTriggerColumn").Cells
    If rngCell.Value = JobGridVal Then
      RowDate = Intersect(rngCell.EntireRow, cSht.Range("B:B")).Value
      Ans = MsgBox(jgSht.Range("J1").Value & " (" & jgSht.Range("C1").Value & " - Lot " & jgSht.Range("E1").Value & ") already exists on " & Format(RowDate, "mm/dd/yy") & ".  Do you want to create a duplicate?", vbYesNo + vbQuestion)
      If Ans = vbNo Then
        'What?
      End If
    End If
  Next

End Sub

Sorry I wasn’t clear. It looks for duplicate job numbers. If it finds one it reports back. If it finds a duplicate I need it to pull the date where it is. I’ll try your code tomorrow. Thanks!
 
Upvote 0
I don't see where you are comparing dates. When the message box pops up it asks the user about a duplicate on a certain date.

Anyway, give this a try

VBA Code:
Sub IDontKnow()

  Dim Ans As Variant
  Dim rngCell
  Dim JobGridVal As String
  Dim RowDate As Date
  Dim cSht As Worksheet
  Dim jgSht As Worksheet

  Set cSht = Worksheets("Calendar")
  Set jgSht = Worksheets("JobGrid")
  JobGridVal = jgSht.Range("J1").Value
 
  For Each rngCell In cSht.Range("CalendarNotesTriggerColumn").Cells
    If rngCell.Value = JobGridVal Then
      RowDate = Intersect(rngCell.EntireRow, cSht.Range("B:B")).Value
      Ans = MsgBox(jgSht.Range("J1").Value & " (" & jgSht.Range("C1").Value & " - Lot " & jgSht.Range("E1").Value & ") already exists on " & Format(RowDate, "mm/dd/yy") & ".  Do you want to create a duplicate?", vbYesNo + vbQuestion)
      If Ans = vbNo Then
        'What?
      End If
    End If
  Next

End Sub

This works fine. Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,543
Messages
6,179,429
Members
452,914
Latest member
echoix

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