我很困惑。我有一个工作簿,用作部分业务的模板作为注册。用户建立他们正在跟踪的注册项目列表。对于主寄存器中的每个项目,我需要创建一个工作表,以提供有关该问题的更多详细信息。新工作表是工作簿“TemplateCRA”中模板的副本。当在登记表“所有权”中制作或更新所有主菜时,使用单个宏完成创建操作
我从这个开始:
Sub Button1_Click()
'
' Button1_Click Macro
'
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("Ownership").Range("B11:B30")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
For Each MyCell In MyRange
If IsEmpty(MyCell) Then End
Sheets("TemplateCRA").Copy After:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = "CRA Ref " & MyCell.Value ' renames the new worksheet
Range("B6").Value = ActiveSheet.Name
Next MyCell
End Sub
然后我继续这样做,以尝试确保宏首先检查尚未为注册项目创建工作表,如果是,则提醒用户,然后继续循环向下项目列表并创建所需的新工作表.
Sub Button2_Click()
'
' Button2_Click Macro
'
Dim MyCell As Range, MyRange As Range
Dim sh As Worksheet, flg As Boolean
Set MyRange = Sheets("Ownership").Range("B11:B30")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
For Each MyCell In MyRange
If IsEmpty(MyCell) Then End
For Each sh In Worksheets
If sh.Name Like "CRA Ref " & MyCell.Value Then flg = True: Exit For
Next
If flg = True Then
MsgBox sh.Name & " Found!"
ElseIf flg = False Then
MsgBox "Creating CRA Ref " & MyCell.Value & " now!"
Sheets("TemplateCRA").Copy After:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = "CRA Ref " & MyCell.Value ' renames the new worksheet
Range("B6").Value = ActiveSheet.Name
End If
Next MyCell
MsgBox "You may now complete your CRA for each item"
End Sub
但这不能正常工作。似乎发生的是:
sh.Name 检查循环通过 OK 报告找到工作表,直到找到没有工作表的项目
Run Time error 91 - object variable with block variable not set
在第一个 MsgBox 行中报告 a。
谁能建议我有什么问题?
干杯