我的VBA代码用于在不同的工作表中分离筛选器,但创建了空的工作表。

huangapple go评论66阅读模式
英文:

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(&quot;A1:C1&quot;).AutoFilter
r = 1
Do
    r = r + 1
    brand = ws.Range(&quot;A&quot; &amp; r).Value
    On Error Resume Next
    If Sheets(brand) Is Nothing Then
        ws.Range(&quot;A1:C1&quot;).AutoFilter field:=1, Criteria1:=brand
        ws.Range(&quot;A1&quot;).CurrentRegion.SpecialCells(xlCellTypeVisible).Copy
        Sheets.Add.Name = brand
        Sheets(brand).Paste
        ws.ShowAllData
    End If
Loop While ws.Range(&quot;A&quot; &amp; r + 1).Value &lt;&gt; &quot;&quot;
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(&quot;A2&quot;)   &#39;first brand
    
    Do While Len(cBrand.Value) &gt; 0
        
        brand = cBrand.Value
        
        Set wsBrand = Nothing  &#39;clear sheet reference
        On Error Resume Next   &#39;ignore error if no sheet match
        Set wsBrand = wb.Worksheets(brand)
        On Error GoTo 0        &#39;stop ignoring errors ASAP
        
        If wsBrand Is Nothing Then  &#39;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(&quot;A1&quot;) &#39;copy headers
        End If
        
        cBrand.EntireRow.Copy wsBrand.Cells(Rows.Count, 1).End(xlUp).Offset(1)
        Set cBrand = cBrand.Offset(1) &#39;Next row of data
    Loop
End Sub

huangapple
  • 本文由 发表于 2023年2月7日 01:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定