簡體   English   中英

excel vba字符串到目前為止

[英]excel vba string to date

Windows 10 Pro,英國英語區域設置。 在Excel VBA中,我有一個字符串“02/05/2017 16:30”,在英國,意思是“2017年5月2日16:30”

但VBA以某種方式將其轉換為美國格式並在單元格中將“05/02/2017 16:30”改為

VBA代碼是這樣的

Dim sField As String
sField = "02/05/2017 16:30"
ws.Cells(1,1) = sField

我可以使用CDate解決這個問題,但需要額外的代碼來確定哪些單元格是日期,哪些不是,而隱式轉換適用於所有類型。

請改用Date變量,並始終在VBA中以MDY提供日期。

Dim sField As Date
sField = #05/02/2017 16:30#
ws.Cells(1,1) = sField

在VBA中的AFAIK你必須始終使用'美國方式',日期為MDY。 它不遵循區域設置。 哪個好,因為這樣可以在異構環境中運行相同的代碼。

這是VBA代碼中的一些解決方法:

Sub Main()        
    Dim myInput     As String
    Dim splitMe     As Variant
    Dim outputDate  As Date

    myInput = "02/05/2017 16:30"
    splitMe = Split(myInput, "/")        
    outputDate = DateSerial(Left(splitMe(2), 4), splitMe(1), splitMe(0))        
    Debug.Print Format(outputDate, "DD-MMM-YY")
    Debug.Print Format(outputDate, "DD-MM-YYYY")           
End Sub

它將日期作為字符串,並將其拆分為/ 然后它需要年,月和日,並在DateSerial()的幫助下建立一個新的日期。 DateSerial MSDN

在這種情況下,請確保您將正確的日期傳遞給Excel,並且您可以通過以下方式更改格式:

Range("A1").NumberFormat = "m/d/yyyy"

為了確保您傳遞的是正確的日期,只需在日期或Day(YourDate)嘗試Month(YourDate) Day(YourDate)

我寧願使用內置的VBA函數DateSerial(年,月,日)和TimeSerial(小時,分鍾,秒)。

Dim myDateTime as date
mydateTime = DateSerial(2017, 5, 2) + TimeSerial(16, 30, 0)
ws.Cells(1,1) = myDateTime

然后,您可以根據自己的喜好在Excel單元格上設置數字格式。

我認為這更快,因為不需要事先翻譯任何字符串。 對我來說更重要的是作為程序員,參數是明確的。 我不必擔心不同的區域環境。

我解決了一個相關的問題。 我的工作簿僅供英國使用。 它有一張表格,用於輸入在各個場地收集的現金的詳細信息。 用戶有兩個單細胞區域來識別每個場地; 通常是位置和日期,但有時“日期”字段將包含擴展的位置名稱。 日期輸入為dd / mm / yy,但 mm / dd / yy 幾乎可以接受任何可識別的日期。 詳細信息存儲在內存中,然后復制到格式化的工作表進行打印。 我在內存中驗證了存儲空間。 但是在工作簿使用了幾個月之后,我發現如果用戶在格式為dd / mm / [yy] yy(例如05/11/17)的單元格中輸入了有效日期,並將其解釋為mm / dd / [yy] yy也會給出一個有效的日期,然后日期將被模糊地打印為11-Mar而不是05-Nov。

一些代碼片段:

'Data structure:
Public Type BkItem             'An item of income, for banking.
    ItemName As String         'The first field, just a text name.
    ItemDate As Date           'The second field, interpreted as a date.
    ItemDateNumber As Long     'The date as internally stored as an integer.
    ItemDateString As String   'Re-formatted string, e.g. "05-Nov-17".
'   ...
End Type    'BkItem.

'Input validation:
BankData = Range(.Cells(BankFirstRow, BankFirstCol), _
                 .Cells(BankLastItemLastRow, BankLastCol))

With BankItem(BankTotalItems)
    .ItemName = IName
    .ItemDateString = BankData(<row>, <col>)
    .ItemDateNumber = DateToLong(.ItemDateString)
End With

'Utility routine. "Paper" is a 2-dimensional array of all the data to be printed
'on one or more pages; "Dest" is a global range.:

Sub OutputDataToSheet(ByVal Size As Long, ByRef CurrentSheet As String, _
                      ByRef Paper() As Variant)
    Worksheets(CurrentSheet).Activate
    Set Dest = Worksheets(CurrentSheet).Range((Cells(1, 1)), _
                                              (Cells(Size, LastCol)))
    Dest.Value = Paper 'Copy data to final sheet for printing.                                         
End Sub    'OutputDataToSheet.

'As we build the array "Paper", it helps to format those cells on the final
'printout worksheet which are going to contain dates.

.Range(Cells(CurRow, L15c01), Cells(CurRow, L15c01)).NumberFormat = "dd-Mmm-yyyy"
'For the item date.
.Range(Cells(CurRow, L15c01), Cells(CurRow, L15c01)).HorizontalAlignment = xlCenter

If IsDate(BankItem(item).ItemDateString) Then
    Paper(<row>, <col>) = BankItem(item).ItemDateNumber
    'Date as a number, so OutputDataToSheet preserves UK date format.
Else
    Paper(<row>, <col>) = BankItem(item).ItemDateString
    'Extension of name.
End If  'IsDate(.ItemDateString).

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM