检查字符串格式

起司

我有一个循环,循环一个列中的所有条目:

Dim indexOfDATE As Integer
indexOfDATE = 3
Do Until indexOfDATE - 1 = Cells(Rows.Count, 2).End(xlUp).Row
    Dim tempDate As String
    tempDate = Replace(Sheets("Pirmie Ievaddati").Cells(indexOfDATE, 2).Value, ",", ".")
    tempDate = Replace(tempDate, ".", "/")
    indexOfDATE = indexOfDATE + 1
Loop

我需要检查我的tempDate字符串变量是否为dd.MM.yyyy格式,如果不这样做,请显示消息框,而不出现错误?

编辑:

我正在这样做:

Dim indexOfDATE As Integer
indexOfDATE = 3
Do Until indexOfDATE - 1 = Cells(Rows.Count, 2).End(xlUp).Row
    Dim tempDate As String
    tempDate = Replace(Sheets("Pirmie Ievaddati").Cells(indexOfDATE, 2).Value, ",", ".")
    tempDate = Replace(tempDate, ".", "/")
    Dim current As Date
    current = Format(CDate(tempDate), "dd/mm/yyyy")
    Sheets("Pirmie Ievaddati").Cells(indexOfDATE, 2).Value = current
    indexOfDATE = indexOfDATE + 1
Loop

如您所见,该单元格是一个字符串单元格,我需要确保tempDate字符串的格式正确,然后再进行转换或应用程序掉落。

我想告诉用户消息,在细胞()他有不正确的数据

或者也许是转换时的Try Catch块-但是在第一个错误后我未能停止代码执行

悉达思·劳特

试试这个(未测试)

Dim indexOfDATE As Long
Dim tempDate As String
Dim current As Date

indexOfDATE = 3

Do Until (indexOfDATE - 1) = Cells(Rows.Count, 2).End(xlUp).Row
    tempDate = Replace(Sheets("Pirmie Ievaddati").Cells(indexOfDATE, 2).Value, ",", "/")

    If IsDate(tempDate) Then
        current = Format(CDate(tempDate), "dd/mm/yyyy")
        With Sheets("Pirmie Ievaddati").Cells(indexOfDATE, 2)
            .NumberFormat = "dd/mm/yyyy"
            .Value = current
        End With
        indexOfDATE = indexOfDATE + 1
    End If
Loop

或更短的版本

Dim indexOfDATE As Long
Dim tempDate As String
Dim current As Date

indexOfDATE = 3

With Sheets("Pirmie Ievaddati").Cells(indexOfDATE, 2)
    Do Until (indexOfDATE - 1) = Cells(Rows.Count, 2).End(xlUp).Row
        tempDate = Replace(.Value, ",", "/")

        If IsDate(tempDate) Then
            current = Format(CDate(tempDate), "dd/mm/yyyy")
            .NumberFormat = "dd/mm/yyyy"
            .Value = current
            indexOfDATE = indexOfDATE + 1
        End If
    Loop
End With

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

检查字符串的格式/内容

来自分类Dev

检查字符串的格式

来自分类Dev

如何检查字符串格式

来自分类Dev

如何检查字符串是否为json格式

来自分类Dev

PHP检查字符串格式是否正确

来自分类Dev

检查字符串是否有日期,任何格式

来自分类Dev

检查字符串是否采用“坐标/坐标”格式

来自分类Dev

检查字符串在C中的格式是否正确

来自分类Dev

如何检查字符串中的日期时间格式?

来自分类Dev

使用sscanf检查字符串格式

来自分类Dev

如何检查字符串的格式正确(Python)

来自分类Dev

检查字符串是否遵循某种格式?

来自分类Dev

检查字符串是否与特定格式匹配

来自分类Dev

检查字符串是否格式化单词

来自分类Dev

如何检查字符串输入的格式

来自分类Dev

使用变量检查字符串格式

来自分类Dev

检查字符串中的字符

来自分类Dev

检查字符串中的字符

来自分类Dev

如何以编程方式检查字符串的日期格式以获取正确的格式

来自分类Dev

如何以编程方式检查字符串的日期格式以获取正确的格式

来自分类Dev

Delphi-如何检查字符串是否包含设置的字符格式

来自分类Dev

如何检查字符串或数字

来自分类Dev

使用枚举检查字符串

来自分类Dev

检查字符串是否是回文

来自分类Dev

尝试/除外检查字符串

来自分类Dev

检查字符串中的辅音

来自分类Dev

检查字符串和对象

来自分类Dev

检查字符串的更好方法?

来自分类Dev

C ++:检查字符串的内容