我和我的朋友目前有一个大型电子表格,我需要经常将其拆分为小型电子表格。这曾经是一个手工过程,但我希望自动化。我在VBA中创建了一个三步解决方案,可以帮助我完成以下操作:
在调试代码时,我发现在这一行代码处出现错误信息:
只有一个Excel工作簿保持打开状态,而且只有一行可见(从包含标题信息的主表中提取的第二行),除此之外什么都没有。
到底发生了什么?
以下是我目前的代码: 核心代码
运行该进程以保存工作簿。
- 向电子表格应用相关过滤器
- 导出当前可见(经过筛选的)数据到新的电子表格
- 保存电子表格并返回到1(不同的标准)
![enter image description here](https://istack.dev59.com/ZdZjJ.webp)
![enter image description here](https://istack.dev59.com/mK5xx.webp)
到底发生了什么?
以下是我目前的代码: 核心代码
' 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
A2:22
不是“范围内的单个单元格”。错误消息中的单词通常具有含义。 :-) - Ken Whiteitm
的值,并向我们展示您正在尝试过滤的列表。 也许您需要在调用TokenNotActivated后使用断点(F9)来中断代码,并查看循环中是否有任何行可供筛选,然后尝试使用F8逐行调试。 - Our Man in BananasLastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
这一行可能是itm为空的原因。 - MP24