'Range类的取消组合方法失败'错误

时间:2017-04-06 18:43:56

标签: vba excel-vba loops grouping excel

我正在尝试取消组合并重新组合我存储在数组中的一系列组。但是,当我运行它时,我收到以下错误:

  

运行时错误'1004':

     

Range类的取消组合方法失败

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

我之前使用过这种格式(.Range("A" & variable & ":A" & variable).ungroup)的ungroup和group方法没有问题。

''''' 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 个答案:

答案 0 :(得分:1)

Range.Ungroup

那就是问题所在。

在此之前:

UNGROUP ALL THE THINGS!

您需要确保首先将内容分组。

在未分组的单元格上调用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 会引发您遇到的错误。

我没有找到一种方法来避免在我投入尝试的30秒内出现错误,因此快速而肮脏的解决方法是将“危险”调用包装到自己的程序中,并且吞下它抛出的任何错误:

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

因此:

import tensorflow as tf

a = tf.placeholder(tf.bool)  #placeholder for a single boolean value
b = tf.cond(tf.equal(a, tf.constant(True)), lambda: tf.constant(10), lambda: tf.constant(0))
sess = tf.InteractiveSession()
res = sess.run(b, feed_dict = {a: True})
sess.close()
print(res)