Forum Discussion
Excel VBA Updates not functioning as intended
Afternoon all,
Attaching a deidentified copy of a metrics file. I built a macro which does two main things once a user clicks the "Update Sheet" icon in the ribbon.
- asks for a date in the format "MM/DD/YYYY"
- Compares the date entered to the format requirement, & if appropriately formatted, proceeds to make updates to the two tabs of the sheet.
As a safeguard, the macro also checks if the entered date is found in Data tab, row 2. If yes, a msgbox should notify the user of the existence of the date, and exit sub. However with a user entered date which already exists, my macro continues on to make updates of inserting a new column C on Data tab, as well as inserting a new row 2 on Slide Prep tab. I've checked all the formatting and can't find why it doesn't find a match. Also, on the Slide Prep tab it when inserting new formulas, the formulas added to I2:M2 all present as text, and do not auto calculate - again, nothing i can locate as the root source of the surprise.
I've attached a copy with the incorrect duplication of 10/01/2025 as two columns on Data tab, and as two rows on Slide Prep tab. VBA Code below:
Option Explicit
Sub CMLUpdateV2()
Dim Wb As Workbook
Dim Ws As Worksheet
Dim ICount As Integer
Dim iRow As Integer
Dim Row As Object
Dim RowAddress As String
Dim RowRange As String
Dim NM As String
Dim DR As Range 'Data Date Tab Date Row(2)
Dim DC As Range 'Date Found Range
Dim strDateInput As String
Dim blnValidDate As Boolean
Application.ScreenUpdating = False
For Each Wb In Application.Workbooks
If Not (Wb Is Application.ThisWorkbook) Then
Wb.Close
End If
Next
blnValidDate = False
'Determine if input value is valid format, re-enter if not
Do
strDateInput = InputBox("Please Enter File Submission Month as MM/DD/YYYY:", "Date Entry")
'If strDateInput = "" Then
' MsgBox "No Date Entered, update cancelled.", vbInformation
' Exit Sub
' End If
If IsDate(strDateInput) Then
If Format(CDate(strDateInput), "MM/DD/YYYY") = strDateInput Then
blnValidDate = True
Else
MsgBox "Invalid Date Format or No Date Entered. Please Re-Enter in MM/DD/YYYY format."
blnValidDate = False
End If
Else
MsgBox "Invalid Date Format or No Date Entered. Please Re-Enter in MM/DD/YYYY format."
blnValidDate = False
End If
Loop Until blnValidDate
NM = strDateInput
'Determine if valid input is already present on sheet
Set DR = ThisWorkbook.Worksheets("Data").Range("2:2")
Set DC = DR.Find(What:=NM, LookIn:=xlValues, LookAt:=xlWhole)
If Not DC Is Nothing Then
MsgBox "Date entered is already present. Please Review Date.", vbOKCancel
Exit Sub
Else
'Update Data Tab with new column and formula updates
With ThisWorkbook.Worksheets("Data")
.Columns("C:C").EntireColumn.Insert
.Range("D:D").Copy
.Range("C:C").PasteSpecial xlPasteAll
.Range("C2").Value = NM
.Range("C3:C6,C16:C19,C29:C32,C42:C45,C55:C58,C68:C71").ClearContents
.Columns("O:O").Copy
.Columns("O:O").PasteSpecial xlPasteValues
End With
'Update Slide Prep Tab with New row and formula updates
With ThisWorkbook.Worksheets("Slide Prep")
.Rows("2:2").EntireRow.Insert
.Range("A2").Value = NM
.Range("A2").NumberFormat = "m/d/yyyy"
.Range("B3:H3").Copy
.Range("B2:H2").PasteSpecial xlPasteAll
.Range("B2").Formula = "=XLOOKUP(B$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Range("C2").Formula = "=XLOOKUP(C$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Range("D2").Formula = "=XLOOKUP(D$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Range("E2").Formula = "=XLOOKUP(E$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Range("F2").Formula = "=XLOOKUP(F$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Range("G2").Formula = "=XLOOKUP(G$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Range("H2").Formula = "=XLOOKUP(H$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Range("I2").Formula = "=XLOOKUP(I$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Range("J2").Formula = "=XLOOKUP(J$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Range("K2").Formula = "=XLOOKUP(K$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Range("L2").Formula = "=XLOOKUP(L$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Range("M2").Formula = "=XLOOKUP(M$1,Data!$B$3:$B$80,XLOOKUP($A2,Data!$C$2:$Q$2,Data!$C$3:$Q$80))"
.Rows("14:14").EntireRow.Copy
.Rows("14:14").EntireRow.PasteSpecial xlPasteValues
End With
MsgBox "New File Date " & NM & " added and sheets updated. Proceed to Data Tab to record Enrollment Line Counts."
End If
End Sub
3 Replies
- NikolinoDEPlatinum Contributor
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_tarlerBronze 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 :)
- NikolinoDEPlatinum 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