VBA:如何标记单元格中的输入
VBA: How can I tokenize an input in a cell
我想做的是将单元格中的时间输入转换为特定格式。示例:
"9" or "9 am" = 9:00:00 AM, which is the same as TIME(9, 0, 0)
"9 30" = 9:30:00 AM = TIME(9, 30, 0)
"4 30 pm" = 4:30:00 PM = TIME(16, 30, 0)
如何在 VBA 中实现此目的?
顺便说一下,这实际上是我第一次尝试 VBA。
谢谢。
我可以支持一些学习:
Function timm(str As String) As Double
Dim spltstr() As String
Dim hr As Integer
Dim min As Integer
Dim sec As Integer
hr = 0
min = 0
sec = 0
str = Replace(str, """", "")
spltstr = Split(str)
hr = spltstr(0)
If UCase(spltstr(UBound(spltstr))) = "PM" Then hr = hr + 12
If 1 <= UBound(spltstr) Then
If IsNumeric(spltstr(1)) Then min = spltstr(1)
End If
timm = TimeSerial(hr, min, sec)
End Function
将其放在工作簿的附加模块中。然后可以直接在工作表上或从另一个子表中将其作为函数调用。
它会将文本更改为数字,因此如果将其用作 UDF,您仍然需要将自定义数字格式应用于单元格。
根据您的意见,如果要就地进行,那么您将使用 Worksheet_Change 事件。
首先在您想要的整个列上使用自定义格式,例如 hh:mm:ss AM/PM
。将其放入工作表代码中。
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo getout
Application.EnableEvents = False
If Not Intersect(Range("A:A"), Target) Is Nothing Then
If Not IsDate(Target.Value) Then
Target = timm(Target.Value)
End If
End If
Application.EnableEvents = True
Exit Sub
getout:
Application.EnableEvents = True
End Sub
它调用先前的代码和 returns 号码。因此,当您离开编辑模式时,它会将其更改为时间。
您似乎想使用 Split 和 TimeSerial。这是一个帮助您入门的示例。
Public Function ConvertToTime(ByVal Input_ As String) As Date
Dim vaSplit As Variant
Dim lHour As Long, lMinute As Long
'split the string into an array using space as a delimiter
vaSplit = Split(Input_, Space(1))
'The first element is the hour
lHour = vaSplit(0)
'If there's more than one element, the second element is the minute
If UBound(vaSplit) > 0 Then lMinute = vaSplit(1)
'if the last element is "pm", then add 12 to the hour
If vaSplit(UBound(vaSplit)) = "pm" Then lHour = lHour + 12
ConvertToTime = TimeSerial(lHour, lMinute, 0)
End Function
我想做的是将单元格中的时间输入转换为特定格式。示例:
"9" or "9 am" = 9:00:00 AM, which is the same as TIME(9, 0, 0)
"9 30" = 9:30:00 AM = TIME(9, 30, 0)
"4 30 pm" = 4:30:00 PM = TIME(16, 30, 0)
如何在 VBA 中实现此目的?
顺便说一下,这实际上是我第一次尝试 VBA。
谢谢。
我可以支持一些学习:
Function timm(str As String) As Double
Dim spltstr() As String
Dim hr As Integer
Dim min As Integer
Dim sec As Integer
hr = 0
min = 0
sec = 0
str = Replace(str, """", "")
spltstr = Split(str)
hr = spltstr(0)
If UCase(spltstr(UBound(spltstr))) = "PM" Then hr = hr + 12
If 1 <= UBound(spltstr) Then
If IsNumeric(spltstr(1)) Then min = spltstr(1)
End If
timm = TimeSerial(hr, min, sec)
End Function
将其放在工作簿的附加模块中。然后可以直接在工作表上或从另一个子表中将其作为函数调用。
它会将文本更改为数字,因此如果将其用作 UDF,您仍然需要将自定义数字格式应用于单元格。
根据您的意见,如果要就地进行,那么您将使用 Worksheet_Change 事件。
首先在您想要的整个列上使用自定义格式,例如 hh:mm:ss AM/PM
。将其放入工作表代码中。
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo getout
Application.EnableEvents = False
If Not Intersect(Range("A:A"), Target) Is Nothing Then
If Not IsDate(Target.Value) Then
Target = timm(Target.Value)
End If
End If
Application.EnableEvents = True
Exit Sub
getout:
Application.EnableEvents = True
End Sub
它调用先前的代码和 returns 号码。因此,当您离开编辑模式时,它会将其更改为时间。
您似乎想使用 Split 和 TimeSerial。这是一个帮助您入门的示例。
Public Function ConvertToTime(ByVal Input_ As String) As Date
Dim vaSplit As Variant
Dim lHour As Long, lMinute As Long
'split the string into an array using space as a delimiter
vaSplit = Split(Input_, Space(1))
'The first element is the hour
lHour = vaSplit(0)
'If there's more than one element, the second element is the minute
If UBound(vaSplit) > 0 Then lMinute = vaSplit(1)
'if the last element is "pm", then add 12 to the hour
If vaSplit(UBound(vaSplit)) = "pm" Then lHour = lHour + 12
ConvertToTime = TimeSerial(lHour, lMinute, 0)
End Function