logo
search
VBA & Macro Problems

Fix Excel VBA Returning 12:00:00 AM Instead of a Date

Maira MehtabMaira Mehtab Sep 20, 2026 869 views

Question details

A VBA macro fails to output calculated maximum, minimum, and median dates correctly, returning a zero-value timestamp instead.

Product
Excel
Device & OS
not provided
Scenario
Running a VBA script to search columns for specific text values (FUTIDX and NIFTY), extract corresponding dates from another column, and calculate statistical values.
Observed behavior
The macro outputs 12:00:00 AM instead of the actual calculated dates, indicating that the final output variables are returning a value of zero.
Before you start

Open the Visual Basic for Applications (VBA) editor by pressing Alt + F11 and navigate to the module containing your date calculation loop so you can inspect the code.

Solution 1Recommended

Clean Hidden Spaces in Cell Values Using the Trim Function

Trailing spaces or non-printing characters can prevent exact string matches during loop iterations, causing date arrays to remain empty.

When VBA searches columns for specific text like 'FUTIDX', an invisible trailing space in the spreadsheet cell data will cause the 'If' statement to fail. Because the condition is never met, the dates are never collected, resulting in a zero value that Excel formats and displays as '12:00:00 AM'.

1
Locate the comparison line

Find the line of code in your macro where you compare the cell value to your target text (e.g., `If Cells(b, 2).Value = "NIFTY" Then`).

2
Apply the Trim function

Wrap the cell reference in the `Trim$()` function to strip out invisible spaces. Update the line of code to `If Trim$(Cells(b, 2).Value) = "NIFTY" Then`.

3
Run the macro again

Save your code and run the macro to see if the dates are now successfully captured and calculated.

Pro Tip: Always use Trim$() when comparing text strings pulled from raw spreadsheet data to avoid silent failures caused by data entry errors.
Advanced Macro Support

Debug and Run VBA Macros Easily with WPS Spreadsheets

WPS Office provides robust built-in support for writing, debugging, and running VBA macros. You can seamlessly troubleshoot string matching, utilize Trim functions, and execute your scripts step-by-step using an intuitive developer environment.

  1. 1. Download and Install WPS Office: Get WPS Office from the official website and install the suite on your computer.
  2. 2. Open Your Macro-Enabled File: Launch WPS Spreadsheets and open your .xlsm or .xls file containing the date calculation macro.
  3. 3. Access the VBA Editor: Navigate to the 'Developer' tab and click on 'Visual Basic' or simply press Alt + F11 to open the code editor.
  4. 4. Edit and Debug: Apply the Trim function to your conditions, fix variable directions, and press F8 to step through the script to verify your date output.
Full compatibility with Microsoft Excel VBA syntax and macro-enabled formats (.xlsm, .xls).Built-in VBA editor for easy step-by-step debugging (F8) and code execution.Lightweight application design that processes large data loops and arrays quickly.Free and highly intuitive interface for viewing, editing, and managing complex spreadsheets.
QA img-10

Frequently Asked Questions

Why does Excel display '12:00:00 AM' instead of a blank cell or zero?

In Excel's date and time serial formatting system, the number 0 corresponds to the very beginning of the date calendar (January 0, 1900). When a VBA macro fails to assign a valid date to a variable, that variable defaults to zero, which Excel formats and displays visually as 12:00:00 AM.

How do I ensure my VBA macro ignores spaces when matching text?

You can use the Trim() or Trim$() function in VBA to strip leading and trailing spaces from cell values before comparing them. Using syntax like `If Trim(Cells(row, col).Value) = "TargetText" Then` guarantees that hidden spaces won't cause the matching condition to fail.

Can changing my VBA variable type from Long to Date fix the 12:00:00 AM issue?

Changing the variable type to Date will not resolve the issue if the variable is still being assigned a value of zero due to an underlying logic error or an empty array. You must first ensure the logic successfully pulls and stores the date values properly.