Excel VBA中自动生成电子表格的功能

4
我和我的朋友目前有一个大型电子表格,我需要经常将其拆分为小型电子表格。这曾经是一个手工过程,但我希望自动化。我在VBA中创建了一个三步解决方案,可以帮助我完成以下操作:
  1. 向电子表格应用相关过滤器
  2. 导出当前可见(经过筛选的)数据到新的电子表格
  3. 保存电子表格并返回到1(不同的标准)
不幸的是,我在实施时遇到了困难。每当我尝试生成电子表格时,我的文件就会挂起,并开始执行多次计算,然后显示以下错误信息: enter image description here 在调试代码时,我发现在这一行代码处出现错误信息: enter image description here 只有一个Excel工作簿保持打开状态,而且只有一行可见(从包含标题信息的主表中提取的第二行),除此之外什么都没有。
到底发生了什么?
以下是我目前的代码: 核心代码
' This bit of code get's all the primary contacts in column F, it does 
' this by identifying all the unique values in column F (from F3 onwards)   
Sub GetPrimaryContacts()   
    Dim Col As New Collection
    Dim itm
    Dim i As Long
    Dim CellVell As Variant 

    'Get last row value
    LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row  

    'Loop between all column F to get unique values
    For i = 3 To LastRow
        CellVal = Sheets("Master").Range("F" & i).Value
        On Error Resume Next
        Col.Add CellVal, Chr(34) & CellVal & Chr(34)
        On Error GoTo 0
    Next i    

    ' Once we have the unique values, apply the TOKEN NOT ACTIVATED FILTER
    Call TokenNotActivated
    For Each itm In Col
        ActiveSheet.Range("A2:Z2").Select
        Selection.AutoFilter Field:=6, Criteria1:=itm          
        ' This is where the magic happens... creating the individual workbooks
        Call TokenNotActivatedProcess
    Next
    ActiveSheet.AutoFilter.ShowAllData   
End Sub
"令牌未激活"过滤器
Sub TokenNotActivated()    
'Col M = Yes
'Col U = provisioned
ThisWorkbook.Sheets(2).Activate
ActiveSheet.Range("A2:Z2").Select
Selection.AutoFilter Field:=13, Criteria1:="Yes"
Selection.AutoFilter Field:=21, Criteria1:="provisioned", Operator:=xlFilterValues   
End Sub

运行该进程以保存工作簿。
Function TokenNotActivatedProcess()
    Dim r As Range, n As Long, itm, FirstRow As Long
    n = Cells(Rows.Count, 1).End(xlUp).Row
    Set r = Range("A1:A" & n).Cells.SpecialCells(xlCellTypeVisible)
    FirstRow = ActiveSheet.Range("F2").End(xlDown).Row
    itm = ActiveSheet.Range("F" & FirstRow).Value
    If r.Count - 2 > 0 Then Debug.Print itm & " - " & r.Count - 2
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveWorkbook.SaveAs Filename:="C:\Working\Testing\TokenNotActivated - " & itm + ".xls", FileFormat:=52, CreateBackup:=False
End Function

2
错误信息中的指令不是很清楚吗?请在范围内选择单个单元格,然后再次尝试该命令。A2:22 不是“范围内的单个单元格”。错误消息中的单词通常具有含义。 :-) - Ken White
@KenWhite - 我想选择那个范围,这不可能吗?目前只有第二行被保存到新的工作簿中,之后我会收到一个错误消息,没有更多的工作簿被保存... - methuselah
@methuselah:当出现错误时,您能否添加itm的值,并向我们展示您正在尝试过滤的列表。 也许您需要在调用TokenNotActivated后使用断点(F9)来中断代码,并查看循环中是否有任何行可供筛选,然后尝试使用F8逐行调试。 - Our Man in Bananas
我也想知道:当出现错误时,itm的值是多少?我怀疑它是空的,并且LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row这一行可能是itm为空的原因。 - MP24
1
你的朋友@jmb吗? - Jean-François Corbett
显示剩余5条评论
1个回答

2
这个错误是由于尝试过滤一个空范围引起的。经过分析您的代码,我的猜测是您在这里缺少工作表激活,因为在调用函数TokenNotActivated后重复行ActiveSheet.Range("A2:Z2").Select是没有意义的,也许您的代码正在尝试过滤一些空范围/工作表。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接