Excel 2016 VBA无法写入未锁定的单元格

时间:2015-12-23 19:49:35

标签: excel excel-vba vba

我在Windows 10上运行Excel 2016。

我有一个非常简单的宏,它读取工作表上的数据并生成一系列汇总值,并将其写入工作表上的特定单元格。失败的指令是: .Cells(nSumRow,nTester).Value = aSums(TstCount)

执行此命令时,系统返回错误代码1004.如果写入命令,它也会失败: Activesheet.Cells(nSumRow,nTester).Value = aSums(TstCount)

Debug显示所有值都已正确设置,

使用debug我试图写入.Cells(1,1),它也失败了。

表格如果不受保护且单元格未锁定。

任何想法?


Function ProcessData(rData As Range) As String
    Dim nAreaRow As Long                ' The current row within a range
    Dim nColumn As Long             '
    Dim nTotalMutations As Long     ' The total allele variance
    Dim nUniqueMarkers As Long      ' Number of unique markers with a mutation
    Dim nFirstMarker As Long        ' Column of DYS393
    Dim nLastMarker As Long         ' Column of DYS565
    Dim nModal As Long              ' The Row where the Modal values are shown
    Dim aMarkers() As Long          ' Container for the count of deviations
    Dim nArea As Long               ' The current range being processed
    Dim nTester As Long             ' Colunn for the Number of Haplotypes processed
    Dim nRaw                        ' Column for the Total(Raw) Mutations found in the range
    Dim nUnique                     ' Column for the Number of Unique Markers with Mutation
    Dim nAvg                        ' Column for the average Number of Mutations per Tester
    Dim nSumRow As Long             ' Row where the summary values are to be placed
    Dim aSums() As Long
    Const TstCount = 1
    Const TstUnique = 2
    Const TstRaw = 3
    Const TstAvg = 4

        'Initialize common work variables
        nFirstMarker = GetColumn("393", ActiveSheet)
        nLastMarker = GetColumn("565", ActiveSheet)
        nModal = GetRow("393", ActiveSheet) + 1
        nTester = GetColumn("Testers", ActiveSheet)
        nRaw = GetColumn("Raw", ActiveSheet)
        nUnique = GetColumn("Unique", ActiveSheet)
        nAvg = GetColumn("Avg", ActiveSheet)
        nSumRow = ActiveCell.Row
        ReDim aMarkers(nLastMarker + 1)
        ReDim aSums(6)

        With Worksheets("Sheet1")
            On Error GoTo ERROR_Submittal
            ' Process all Ranges that were selected
            For nArea = 1 To rData.Areas.Count
                ' Process all rows in this area
                For nAreaRow = 1 To rData.Areas(nArea).Rows.Count
                    ' If there are no more rows, exit the loop and process the next range
                    If IsError(rData.Areas(nArea).Item(nAreaRow, 1).Value) Then Exit For
                    ' if this is not an empty row then
                    If Not IsEmpty(rData.Areas(nArea).Item(nAreaRow, 1).Value) Then
                        ' Bump the count of rows/Testors processed
                        aSums(TstCount) = aSums(TstCount) + 1
                        ' scan all of the defined markers
                        For nX = nFirstMarker To nLastMarker Step 1
                            ' compute up the Allele diversity
                            aMarkers(nX) = Abs(.Cells(nModal, nX).Value - .Cells(rData.Areas(nArea).Item(nAreaRow, 1).Row, nX).Value)
                        Next nX
                        For nX = LBound(aMarkers) To UBound(aMarkers)
                            If aMarkers(nX) > 0 Then
                                aSums(TstUnique) = aSums(TstUnique) + 1
                                aSums(TstRaw) = aSums(TstRaw) + aMarkers(nX)
                                aSums(TstAvg) = Application.WorksheetFunction.RoundUp(aSums(TstRaw) / aSums(TstCount), 0)
                            End If
                        Next nX
                    End If
                Next nAreaRow
            Next nArea
            .Cells(nSumRow, nTester).Value = aSums(TstCount)
            .Cells(nSumRow, nRaw).Value = aSums(TstRaw)
            .Cells(nSumRow, nUnique).Value = aSums(TstUnique)
            .Cells(nSumRow, nAvg).Value = aSums(TstAvg)
    End With
EXIT_Function:
    Exit Function

ERROR_Submittal:
    GoTo EXIT_Function
End Function

0 个答案:

没有答案