Vba Excel父-子表到树

Vba Excel父-子表到树,vba,excel,Vba,Excel,因此,我试图在excel中组织一系列具有父子关系的流程。有2000多个条目需要组织,所以我想我会继续用宏来组织。我搜索了一下,基本上找到了我想要的东西。但是,当使用顶部答案中的代码时,我不断遇到以下错误: Run-time error '1004': Application-defined or object-defined error 根据VBA编辑器,代码在这一行失败: Cells(Range("Destination").row + row, Range("Destination").C

因此,我试图在excel中组织一系列具有父子关系的流程。有2000多个条目需要组织,所以我想我会继续用宏来组织。我搜索了一下,基本上找到了我想要的东西。但是,当使用顶部答案中的代码时,我不断遇到以下错误:

Run-time error '1004': Application-defined or object-defined error
根据VBA编辑器,代码在这一行失败:

Cells(Range("Destination").row + row, Range("Destination").Column + depth) = header
我知道这是一种转载,但我还没有在其他地方找到帮助

编辑:完整代码张贴在下面。这和原来的帖子一样

Sub MakeTree()

    Dim r As Integer
    ' Iterate through the range, looking for the Root
    For r = 1 To Range("Data").Rows.Count
        If Range("Data").Cells(r, 1) = "Root" Then
            DrawNode Range("Data").Cells(r, 2), 0, 0
        End If
    Next

End Sub

Sub DrawNode(ByRef header As String, ByRef row As Integer, ByRef depth As Integer)

'The DrawNode routine draws the current node, and all child nodes.
' First we draw the header text:
    Cells(Range("Destination").row + row, Range("Destination").Column + depth) = header

    Dim r As Integer
    'Then loop through, looking for instances of that text
    For r = 1 To Range("Data").Rows.Count
        If Range("Data").Cells(r, 1) = header Then
        'Bang!  We've found one!  Then call itself to see if there are any child nodes
            row = row + 1
            DrawNode Range("Data").Cells(r, 2), row, depth + 1
        End If
    Next

End Sub
运行时错误“1004”发生在您尝试写入受保护工作表上的锁定单元格时。取消对工作表的保护或解锁需要写入的单元格

如果要使用VBA保护/取消保护工作表,请查看有关工作表对象的保护和取消保护方法的帮助。另外一个好主意是完全限定您的范围所属的工作簿和工作表,并与。。。以块结束,例如

With Workbooks("book_name.xlsm").Worksheets("sheet_name")
    .Unprotect
    .Cells(.Range("Destination").row + row, .Range("Destination").Column + depth) = header
    .Protect
End With

Protect方法有一组可选参数,例如密码、允许筛选,您可能需要查看这些参数。

是否有一个名为Destination的命名范围?否则,该线路将失败。另外,如果您添加代码而不是以前代码的链接,您的问题会更好。或者它也可以是标题变量。当它出错时,您可以进入调试模式,如果您将鼠标悬停在每个变量上,它将告诉您VBA与每个变量关联的值。目标是我定义的范围。很抱歉表单不好,新的堆栈溢出。