已建立一个表格,在第一行插入一个已知的过去时间,然后根据系统时间算出要向下插入多少行,并插入每行加一的日期,希望每次打开都会自动的更新,怎么弄?
求指教
------解决方案--------------------
你看看能否满足你的要求
Sub InsertRow()
Dim NewDate As Long, OldDate As Long
NewDate = Date
If Sheets(1).Cells(1, 1) = "" Or Val(Sheets(1).Cells(1, 1)) = 0 Then
Exit Sub
End If
OldDate = DateValue(Sheets(1).Cells(1, 1))
If OldDate = 0 Or OldDate >= NewDate Or NewDate - OldDate > 1000 Then
Exit Sub
End If
Application.EnableEvents = False
Dim Temp As Long
For Temp = OldDate + 1 To NewDate
Sheets(1).Rows(1).Insert Shift:=xlDown
Sheets(1).Cells(1, 1) = Temp
Sheets(1).Cells(1, 1).NumberFormatLocal = "YYYY-M-D"
Next Temp
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect([A1:A1], Target) Is Nothing Then
InsertRow
End If
End Sub
Private Sub Workbook_Open()
InsertRow
End Sub
------解决方案--------------------
这个是VBA,为了简便,我将代码调整了一下
打开Excel表后,按Alt+F11进入Visual Basic编辑器
在Sheet1里加入如下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect([A1:A1], Target) Is Nothing Then
InsertRow
End If
End Sub
Public Sub InsertRow()
Dim NewDate As Long, OldDate As Long
NewDate = Date
If Sheets(1).Cells(1, 1) = "" Or Val(Sheets(1).Cells(1, 1)) = 0 Then
Exit Sub
End If
OldDate = DateValue(Sheets(1).Cells(1, 1))
If OldDate = 0 Or OldDate >= NewDate Or NewDate - OldDate > 1000 Then
Exit Sub
End If
Application.EnableEvents = False
Dim Temp As Long
For Temp = OldDate + 1 To NewDate
Sheets(1).Rows(1).Insert Shift:=xlDown