gpt4 book ai didi

sql - Excel时间格式自动更改问题

转载 作者:行者123 更新时间:2023-12-02 12:09:16 27 4
gpt4 key购买 nike

我目前正在开发一个 Excel 应用程序,它可以从我的表单工作表中获取信息并将其显示到显示工作表中。到目前为止,除了一个小问题之外,数据可以完美传输。

单击“执行”时,“显示”工作表将显示数据。
这是屏幕截图: Here is the screenshot.

在执行显示之前,表单工作表中的时间字段格式为 h:mm AM/PM。但是,当我在“显示”工作表中单击“执行”时,“表单工作表”中的“时间”格式突然发生变化,如下所示: as seen here.

此外,格式的变化也可以在显示表中看到。我尝试将两个工作表的格式设置为相同,结果仍然相同。

是SQL1语句的问题还是一般编码的问题?这是代码示例。

Public Sub QueryWorksheet(szSQL As String, rgStart As Range, wbWorkBook As String, AB As String)
Dim rsData As ADODB.Recordset
Dim szConnect As String
On Error GoTo ErrHandler

If AB = "1st" Then
wbWorkBook = ActiveWorkbook.Sheets("Inner Workings").Range("B9").Text
End If

Application.StatusBar = "Retrieving data ....."
'Set up the connection string to excel - thisworkbook
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & wbWorkBook & ";" & _
"Extended Properties=Excel 8.0;"

Set rsData = New ADODB.Recordset
'Run the query as adCmdText
rsData.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText

'Check if data is returned
If Not rsData.EOF Then
'if the recordset contains data put them on the worksheet
rgStart.CopyFromRecordset rsData
Else

End If
'Close connection
rsData.Close
'Clean up and get out
Set rsData = Nothing
Application.StatusBar = False
Exit Sub
ErrHandler:
'an error occured in the SQL-statement
MsgBox "Your query could not be executed, the SQL-statement is incorrect."
Set rsData = Nothing
Application.StatusBar = False
End Sub

Sub process()
Call clear
Call testsql("1st") ' populate 1st Summary
Call testsql("2nd") ' find Date+Time
Call testsql("3rd") ' GET LATEST RECORD
End Sub

Sub testsql(AB As String)

Dim rgPlaceOutput As Range 'first cell for the output of the query
Dim stSQLstring As String 'text of the cell containing the SQL statement
Dim rg As String, SQL As String

If AB = "1st" Then
stSQLstring = ActiveWorkbook.Sheets("Inner Workings").Range("B2").Text
Set rgPlaceOutput = ActiveWorkbook.Sheets("1st Summary").Range("A2")
End If
If AB = "2nd" Then
stSQLstring = ActiveWorkbook.Sheets("Inner Workings").Range("B3").Text
Set rgPlaceOutput = ActiveWorkbook.Sheets("2nd Summary").Range("A2")
End If
If AB = "3rd" Then
stSQLstring = ActiveWorkbook.Sheets("Inner Workings").Range("B4").Text
Set rgPlaceOutput = ActiveWorkbook.Sheets("Final Summary").Range("A5")
End If

QueryWorksheet stSQLstring, rgPlaceOutput, ThisWorkbook.FullName, AB

End Sub

Sub clear()
ActiveWorkbook.Sheets("1st Summary").Range("A2:BR5000").Value = Empty
ActiveWorkbook.Sheets("2nd Summary").Range("A2:BR5000").Value = Empty
ActiveWorkbook.Sheets("Final Summary").Range("A2:BR5000").Value = Empty
End Sub

如果有人可以提供帮助,我将不胜感激。

更新:

显然,这个怪癖比我想象的要大。经过更多测试后,我发现第二个摘要表也受到影响,如下所示。 as seen here 。尽管至少是下半部分。谜团不断堆积......

最佳答案

我认为您必须查看 NumberFormat iirc 将“DD/MM/YYYY”之类的内容添加到范围(在您的情况下为列)。我使用的是装有 Office 365 的 Mac,虽然应用程序中现在包含 VBA,但缺少智能感知功能,因此除非您熟记 Excel 对象模型,否则它就是皇家 PITA!

关于sql - Excel时间格式自动更改问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44298477/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com