2013-04-22 36 views
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 

回答

2

您的表名(rng.Value)是否包含空格?如果是這樣,你需要添加單引號括起來的名稱,像這樣:

='Test Sheet'!A:A 

而且,使用的是R1C1符號,這比A1符號(see more info here)有一點不同。以下兩個示例都應該可以工作。


您與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函數來獲取列標題的一個額外的音符......

這可能是寫出來的,如果你正在尋找大量列的痛苦,所以我有一個功能,我用它來獲取該值更簡單(充分披露,我抓住這個從another site on the internet,但更新它自己的工作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)

+0

嗨,感謝您的輸入 - 但是我仍然得到了運行時錯誤1004.我不確定是否由於'Lsumcolumn'。我在找總結'U:U'的列。我的'Lsumcolumn'返回列號而不是字母 - 這會影響它嗎? – user1624926 2013-04-22 14:22:37

+0

是的,但你可以容納......更新我的答案。 – Gaffi 2013-04-22 14:41:55

+0

非常感謝您的支持。 – user1624926 2013-04-22 15:02:11