Forum Discussion
Excel VBA Updates not functioning as intended
Option Explicit
Sub CMLUpdateV3()
Dim wb As Workbook
Dim wsData As Worksheet, wsSlide As Worksheet
Dim rngDateRow As Range, rngFound As Range
Dim dtInput As Variant, NM As String, vDate As Date
Dim blnValidDate As Boolean
'--- Optimize performance
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'--- Close all other workbooks to avoid confusion
For Each wb In Application.Workbooks
If Not (wb Is ThisWorkbook) Then wb.Close SaveChanges:=False
Next wb
'--- Ask user for a valid date (InputBox Type:=1 ensures date entry)
blnValidDate = False
Do
dtInput = Application.InputBox("Please Enter File Submission Month as MM/DD/YYYY:", _
"Date Entry", , , , , , 1)
If dtInput = False Then
MsgBox "Update cancelled by user.", vbInformation
GoTo Cleanup
End If
If IsDate(dtInput) Then
NM = Format(CDate(dtInput), "MM/DD/YYYY")
vDate = CDate(NM)
blnValidDate = True
Else
MsgBox "Invalid Date Format. Please Re-Enter in MM/DD/YYYY format.", vbExclamation
End If
Loop Until blnValidDate
'--- Reference worksheets
Set wsData = ThisWorkbook.Worksheets("Data")
Set wsSlide = ThisWorkbook.Worksheets("Slide Prep")
'--- Check if date already exists in Data row 2
Set rngDateRow = wsData.Rows(2)
Set rngFound = rngDateRow.Find(What:=vDate, LookIn:=xlValues, LookAt:=xlWhole)
If Not rngFound Is Nothing Then
MsgBox "Date " & NM & " already exists on the Data sheet. Please review.", vbInformation
GoTo Cleanup
End If
'=======================
' Update Data tab
'=======================
With wsData
.Columns("C:C").Insert
.Columns("D:D").Copy
.Columns("C:C").PasteSpecial xlPasteAll
.Range("C2").Value = vDate
.Range("C2").NumberFormat = "m/d/yyyy"
'Clear old content blocks
.Range("C3:C6,C16:C19,C29:C32,C42:C45,C55:C58,C68:C71").ClearContents
'Ensure column O is static values
.Columns("O:O").Copy
.Columns("O:O").PasteSpecial xlPasteValues
End With
'=======================
' Update Slide Prep tab
'=======================
With wsSlide
.Rows("2:2").Insert
.Range("A2").Value = vDate
.Range("A2").NumberFormat = "m/d/yyyy"
'Copy formulas from previous row (B3:H3)
.Range("B3:H3").Copy
.Range("B2:H2").PasteSpecial xlPasteAll
'Reset cell format to General to ensure formulas calculate
.Range("I2:M2").NumberFormat = "General"
'Insert XLOOKUP formulas
Dim col As Long
For col = 2 To 13 'B to M
.Cells(2, col).Formula = "=XLOOKUP(" & .Cells(1, col).Address(False, False) & _
",Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
Next col
'Convert row 14 to values (per original code)
.Rows("14:14").Copy
.Rows("14:14").PasteSpecial xlPasteValues
End With
'--- Confirm success
MsgBox "New File Date " & NM & " added and sheets updated." & vbCrLf & _
"Proceed to Data tab to record Enrollment Line Counts.", vbInformation
Cleanup:
'--- Restore defaults
Application.CutCopyMode = False
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End SubThis is a suggestion🙂.
Hope it helps.
- m_tarlerNov 04, 2025Bronze Contributor
Hi Niko, it would be helpful to clarify what is different about your code (i.e. what they did wrong). It appears you made a number of differences including cleaning up a bit of the code. As for changes that might be directly related to the issue I see you:
- used the Inputbox method instead of the Inputbox function and forcing a number as a return value
- defined vDate as CDate(NM) and then using that for the search term
but that said I'm not sure if or how those changes should make a difference.
That all said the OP says "I've checked all the formatting and can't find why it doesn't find a match. " but I do find there to be some nuances with excel finding certain formats. so for example I used a custom format on the sheet using "MM/DD/YYYY" and the find will work if I search for the TEXT but not if I search using the date value. Also if you are using the standard Short Date that formatting is actually "M/D/YYYY" which means the month and day do not put in the preceeding 0. so if you are searching for "01/04/2026" (per the forced formatting in the macro) but on the sheet it may show as "1/4/2025".
I hope you find and let us know the issue because I am curious :)
- NikolinoDENov 04, 2025Platinum Contributor
Thanks for that thoughtful follow-up — really appreciate the careful read.
You’re absolutely right: most of the edits were cleanup (naming consistency, structure, better error handling, and avoiding duplicated Exit Sub branches). But yes, the two points you noted — using Application.InputBox with Type:=1 and separating vDate as an actual Date variable — are what primarily affect the matching logic.
The root of the original issue was indeed a type mismatch during the .Find operation. The original code was searching for a text string like "10/01/2025", while the worksheet cells contained true date serials formatted as m/d/yyyy. Since .Find with LookIn:=xlValues compares underlying values, not visible formatting, the string "10/01/2025" didn’t match the numeric date value.
By explicitly converting both sides to a true date (vDate = CDate(NM)) and searching for that date value, Excel’s internal comparison works correctly regardless of how the cell is formatted (whether 1/4/2025 or 01/04/2025).
The other side benefit of the rewrite is that it ensures formulas pasted into the “Slide Prep” sheet are treated as real formulas — not text — by resetting number formats before assigning them.
I’ll definitely share if anything else interesting turns up, but yes, it seems to come down to how .Find interprets date strings versus serials depending on locale and cell formatting.
Thanks again for the thoughtful comment — this kind of cross-check is exactly why the community is great.
… I rewrite it in a slightly more bulletproof version with error handling and dynamic column handling for Slide Prep. That would make it future-proof….i hope 🙂i dont test it.
Option Explicit Sub CMLUpdateV4() Dim wb As Workbook Dim wsData As Worksheet, wsSlide As Worksheet Dim rngDateRow As Range, rngFound As Range Dim dtInput As Variant, NM As String, vDate As Date Dim blnValidDate As Boolean Dim col As Long, lastCol As Long ``` On Error GoTo Cleanup '--- Optimize performance Application.ScreenUpdating = False Application.EnableEvents = False Application.Calculation = xlCalculationManual '--- Close other workbooks For Each wb In Application.Workbooks If Not (wb Is ThisWorkbook) Then wb.Close SaveChanges:=False Next wb '--- Ask user for a valid date blnValidDate = False Do dtInput = Application.InputBox("Please Enter File Submission Month as MM/DD/YYYY:", _ "Date Entry", , , , , , 1) If dtInput = False Then MsgBox "Update cancelled by user.", vbInformation GoTo Cleanup End If If IsDate(dtInput) Then NM = Format(CDate(dtInput), "MM/DD/YYYY") vDate = CDate(NM) blnValidDate = True Else MsgBox "Invalid Date Format. Please re-enter in MM/DD/YYYY format.", vbExclamation End If Loop Until blnValidDate '--- Reference worksheets Set wsData = ThisWorkbook.Worksheets("Data") Set wsSlide = ThisWorkbook.Worksheets("Slide Prep") '--- Check if date already exists in Data row 2 Set rngDateRow = wsData.Rows(2) Set rngFound = rngDateRow.Find(What:=vDate, LookIn:=xlValues, LookAt:=xlWhole) If Not rngFound Is Nothing Then MsgBox "Date " & NM & " already exists on the Data sheet. Please review.", vbInformation GoTo Cleanup End If '======================= ' Update Data tab '======================= With wsData .Columns("C:C").Insert .Columns("D:D").Copy .Columns("C:C").PasteSpecial xlPasteAll .Range("C2").Value = vDate .Range("C2").NumberFormat = "m/d/yyyy" ' Clear old content blocks .Range("C3:C6,C16:C19,C29:C32,C42:C45,C55:C58,C68:C71").ClearContents ' Ensure column O is static values .Columns("O:O").Copy .Columns("O:O").PasteSpecial xlPasteValues End With '======================= ' Update Slide Prep tab '======================= With wsSlide .Rows("2:2").Insert .Range("A2").Value = vDate .Range("A2").NumberFormat = "m/d/yyyy" ' Copy formulas from previous row (B3:H3) .Range("B3:H3").Copy .Range("B2:H2").PasteSpecial xlPasteAll ' Reset number format for new formula columns lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column .Range(.Cells(2, 9), .Cells(2, lastCol)).NumberFormat = "General" ' Insert dynamic XLOOKUP formulas B2 to last column For col = 2 To lastCol .Cells(2, col).Formula = "=XLOOKUP(" & .Cells(1, col).Address(False, False) & _ ",Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))" Next col ' Convert row 14 to values (per original code) .Rows("14:14").Copy .Rows("14:14").PasteSpecial xlPasteValues End With '--- Success message MsgBox "New File Date " & NM & " added and sheets updated." & vbCrLf & _ "Proceed to Data tab to record Enrollment Line Counts.", vbInformation ``` Cleanup: '--- Restore defaults Application.CutCopyMode = False Application.ScreenUpdating = True Application.EnableEvents = True Application.Calculation = xlCalculationAutomatic ``` If Err.Number <> 0 Then MsgBox "An error occurred: " & Err.Description, vbCritical End If ``` End Sub