You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Access导入Excel时日期格式混乱问题求助

Fixing Date Format Issues When Importing from Access to Excel

Hey there, let's break down exactly why you're seeing those mixed-up dates and inconsistent data types, plus how to fix it for good.

What's Causing the Problem?

Your issue boils down to two key things:

  1. Regional Mismatch: Access and Excel are interpreting date formats differently. Since your dates follow French-style dd/mm/yyyy, but Excel might default to a regional setting like US English (mm/dd/yyyy). When the day value is ≤12 (e.g., 02/10/2017), Excel incorrectly reads it as month/day instead of day/month. For days >12 (29/09/2017), Excel can't treat 29 as a valid month, so it falls back to storing the value as a string instead of a proper date.
  2. Automatic Type Guessing: Using GetRows + Transpose to bulk-paste data forces Excel to "guess" each cell's data type. This leads to the inconsistent behavior you're seeing—some dates become strings, others get their day/month flipped.

Step-by-Step Fixes

Option 1: Format Dates in the SQL Query (Quick Win)

Modify your Access SQL to output dates in an ISO standard format (yyyy-mm-dd). Excel will always recognize this format correctly, no matter the regional settings.

Update your SQL string to include a formatted date field:

' Replace "YourDateField" with the actual name of your date column in Histo
str_req = "SELECT " & param_champs & ", Format(a.YourDateField, 'yyyy-mm-dd') AS FormattedDate FROM Histo a, Referential b WHERE a.productID = b.ID AND a.Isin IN " & sicoList

Then, after importing the data into Excel, convert the formatted string column to a proper date format:

Dim ws As Worksheet
Set ws = Worksheets("test")

' Assume FormattedDate is in column colref + 3 (adjust based on your param_champs)
With ws.Columns(colref + 3)
    .NumberFormat = "dd/mm/yyyy" ' Set your desired date display format
    .Value = .Value ' Convert the string to an actual date value
End With

Option 2: Write Records Line-by-Line (Most Reliable)

Avoid bulk-pasting with GetRows entirely. Instead, loop through the ADODB recordset and write each row directly to Excel, pre-setting the cell format to ensure dates are recognized correctly. This eliminates Excel's automatic type guessing.

Replace your bulk assignment code with this:

Dim ws As Worksheet
Dim rowNum As Integer
Set ws = Worksheets("test")
rowNum = 2 ' Start at row 2 as you did before

' First, set the target column's format to your desired date style (dd/mm/yyyy)
' Replace colref + 1 with the actual column index where your date will go
ws.Columns(colref + 1).NumberFormat = "dd/mm/yyyy"

' Loop through the recordset
Do While Not recordset.EOF
    ' Write the date field directly (Excel will honor the pre-set format)
    ws.Cells(rowNum, colref + 1).Value = recordset.Fields("YourDateField").Value
    
    ' Write your other fields here (match them to your param_champs)
    ' Example: ws.Cells(rowNum, colref).Value = recordset.Fields("productID").Value
    
    recordset.MoveNext
    rowNum = rowNum + 1
Loop

This method guarantees all dates are stored as proper date values (not strings) and displayed correctly, regardless of regional settings.

内容的提问来源于stack exchange,提问作者mehdi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:56:54