Excel VBA:将今天的date插入最后一行
我试图:
- 检查最后一行是否空白。
- 检查最后的非空白行是否包含今天的date
- 如果不是,则在第一个空白行中input今天的date,每个由Dims指示的列。 (因为有5个单独的表需要date)。
我的代码是否完成了这个? 我希望在打开工作簿时更新它,但保留前一天input的date。 所以,实质上,每天当我需要更新数据时,今天的date已经被设置在那里,我可以把数据放在里面。
码:
Private Sub Workbook_Open() Dim D1Col As Long, D2Col As Long, D3Col As Long, D4Col As Long, D5Col As Long, rowCnt As Long D1Col = 1 D2Col = 4 D3Col = 7 D4Col = 10 D5Col = 13 endRow = Cells(Rows.Count, 1).End(xlUp).Row '<--| set 'endRow' to column A If endRow.Offset(1, 0).Value = 0 Then 'Does the zero idicate "if it is blank"? If endRow.Value <> Format(Now(), "mm/dd/yyyy") Then Cells(endRow.Offset(1, 0), D1Col) = Format(Now(), "mm/dd/yyyy") Cells(endRow.Offset(1, 0), D2Col) = Format(Now(), "mm/dd/yyyy") Cells(endRow.Offset(1, 0), D3Col) = Format(Now(), "mm/dd/yyyy") Cells(endRow.Offset(1, 0), D4Col) = Format(Now(), "mm/dd/yyyy") Cells(endRow.Offset(1, 0), D5Col) = Format(Now(), "mm/dd/yyyy") End If Else: endRow.Offset(1, 0).Value = 0 End Sub
数据示例:
我已经清理并重构了消除冗余代码,并且明确了所有隐式的ActiveSheet
引用:
Private Sub Workbook_Open() Const startCol As Long = 1 Const colCountToSet As Long = 5 Const skipColCount As Long = 3 Dim endRow As Long endRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row If IsDate(Cells(endRow, startCol)) Then If Int(CDate(ActiveSheet.Cells(endRow, startCol))) <> Date Then endRow = endRow + 1 Dim curCol As Long curCol = startCol Dim counter As Integer For counter = startCol To colCountToSet ActiveSheet.Cells(endRow, curCol) = Date curCol = curCol + skipColCount Next End If End If End Sub
这是做了什么,为什么:
Const startCol As Long = 1 Const colCountToSet As Long = 5 Const skipColCount As Long = 3
如果您需要添加或删除一组列,请调整colCountToSet
并且您的代码继续工作。
如果您为每组添加另一列或在数据集之间添加空格,请调整skipColCount
,您的代码将继续工作。
如果插入新的栏A,请调整startCol
Dim todaysDate As String todaysDate = Format(Now(),“mm / dd / yyyy”)
多次调用Format()
函数没有意义,只对date感兴趣,如果有人在午夜之前打开工作簿,则可能在同一行上获得不同的date。
If IsDate(ActiveSheet.Cells(endRow, startCol)) Then
我已经修复了这一点感谢来自@Comintern的build议。 首先确保你的最后一行包含一个date。 如果由于某种原因某人在底部input了非date值,则会跳过覆盖它。
If Int(CDate(ActiveSheet.Cells(endRow, startCol))) <> Date Then
Date
函数返回一个没有时间的date(作为一个整数),所以把它和最后一行的内容进行比较。
If Format(ActiveSheet.Cells(endRow, startCol), "mm/dd/yyyy") <> todaysDate Then
如果最后一行是空的,则不匹配。 如果是昨天的date,就不会匹配。 两种情况都属于If
语句。 如果是今天的date,它会匹配,它会跳过If
语句。 您必须在列中格式化date以与您使用的格式完全匹配,因为单元格的显示格式可能会返回与您testing的string不同的string。
endRow = endRow + 1
摆脱.Offset()
。 特别是因为你没有使用Range
对象。
For counter = startCol To colCountToSet ActiveSheet.Cells(endRow, curCol) = Date curCol = curCol + skipColCount Next
一个很好的,简单的小循环,将设置每个列的date,而不必调整除CONST
之外的其他任何东西,或者在代码的最顶端调整其他任何东西。 它将date设置为系统返回的Date
整数。
尝试像下面的代码:
Dim D1Col As Long, D2Col As Long, D3Col As Long, D4Col As Long, D5Col As Long, rowCnt As Long Dim endRow As Long D1Col = 1 D2Col = 4 D3Col = 7 D4Col = 10 D5Col = 13 With Worksheets("Sheet4") ' <-- define which sheet to perform the tests below endRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' <-- set 'endRow' to column A If .Range("A" & endRow).Offset(1, 0).Value = 0 Or .Range("A" & endRow).Offset(1, 0).Value = "" Then ' check is blank (maybe also zero, not sure whatthe PO wants) If .Range("A" & endRow).Value <> Date Then .Cells(endRow + 1, D1Col) = Date .Cells(endRow + 1, D2Col) = Date .Cells(endRow + 1, D3Col) = Date .Cells(endRow + 1, D4Col) = Date .Cells(endRow + 1, D5Col) = Date End If Else .Range("A" & endRow).Offset(1, 0).Value = 0 End If End With
我没有评论的声誉,但我认为你应该培养一点VBA的基础知识,这里有一些提示:
endRow = Range("A1").End(xlDown).Row '<--| really set 'endRow' to column A in active sheet!
If Cells(endRow, D1Col).Value = "" Then 'if cell in active sheet is blank...
If Cells(endRow, D1Col).Value <> Date Then 'if active sheet cell value is today's date...
Cells(endRow + 1, D1Col).Value = Date 'set the cell in the next row to today's date
祝你好运
您的代码将无法正常工作,下面的代码将返回错误..
endRow = Cells(Rows.Count, 1).End(xlUp).Row '<--| set 'endRow' to column A If endRow.Offset(1, 0).Value = 0 Then 'Does the zero idicate "if it is blank"?
原因是endRow被分配了Row属性,它返回一个数值而不是一个Range对象,所以你不能从endRow variabel访问Offset对象。 如果你想访问lastNonBlank或firstBlank行使用下面的代码
Public Sub LastRow() Dim lastNonBlankRow As Range Dim firstBlankRow As Range Set lastNonBlankRow = Range("A1").End(xlDown) Set firstBlankRow = Range("A1").End(xlDown).Offset(1, 0) MsgBox lastNonBlankRow.Address MsgBox firstBlankRow.Address End Sub