英文:
My VBA code to separate a filter in different worksheets is creating empty worksheets
问题
Sub um_separatabelanosfiltros()
Dim r As Integer, brand As String, ws As Worksheet
Set ws = ActiveSheet
ws.Range("A1:C1").AutoFilter
r = 1
Do
r = r + 1
brand = ws.Range("A" & r).Value
On Error Resume Next
If Sheets(brand) Is Nothing Then
ws.Range("A1:C1").AutoFilter field:=1, Criteria1:=brand
ws.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy
Sheets.Add.Name = brand
Sheets(brand).Paste
ws.ShowAllData
End If
Loop While ws.Range("A" & r + 1).Value <> ""
End Sub
使用这段代码,你可以根据单元格A1中的筛选条件将工作表分隔开。然而,当尝试在不同的数据集中使用它时,会创建空的工作表。有任何建议或帮助吗?
我尝试了很多方法,我的结论是数据在某种方式上搞砸了它,我不知道它是如何做到的,也不知道如何修复它。
英文:
Sub um_separatabelanosfiltros()
Dim r As Integer, brand As String, ws As Worksheet
Set ws = ActiveSheet
ws.Range("A1:C1").AutoFilter
r = 1
Do
r = r + 1
brand = ws.Range("A" & r).Value
On Error Resume Next
If Sheets(brand) Is Nothing Then
ws.Range("A1:C1").AutoFilter field:=1, Criteria1:=brand
ws.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy
Sheets.Add.Name = brand
Sheets(brand).Paste
ws.ShowAllData
End If
Loop While ws.Range("A" & r + 1).Value <> ""
End Sub
With this code i can separate a worksheet by the filters in the A1 cell, but i tried to use it in a different dataset and it kept creating empty worksheets, any sugestion and or help?
Iv tried alot of thing and my conclusion is that some how the data is screwing it up, i just dont know how it can do that or how to fix it
答案1
得分: 0
以下是代码的翻译部分:
如果您没有准备好的在要拆分的列中的唯一项目列表,您可以在不使用自动筛选的情况下执行以下操作:
Sub um_separatabelanosfiltros()
Dim brand As String, wsData As Worksheet, wsBrand As Worksheet
Dim wb As Workbook, cBrand As Range
Set wb = ActiveWorkbook
Set wsData = ActiveSheet
Set cBrand = wsData.Range("A2") '第一个品牌
Do While Len(cBrand.Value) > 0
brand = cBrand.Value
Set wsBrand = Nothing '清除工作表引用
On Error Resume Next '忽略错误,如果没有匹配的工作表
Set wsBrand = wb.Worksheets(brand)
On Error GoTo 0 '尽快停止忽略错误
If wsBrand Is Nothing Then '需要创建新工作表吗?
Set wsBrand = wb.Worksheets.Add(after:=wb.Worksheets(wb.Worksheets.Count))
wsBrand.Name = brand
wsData.Rows(1).Copy wsBrand.Range("A1") '复制标题
End If
cBrand.EntireRow.Copy wsBrand.Cells(Rows.Count, 1).End(xlUp).Offset(1)
Set cBrand = cBrand.Offset(1) '下一行数据
Loop
End Sub
请注意,我只翻译了代码部分,其他内容保持原样。
英文:
If you don't have a ready list of unique items in the column you want to split on, you can do it without autofilter:
Sub um_separatabelanosfiltros()
Dim brand As String, wsData As Worksheet, wsBrand As Worksheet
Dim wb As Workbook, cBrand As Range
Set wb = ActiveWorkbook
Set wsData = ActiveSheet
Set cBrand = wsData.Range("A2") 'first brand
Do While Len(cBrand.Value) > 0
brand = cBrand.Value
Set wsBrand = Nothing 'clear sheet reference
On Error Resume Next 'ignore error if no sheet match
Set wsBrand = wb.Worksheets(brand)
On Error GoTo 0 'stop ignoring errors ASAP
If wsBrand Is Nothing Then 'need to create a new sheet?
Set wsBrand = wb.Worksheets.Add(after:=wb.Worksheets(wb.Worksheets.Count))
wsBrand.name = brand
wsData.Rows(1).Copy wsBrand.Range("A1") 'copy headers
End If
cBrand.EntireRow.Copy wsBrand.Cells(Rows.Count, 1).End(xlUp).Offset(1)
Set cBrand = cBrand.Offset(1) 'Next row of data
Loop
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论