0

在下面的代码行中,我试图对变量范围(U 列)求和。您能否让我知道我在代码中做错了什么(感谢您的帮助):

有问题的代码行:

Sheets(gcsReportSheetName).Cells(LCounter, 3).FormulaR1C1 = "=sum(" & rng.Value & "!" & Lsumcolumn & ":" & Lsumcolumn & ")"

完整的代码。

Sub Report()

Dim rng As Range
Dim LCounter As Long
Dim sLsheet As String
Dim Lsumcolumn As Long
Set gRwksconfigeration = Sheets("Config")
Set gRnct_Funds_2 = gRwksconfigeration.Range(CT_Funds_2)

LCounter = 3

For Each rng In gRnct_Funds_2

    Sheets(gcsReportSheetName).Cells(LCounter, 1) = rng.Value

    If rng.Value = "" Then

            Exit Sub

        Else

            Sheets(gcsReportSheetName).Cells(LCounter, 2) = Sheets(rng.Value).Cells.Find(What:="Value", After:=Cells(1, 1), LookIn:=xlValues, LookAt:= _
                    xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
                    False, SearchFormat:=False).End(xlDown).Value
            Lsumcolumn = Sheets(rng.Value).Cells.Find(What:="illiquid check", After:=Cells(1, 1), LookIn:=xlValues, LookAt:= _
                    xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
                    False, SearchFormat:=False).Column
            Sheets(gcsReportSheetName).Cells(LCounter, 3).FormulaR1C1 = "=sum(" & rng.Value & "!" & Lsumcolumn & ":" & Lsumcolumn & ")"


            LCounter = LCounter + 1

    End If

Next

End Sub
4

1 回答 1

2

您的工作表名称 ( rng.Value) 是否包含空格?如果是这样,您需要在名称周围添加单引号,如下所示:

='Test Sheet'!A:A

此外,您使用的是 R1C1 表示法,它与 A1 表示法略有不同(请参阅此处的更多信息)。以下两个示例都应该有效。


您使用 R1C1 表示法的代码应如下所示:

Sheets(gcsReportSheetName).Cells(LCounter, 3).FormulaR1C1 = "=sum('" & rng.Value & "'!C" & Lsumcolumn & ")"

您使用 A1 表示法的代码应如下所示:

sSumColumn = Choose(Lsumcolumn,"A","B","C","D","E","F","G") ' Expand this to include all column headers.

Sheets(gcsReportSheetName).Cells(LCounter, 3).Formula = "=sum('" & rng.Value & "'!" & sSumColumn & ":" & sSumColumn & ")"

即使您的工作表名称不包含空格,此语法仍应有效。


Choose关于获取列标题的功能的额外说明...

如果您正在查看大量列,写出来这可能会很痛苦,所以我有一个函数可以更简单地获取这个值(完全公开,我从互联网上的另一个站点抓取了这个,但是自己更新了 2007/2010/2013 版本的 Excel 工作):

Public Function ColumnLetter(ColumnNumber As Integer) As String

    If ColumnNumber > 26 Then

        If ColumnNumber > 702 Then
            ' Compatible with Excel 2007+ extension of columns
            ColumnLetter = Chr(Int((Int((ColumnNumber - 1) / 26) - 1) / 26) + 64) & _
                Chr((Int((ColumnNumber - 1) / 26) - 1) Mod 26 + 65) & _
                Chr(((ColumnNumber - 1) Mod 26) + 65)
        Else
            ' 1st character:  Subtract 1 to map the characters to 0-25,
            '                 but you don't have to remap back to 1-26
            '                 after the 'Int' operation since columns
            '                 1-26 have no prefix letter

            ' 2nd character:  Subtract 1 to map the characters to 0-25,
            '                 but then must remap back to 1-26 after
            '                 the 'Mod' operation by adding 1 back in
            '                 (included in the '65')

            ColumnLetter = Chr(Int((ColumnNumber - 1) / 26) + 64) & _
                Chr(((ColumnNumber - 1) Mod 26) + 65)
        End If
    Else
        ' Columns A-Z
        ColumnLetter = Chr(ColumnNumber + 64)
    End If
End Function

代替Choose上面的公式,您可以使用sSumColumn = ColumnLetter(Lsumcolumn).

于 2013-04-22T14:15:26.137 回答