【问题标题】:Date Check in Vb.net在 Vb.net 中检查日期
【发布时间】:2016-10-12 10:00:07
【问题描述】:

我有一个问题,在 excel 中一列包含一个唯一编号,格式为:mmdd 后跟序列号 01,02.. 例如:101201(10:月,12:今天的日期,01:第一个条目), 101202。 现在,我需要的是我在 vb.net 中的表格应该采用最后输入的数据(例如:101202)检查它是否是今天的日期。如果是,则应加 1 并在消息框中显示。(是,则应显示 101203)。如果不是,那么它应该取当前日期并从 01 开始(如果是 101301,则日期是 2016 年 10 月 13 日)。 我设法从前一行获取数据。但是我应该如何用日期检查它? 请帮忙!

   Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    xlWorkBook = xlApp.Workbooks.Open("C:\Users\Desktop\testing.xlsx")
    'xlApp.Visible = True
    xlWorkSheet = xlWorkBook.Sheets("Sheet1")

    Dim row As Long
    row = 1
    With xlWorkSheet
        While .Cells(row, 2).value IsNot Nothing
            row = row + 1
        End While
        'MsgBox(row - 1)
    End With

    Dim r As Excel.Range
    Dim t As String = Format(Today, "MMdd")
    If xlWorkSheet.Cells(row - 1, 4).value Is Nothing Then
        Me.txtQuote.Text = t & "01"
    Else
        r = xlWorkSheet.Cells(row - 1, 4)
        Me.txtQuote.Text = (r.Value + 1)
    End If

'this is wrong and I know it's wrong
    If r.Value = Date.Today Then
        MsgBox("true")
    Else
        MsgBox("false")
    End If

End Sub

【问题讨论】:

    标签: excel vb.net date visual-studio-2013 excel-2013


    【解决方案1】:

    把这个放在你写'this is wrong and I know it's wrong的地方

    Dim rDate As String = CStr(r.Value).Substring(0, 4)
    If rDate = t Then
        MsgBox("true")
    Else
        MsgBox("false")
    End If
    

    rDate 基本上是从 r 中获取前 4 位数字,所以现在您可以将其与今天的日期进行比较。我还用t 替换了今天的日期,因为您有t 已经以所需的格式使用今天的日期。

    将变量命名为tr 会让人很难理解它们的用途。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多