【问题标题】:Excel time format auto-change issueExcel时间格式自动更改问题
【发布时间】:2017-11-02 01:37:21
【问题描述】:

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

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

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

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

是 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

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

更新:

显然,这个怪癖比我想象的要大。经过更多测试后,我发现第二个摘要表也受到影响,就像在这里看到的一样。 。尽管至少是下半部分。谜团不断堆积......

【问题讨论】:

  • 附言。很抱歉截图给您带来不便。我没有足够的积分在帖子上显示图片。 :p

标签: sql excel vba


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2014-05-16
    相关资源
    最近更新 更多