[Excel VBA for Beginners] How to Get Cell Serial Values | Understanding the Difference Between Value and Value2

目次

Introduction

In Excel, dates and times appear in formats like “2025/7/20,” but internally, they are managed as “Serial Values” (continuous numbers).

Retrieving this “Serial Value” via VBA allows for accurate and efficient date comparisons and calculations.

In this article, I will explain how to retrieve both the displayed value and the serial value using VBA, and clarify the differences between them.

What is a Serial Value?

A serial value is a sequential number where January 1, 1900 is treated as 1, and the number increases by 1 for every subsequent day.

For example, “July 20, 2025” is treated internally as the serial value 45502.

VBA Code: Retrieving Values Using Value and Value2

Sub ShowSerialValue()
    Dim checkCell As Range
    Set checkCell = Range("C3")
    
    ' Display the difference between the formatted date and the serial value
    MsgBox "Date Format (.Value): " & checkCell.Value & vbCrLf & _
           "Serial Value (.Value2): " & checkCell.Value2
End Sub

Code Explanation

  • .Value: Returns the value formatted according to the cell’s settings (or as a VBA Date type). For dates, it looks like “2025/7/20”.
  • .Value2: Ignores the formatting and returns the internal numeric value (Serial Value) itself.

Key Differences: Display Format vs. Actual Data

Even if a cell displays “2025/7/20”, it is treated as the number “45502” inside Excel. Understanding this distinction makes arithmetic (addition/subtraction) and comparison much easier.

Comparison Table

PropertyReturn Value (Example)Data TypeBest Used For
.Value2025/07/20Date (Variant)Displaying to users, standard date operations.
.Value245502Double (Variant)Calculations, sorting, speed optimization.

Why use Serial Values?

Using serial values is effective for:

  • Date Order Checks: Determining if a date is past a deadline.
  • Days Calculation: Calculating the exact difference between a start date and an end date.
  • Keys: Using dates as integer keys for Dictionaries or arrays.

Summary

In Excel VBA, using .Value2 allows you to handle the actual entity of the date (the serial value) directly.

  • .Value → The value as it appears (dependent on formatting/locale).
  • .Value2 → The actual numeric data (stable and unformatted).

By understanding this difference, you can perform more reliable date processing and data analysis, unaffected by the user’s display settings. This concept is essential for If statements, conditional logic, sorting, and date difference calculations.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次