[英]Printing on a different sheet, why isn't it working?
所以我有這個按鈕代碼進入和退出,它工作正常,但我的問題是按鈕是在第一張紙上,我需要它在一個名為Timesheet的不同工作表上標記時間。 這是我的代碼。 我想如果我用單詞Timesheet替換每個括號中的位置,它就不會在時間表中標記時間。
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
我還保護時間表,因為我的老板不希望員工篡改工作表。 我不知道這對代碼是否會成為一個阻礙問題,或者這無關緊要。
您需要聲明工作表對象,然后您可以寫入它。
Dim ws1 As Excel.Worksheet
Set ws1 = ActiveWorkbook.Sheets("Timesheet")
ws1.Range("A1").Value = Time
我不確定受保護的紙張,但我的猜測是你必須暫時取消保護。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.