下面是我打开工作簿时自动运行TimeStamp宏的代码。但是,它仅在时间是早上6:45或更晚时运行。我的问题是我试图让它清除第2行中A列到D列中的单元格内容,如果时间早于上午6:45之前和运行TimeStamp宏之前。我尝试只用A列来做,调试器说顶线是一个问题。我似乎无法弄清楚为什么它是一个问题。我假设我打破了某种语法规则,但我不确定。
Private Sub Workbook_Open()
If Time > TimeSerial(6, 45, 0) Then
Call TimeStamp
Else
Sub Clear()
Dim wb3 As Workbook
Set wb3 = ThisWorkbook
With wb3.Worksheets("Avnet")
.Range("A2:A" & .Range("A2").End(xlDown).Row).ClearContents
End With
End Sub
Application.Wait "06:45:00"
Call TimeStamp
End Sub
在我添加Sub Clear()部分之前它才有用......
更新:
刚尝试将Sub Clear()作为一个单独的模块并使用call Clear()
,但这也没有用。这是它的样子
Clear()代码
Sub Clear()
Dim wb3 As Workbook
Set wb3 = ThisWorkbook
With wb3.Worksheets("Avnet")
Range("A2:A" & .Range("A2").End(xlDown).Row).ClearContents
End With
End Sub
要调用Clear()的部分
Private Sub Workbook_Open()
If Time > TimeSerial(20, 0, 0) Then
Call TimeStamp
Else
Call Clear
Application.Wait "19:35:00"
Call TimeStamp
End Sub
答案 0 :(得分:2)
在sub中声明一个sub是行不通的(我99.9999%肯定,但是我从来没有尝试过这个!)
移动Workbook_Open
声明,使其在Clear
之后,然后从Workbook_Open
内拨打TimeStamp
,方法与调用End If'
的方式相同。 (你在WorkBook_open结束时也错过了Private Sub Workbook_Open()
If Time > TimeSerial(6, 45, 0) Then
Call TimeStamp
Else
Call Clear
Application.Wait "06:45:00"
Call TimeStamp
End If
End Sub
Sub Clear()
Dim wb3 As Workbook
Set wb3 = ThisWorkbook
With wb3.Worksheets("Avnet")
.Range("A2:A" & .Range("A2").End(xlDown).Row).ClearContents
End With
End Sub
所以你的代码会变成:
Private Sub Workbook_Open()
If Time <= TimeSerial(6, 45, 0) Then
Call Clear
Application.Wait "06:45:00"
End If
Call TimeStamp
End Sub
另外,您的Workbook_Open代码可以简化为:
INETSHORTD
1 94
2 85,
3 94, 92
4 89, 99, 32,