【问题标题】:How to apply same code to new worksheets?使用完全相同的代码制作新工作表
【发布时间】:2022-01-26 18:04:38
【问题描述】:

这是我下面的代码。我想复制它,以便它适用于每个新工作表,到目前为止,我必须进入并手动将代码更改为新工作表名称。这是很多工作,必须有一个我不知道的解决方法。我发现了循环,但如果该编码有效,我不会。它至少对我不起作用。我正在尝试为计费时间创建秒表功能。

我可以复制工作表并创建副本,但是在它说的新工作表中没有任何功能起作用

运行时错误 1004:范围类的选择方法失败

并停在这条线上:

Sheets("Client").Range("B" & iRow).Select 

任何帮助将不胜感激!!!

Sub Intialize()

    Dim iRow As Long
    
    iRow = Sheets("Client").Range("F" & Application.Rows.Count).End(xlUp).Row
    
    'Code to Validate
    
    If Sheets("Client").Range("D" & iRow).Value = "" Then
    
        Sheets("Client").Range("A" & iRow).Value = Format([Today()], "DD-MMM-YYYY")

        
    End If

End Sub
Sub Start_Time()

    Dim iRow As Long
    
    iRow = Sheets("Client").Range("F" & Application.Rows.Count).End(xlUp).Row + 1
    
    'Code to Validate
    
    If Sheets("Client").Range("B" & iRow).Value = "" Then
    
        MsgBox "Please select the Task Name from the drop down.", vbOKOnly + vbInformation, "Task Name Blank"
        Sheets("Client").Range("B" & iRow).Select
        Exit Sub
        
    ElseIf Sheets("Client").Range("D" & iRow).Value <> "" Then
    
        MsgBox "Start Time is aleady captured for the selected Task."
        Exit Sub
    Else
        
        Sheets("Client").Range("D" & iRow).Value = [Now()]
        
        Sheets("Client").Range("D" & iRow).NumberFormat = "hh:mm:ss AM/PM"
    
    End If

End Sub
Sub End_Time()

    Dim iRow As Long
    
    iRow = Sheets("Client").Range("F" & Application.Rows.Count).End(xlUp).Row + 1
    

    'Code to Validate
    
    If Sheets("Client").Range("D" & iRow).Value = "" Then
    
        MsgBox "Start Time has not been captured for this task."
        Exit Sub
    Else
        
        Sheets("Client").Range("E" & iRow).Value = [Now()]
        
        Sheets("Client").Range("E" & iRow).NumberFormat = "hh:mm:ss AM/PM"
        
        Sheets("Client").Range("F" & iRow).Value = Sheets("Client").Range("E" & iRow).Value - Sheets("Client").Range("D" & iRow).Value
        
        Sheets("Client").Range("F" & iRow).NumberFormat = "hh:mm:ss"
    
    End If

    Call Intialize

End Sub

【问题讨论】:

  • 那么,您收到“错误 1004”,是不是说别的什么?我可以猜到它说了什么,但这是你应该在你的问题中提出的。当您遇到一些错误时,请务必将完整的错误消息复制到您的问题中,以及给出错误时您到底在做什么。
  • 谢谢你,汤姆!是的,这是我得到的错误代码。对于那个很抱歉。具体来说,它说“运行时错误1004:范围类的选择方法失败”
  • 它停在这个:Sheets("Client").Range("B" & iRow).Select
  • 旁注:您在Start_Time() 中的Exit Subs 完全没有做任何事情,而End_Time() 中的Call Intialize 可以通过在@ 中移动来轻松删除987654329@块。
  • 我不确定您的代码是如何放置的(模块与工作表、与数据相同的工作簿等)。您的问题可能是只有在该工作表处于活动状态时才能选择范围,但您还应该小心调用没有明确限定哪个工作簿的代码。所以列是 A = 日期、B = 任务、D = 开始时间、E = 结束时间、F = 持续时间?

标签: excel vba worksheet


【解决方案1】:

好吧,既然你说: 1 需要复制一张Sheet 2 将代码应用于该工作表,无论名称如何 3 您将创建(复制)多个工作表。

这是我的代码。

(将所有内容粘贴到普通模块中)

Option Explicit
    
    Const A = 1
    Const B = 2
    Const D = 4
    Const E = 5
    Const F = 6
    Const L = 1048576 'Excel.Application.Rows.Count   

'With this you can check if you can copy the sheet
'and also, return that sheet you already checked,
'no matter the name of that sheet.

Private Function SetSheet(sht As Worksheet) As Worksheet

    'This function validate if the sheet is one that you need to copy
    'Assuming the first sheets of the book are used to:
    '
    'Parameters         1
    'Main               2
    'Other...           3
    
    If sht.Index >= 4 Then 'Here is where (Sheet #4 and so on) begins...
        Set SetSheet = sht
    Else
        'Message or do nothing...
        End
    End If
End Function

Sub Intialize()
'    You can uncomment this DIM vars but
'    need to comment the const above.

'    Dim F: F = Range("F1").Column
'    Dim D: D = Range("D1").Column
'    Dim A: A = Range("A1").Column
'    Dim L: L = Application.Rows.Count

    Dim ActSht As Worksheet: Set ActSht = SetSheet(ActiveSheet)
    Dim iRow As Long
    iRow = ActSht.Range(Cells(L, F), Cells(L, F)).End(xlUp).Row

'    Code to Validate

    If ActSht.Range(Cells(iRow, D), Cells(iRow, D)).Value = "" Then
        ActSht.Range(Cells(iRow, A), Cells(iRow, A)).Value = Format([Today()], "DD-MMM-YYYY")
    End If
End Sub

Sub Start_Time()
'    Dim B: B = Range("B1").Column
'    Dim F: F = Range("F1").Column
'    Dim D: D = Range("D1").Column
'    Dim L: L = Application.Rows.Count

    Dim ActSht As Worksheet: Set ActSht = SetSheet(ActiveSheet)
    Dim iRow As Long
    iRow = ActSht.Range(Cells(L, F), Cells(L, F)).End(xlUp).Row + 1
    
'    Code to Validate

    If ActSht.Range(Cells(iRow, B), Cells(iRow, B)).Value = "" Then
        MsgBox "Please select the Task Name from the drop down.", vbOKOnly + vbInformation, "Task Name Blank"
        ActSht.Range(Cells(iRow, B), Cells(iRow, B)).Select
        Exit Sub
    ElseIf ActSht.Range(Cells(iRow, D), Cells(iRow, D)).Value <> "" Then
        MsgBox "Start Time is aleady captured for the selected Task."
        Exit Sub
    Else
        ActSht.Range(Cells(iRow, D), Cells(iRow, D)).Value = [Now()]
        ActSht.Range(Cells(iRow, D), Cells(iRow, D)).NumberFormat = "hh:mm:ss AM/PM"
    End If
End Sub

Sub End_Time()
'    Dim D: D = Range("D1").Column
'    Dim F: F = Range("F1").Column
'    Dim E: E = Range("E1").Column
'    Dim L: L = Application.Rows.Count

    Dim ActSht As Worksheet: Set ActSht = SetSheet(ActiveSheet)
    Dim iRow As Long
    iRow = ActSht.Range(Cells(L, F), Cells(L, F)).End(xlUp).Row + 1

'    Code to Validate
    If ActSht.Range(Cells(iRow, D), Cells(iRow, D)).Value = "" Then
        MsgBox "Start Time has not been captured for this task."
        Exit Sub
    Else
        ActSht.Range(Cells(iRow, E), Cells(iRow, E)).Value = [Now()]
        ActSht.Range(Cells(iRow, E), Cells(iRow, E)).NumberFormat = "hh:mm:ss AM/PM"
        
        ActSht.Range(Cells(iRow, F), Cells(iRow, F)).Value = Sheets("Client").Range("E" & iRow).Value - Sheets("Client").Range("D" & iRow).Value
        ActSht.Range(Cells(iRow, F), Cells(iRow, F)).NumberFormat = "hh:mm:ss"
    End If
    Call Intialize
End Sub

假设: 1 您从原始工作表中调用一个或多个此子规则。

注意: 最好不要以这种方式使用硬编码Sheets("Client").Range("D" &amp; iRow).Value,因为当您需要调试时......很痛!这就是我更喜欢ActSht.Range(Cells(iRow, D), Cells(iRow, D)).Value 的原因,您可以在所有代码之上控制单个变量。

【讨论】:

  • 非常感谢@ElbertVillarreal!我只是尝试复制并粘贴到一个新模块中,并分配了新的宏。但是计时器功能不再起作用。它确实会显示错误代码,但不会显示主代码。它说此代码的第一行存在问题。私人子线。 Private Sub Workbook_Open() '在下一个空白行填写日期和名称'调用初始化结束子
  • 嗯,重要的是分享完整的代码来帮助你,因为,你在谈论代码的另一部分Private Sub Workbook_Open()。这行代码进入Thisworkbook(通常称为)。并且消息(我假设)Fill the Date and Name in next blank row 是该私有函数的一部分。
  • 如果可能的话,请将那部分代码或整个代码分享到问题中。并分享错误(截图),看看为什么会发生这种情况。
  • (1) 记住Private Function SetSheet(sht As Worksheet) As Worksheet 可以这样公开Function SetSheet(sht As Worksheet) As Worksheet 就是这样
  • (1) 请记住 Private Function SetSheet(sht As Worksheet) As Worksheet 可以通过这种方式公开 Function SetSheet(sht As Worksheet) As Worksheet ,仅此而已 (2) 如果函数 SetSheet 在您定义为要复制的初始表的编号下命中索引表,则代码将以没有警告的方式结束,并且工作簿中的所有代码都将结束。 (3) 哪个是“定时器”功能?重要的是分享该功能以帮助您。
猜你喜欢
  • 1970-01-01
  • 2013-01-04
  • 2022-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多