以错误格式粘贴的用户表单中的日期

Date from userform pasted in the wrong format

提问人:Tomas Joe Gemine 提问时间:9/11/2023 更新时间:9/11/2023 访问量:33

问:

我有一个用户表单,用于通过将数据从用户表单粘贴到 excel 中来收集 excel 中的数据。

我遇到的这个问题是在粘贴TXT_DateLogged中的日期时,该日期在用户窗体文本框中显示为 dd/mm/yy。Excel 正在将其更改为 MM/DD/YYYY。

我在下面列出了涉及的例程:

Private Sub UserForm_Initialize()
    Me.Height = 432
    Me.Width = 438
    Me.Zoom = 100

    Txt_DateLogged.Value = Format(Date, "dd/mm/yyyy")
    Txt_Month.Value = Format(Date, "MMM-YY")

    Call CBO_Supplier_Items
    Call CBO_SRM_Items
    Call CBO_Cause_Items
    Call CBO_Categorisation_Items

    Dim wsLog As Worksheet
    Dim wsArchive As Worksheet
    Dim i As Long
    Dim maxIssueNum As Long

    ' Set references to the "LSI Log" and "LSI Archive" sheets
    Set wsLog = ThisWorkbook.Sheets("LSI Log")
    Set wsArchive = ThisWorkbook.Sheets("LSI Archive")

    ' Find the maximum LSI-## in "LSI Log"
    i = wsLog.Cells(wsLog.Rows.Count, 1).End(xlUp).Row
    maxIssueNum = GetMaxIssueNumber(wsLog, i)

    ' Find the maximum LSI-## in "LSI Archive"
    i = wsArchive.Cells(wsArchive.Rows.Count, 1).End(xlUp).Row
    maxIssueNum = Application.WorksheetFunction.Max(maxIssueNum, GetMaxIssueNumber(wsArchive, i))

    ' Set the next available LSI-## in the textbox
    Me.Txt_IssueNum.Value = "LSI-" & Format(maxIssueNum + 1, "00")

End Sub 
Sub Btn_Submit_Click()
 
    Dim rw As Integer
    Dim ref As String
    Dim refnum As Integer
    Dim ws As Worksheet
    Set ws = Sheets("LSI Log")
    
    'Find the last row in the sheet
    rw = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1
    
    'Insert data into sheet
    
    ws.Cells(rw, 1).Value = Txt_IssueNum.Value
    ws.Cells(rw, 2).Value = CBO_LA_OOA.Value
    ws.Cells(rw, 3).Value = Txt_Month.Value
    ws.Cells(rw, 4).Value = Txt_DateLogged.Value
    ws.Cells(rw, 5).Value = CBO_SRM.Value
    ws.Cells(rw, 6).Value = CBO_Supplier.Value
    ws.Cells(rw, 7).Value = Txt_Description.Value
    ws.Cells(rw, 8).Value = Cbo_Cause.Value
    ws.Cells(rw, 9).Value = Txt_CauseReason.Value
    ws.Cells(rw, 10).Value = Txt_Impact.Value
    ws.Cells(rw, 11).Value = Txt_NoOfCharges.Value
    ws.Cells(rw, 12).Value = Txt_NoOfSearches.Value
    ws.Cells(rw, 13).Value = Txt_MatrixScore.Value
    ws.Cells(rw, 14).Value = Cbo_Categorisation.Value

    
Unload Me

我还尝试了Btn_Submit例程的不同变体:

ws.Cells(rw, 4).Value = DateValue(Txt_DateLogged.Value)
ws.Cells(rw, 4).NumberFormat = "dd/mm/yyyy"

ws.Cells(rw, 4).Value = Format(Txt_DateLogged.Value, "DD/MM/YYYY")

所有这些都没有成功,仍然以 MM/DD/YYYY 格式粘贴。

关于它失败的地方或任何可能的解决方案的任何想法?

Excel VBA 日期格式

评论

1赞 CDP1802 9/11/2023
将字符串拆分为多个部分并使用 DateSerial。ar = Split(s, "/"): ws.Cells(rw, 4) = DateSerial(ar(2), ar(1), ar(0))

答: 暂无答案