我读过的所有内容都表明我正确地表示了变量并调用了要激活的工作表。最后一行是我遇到类型不匹配的地方。那时CPIDws = CERN000006。我在某处阅读过,名称是字母和数字可能会出现问题,但尚未找到解决方法。

Sub Create_tab()

Dim newWS As Worksheet, CernWS As Worksheet, CPID As Variant
Dim Template As Worksheet, CPIDclm As Long, CERNdata As Range, CPIDcheck As Variant
Dim lngRow As Long, lngCol As Long, i As Integer, CPIDws As Worksheet

Set Template = Sheets("Template")
Set CernWS = Sheets("CERN ID's")
'Set lngRow = 1
'Set lngCol = 1


CernWS.Activate
Cells(1, 1).Select

Do
    ActiveCell.Offset(1, 0).Select
    Set CPID = ActiveCell

    'create a new sheet as a copy of the template
    Sheets("Template").Copy _
           after:=ActiveWorkbook.Sheets(ThisWorkbook.Sheets.Count)

    'Name the new sheet as the current CPID value from CERN ID's worksheet
    ActiveSheet.Name = CPID
    Set CPIDws = ActiveSheet

    'interigate AAA Data and update the new sheet with the data specific to the current cpid
    Sheets("AAA Data").Activate
    Cells(2, 3).Activate
    Set CPIDcheck = ActiveCell

    Do
        If CPID = CPIDcheck Then
            ActiveCell.Offset(0, -2).Select
            Set CERNdata = Range(Selection, Selection.End(xlToRight))
        End If

        Sheets(CPIDws).Activate

最佳答案

那时CPIDws = CERN000006。


不,不是。 :)

您已经声明了CPIDws As Worksheet,但是您将其用作Sheets方法的参数,该方法采用索引(整数)或名称(字符串)值。

因此,类型不匹配。

只需尝试CPIDws.Activate

或者,可以说是多余的:Sheets(CPIDws.Name).Activate

THIS可能也有帮助,因为通常建议在可以避免的情况下不要依赖Active(单元格,工作表等)或Selection(几乎总是这样,除了某些情况下,您可以使用Selection作为输入方法,但通常情况下,您的宏可能永远不需要选择或激活用户选择输入的单元格以外的其他任何单元格。 >并完全通过代码控制迭代,完全不需要Cells(1,1)Select任何东西。

09-28 00:17