2010-09-17 27 views
1

因此,今天收到一個超過20,000個單元格的Excel電子表格。我不得不通過名稱將它們分開(有14行開始新客戶),並且總計價格列。所以,我寫道:在VB中使用Excel函數

Sub NewSpace() 
' 
' NewSpace Macro 
' 
' Keyboard Shortcut: Ctrl+p 
' 
     'create the count variable, initilize it to 17 
     Dim count As Integer 
     count = 17 

    While count <= 30003 

      'add first row 
      Rows(count & ":" & count).Select 
      Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 

      'add second row 
      Rows(count & ":" & count).Select 
      Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 

      'Select cell A in the first created row, write "Total", and format properly 
      Range("A" & count).Select 
      ActiveCell.FormulaR1C1 = "Total" 
      With ActiveCell.Characters(Start:=1, Length:=5).Font 
        .Name = "Calibri" 
        .FontStyle = "Bold" 
        .Size = 11 
        .Strikethrough = False 
        .Superscript = False 
        .Subscript = False 
        .OutlineFont = False 
        .Shadow = False 
        .Underline = xlUnderlineStyleNone 
        .ThemeColor = xlThemeColorLight1 
        .TintAndShade = 0 
        .ThemeFont = xlThemeFontMinor 
      End With 

      'Select cell H in the first created row, sum up the values 
      Range("H" & count).Select 
      ActiveCell.FormulaR1C1 = "=sum(H" & (count - 15) & ":H" & (count - 1) & ")" 
      With ActiveCell.Characters(Start:=1, Length:=5).Font 
        .Name = "Calibri" 
        .FontStyle = "Bold" 
        .Size = 11 
        .Strikethrough = False 
        .Superscript = False 
        .Subscript = False 
        .OutlineFont = False 
        .Shadow = False 
        .Underline = xlUnderlineStyleNone 
        .ThemeColor = xlThemeColorLight1 
        .TintAndShade = 0 
        .ThemeFont = xlThemeFontMinor 
      End With 


    'increment the counter by 16, start again 
    count = count + 16 

    Wend 
End Sub 

不過,我一直在這條線得到一個#NAME錯誤: ActiveCell.FormulaR1C1 = 「= SUM(H」 &(計數 - 15)& 「:H」 &(計 - 1)&「)」

一切都對我來說看起來很好,老實說我不知道​​我必須改變。

編輯:我還忘了提,在#NAME錯誤,則公式顯示正確的,但是它增加了「的功能,如下所示:= SUM(」 H __「:」 H__')

回答

3

嘗試ActiveCell.Formula代替ActiveCell.FormulaR1C1 ...

.FormulaR1C1是不同方式的尋址單元

ActiveCell.Formula = "=A1" 

ActiveCell.FormulaR1C1 = "=R1C1" 

指的是同一個單元格。

+0

完全像它應該有的那樣工作。感謝Bart的幫助。 – AgainstClint 2010-09-17 13:13:17