Excel VBA字符串到目前为止



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来解决此问题,但是CDATE,但这需要额外的代码来确定哪些单元格是哪些日期,而隐式转换适用于所有类型。

使用日期变量,并始终在VBA中提供您的日期。

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

afaik在VBA中,您必须始终用日期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()的帮助下建立了新约会。日期msdn。

在这种情况下,请确保您将正确的日期传递给Excel,并且在那里您可以通过此类简单更改格式:

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

确保您通过正确的日期,只需在日期或Day(YourDate)中尝试Month(YourDate)

我宁愿使用内置的VBA函数日期(年,月份,日)和时间表(小时,最小,SEC)。

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).

相关内容

最新更新