在另一张纸上打印,为什么不工作?

所以我有这个button代码时钟输出,它的工作正常,但我的问题是,该button是在第一张表,我需要它在时间表不同的表上加盖时间表。 这是我的代码。 我想如果我用Timesheetreplace每个括号中的单词Location并且它不会在时间表中标记时间。

Sub StampNext() Dim r As Range On Error Resume Next Set r = Range("Timesheet") On Error GoTo 0 If r Is Nothing Then Range("A1").Name = "Timesheet" With Range("Timesheet") .Value = Time .NumberFormat = "hh:mm" If .Row = 1 Then .Offset(2, 0).Name = "Timesheet" Else .Offset(-2, 1).Name = "Timesheet" End If End With End Sub 

我也有时间表保护,因为我的老板不希望员工篡改表。 我不知道这是否会成为代码的阻碍问题,或者只是没关系。

 Sub StampNext() 'Declare a variable as a worksheet and a variable for the named range Dim wsT As Worksheet Dim wsA as Worksheet Dim rngT As Range 'Set that variable to the worksheet named "Timesheet" Set wsT = ThisWorkbook.Worksheets("Timesheet") Set wsA = ActiveSheet 'Unprotect the sheet using the password. '(Note: you should protect the VBA project from viewing to ' prevent others from viewing the password.) wsT.Unprotect ("sheetPassword") wsT.Activate 'Set Range variable On Error Resume Next Set rngT = wsT.Range("Timesheet") On Error GoTo 0 If rngT Is Nothing Then wsT.Range("A1").Name = "Timesheet" Set rngT = wsT.Range("Timesheet") End If 'Add value and update the named range "Timesheet" With rngT .Value = Time .NumberFormat = "hh:mm" If .Row = 1 Then .Offset(2, 0).Name = "Timesheet" Else .Offset(-2, 1).Name = "Timesheet" End If End With 'Reprotect sheet with password wsT.Protect ("sheetPassword") wsA.activate End Sub 

您需要声明工作表对象,然后可以写入它。

 Dim ws1 As Excel.Worksheet Set ws1 = ActiveWorkbook.Sheets("Timesheet") ws1.Range("A1").Value = Time 

我不确定受保护的表单,但是我的猜测是你将不得不临时解除保护。