2017-04-06 91 views
2

我想取消组合并重新组合我存储在数组中的一系列组。但是,当我运行它时,出现以下错误:'范围类失败的取消组合方法'错误

Run-time error '1004':

Ungroup method of Range class failed

我的代码如下。 xxxDim变量用于识别数组是否存在。我以前正确识别所有sSubeSubsGroupeGroupsCateCat变量,因此实际值是关注的不是。我已经使用所指的确切范围手动完成了取消组合和重新组合,所以我不相信我指的是一个范围,它很难找出解组/分组。

我已经使用这个格式(.Range("A" & variable & ":A" & variable).ungroup)之前没有问题取消分组和分组的方法。

''''' START WITH UNGROUPING EVERYTHING ''''' 
    ''''' SUB-MODELS ''''' 
    If sSubDim = True Then  'This Array has been dimensioned and therefore has at least one entry 
     For i = LBound(sSub) To UBound(sSub) 
      .Range("A" & sSub(i) & ":A" & eSub(i)).Ungroup 'Receive error on this line first 
     Next 
    End If 

    ''''' GROUPS ''''' 
    If sGroupDim = True Then 'Multiple groups exist 
     For i = LBound(sGroup) To UBound(sGroup) 
      .Range("A" & sGroup(i) & ":A" & eGroup(i)).Ungroup 
     Next 
    End If 

    ''''' CATEGORY ''''' 
    .Range("A" & sCat & ":A" & eCat).Ungroup 


''''' NOW GROUP EVERYTHING ''''' 
    ''''' SUB-MODELS ''''' 
    If sSubDim = True Then  'This Array has been dimensioned and therefore has at least one entry 
     For i = LBound(sSub) To UBound(sSub) 
      .Range("A" & sSub(i) & ":A" & eSub(i)).Group 
     Next 
    End If 

    ''''' GROUPS ''''' 
    If sGroupDim = True Then 'Multiple groups exist 
     For i = LBound(sGroup) To UBound(sGroup) 
      .Range("A" & sGroup(i) & ":A" & eGroup(i)).Group 
     Next 
    End If 

    ''''' CATEGORY ''''' 
    .Range("A" & sCat & ":A" & eCat).Group 

回答

1
''''' START WITH UNGROUPING EVERYTHING ''''' 

那么有问题。

之前,你可以这样做:

UNGROUP ALL THE THINGS!

你需要确保事情在首位分组。

调用上未编组单元Range.Ungroup,会提高你得到的错误。

我还没有找到一种方法来避免在30秒内致力于尝试的错误,所以一个快速而又肮脏的解决方法是将“危险”调用包装到它自己的过程中,并且它抛出任何错误:

Private Sub UngroupSafely(ByVal target As Range) 
    On Error Resume Next 
    target.Ungroup 
    Err.Clear ' Err.Number will be 1004 if target was already ungrouped 
    On Error GoTo 0 
End Sub 

这样:

If sSubDim Then 
    For i = LBound(sSub) To UBound(sSub) 
     UngroupSafely .Range("A" & sSub(i) & ":A" & eSub(i)) 
    Next 
End If 
+0

我所有的团体我拆组已经肯定分组。我最终将'.Range(“A”&variable&“:A”&variable).ungroup'更改为'.Rows(variable&“:”&variable).ungroup',它解决了我的问题。不知道为什么以前的范围适用于我使用它的部分,而不是在这里。 – cheshire

+0

如果他们已经分组,那么他们将不会大惊小怪。如果你在非分组范围内调用'.Ungroup',你会得到完全错误的结果。验证你的输入。 –

+1

有趣的答案,顺便说它适用于我。 thx @ Mat'sMug – neverwinter