2016-11-16 40 views
0

編輯2:此問題已得到解決。我犯了一個錯誤,聲明一個名稱爲'Year'的變量...在VBA中調用Year()函數時出現預期的數組錯誤

我試圖讓一個單元格包含我提供的一系列報告中的版權,並且由於版權隨年份發生變化,想要調用年份函數給我當年,所以我不必每年一月更新宏。我有問題的代碼如下所示:

With .Cells(5, 1) 
      .Value = Chr(169) & " " & Year(Date) & " NCH Marketing Services, Inc" 
      .Font.Bold = False 
      .Font.Name = "Arial" 
      .Font.Size = 11 
     End With 

當我執行這個代碼,我收到錯誤消息「編譯錯誤:預期陣」。奇怪的是,我試圖在三個不同的場合運行這個宏,它沒有編譯第一次或第三次,但第二次,它運行得很好。我沒有對代碼進行任何編輯,並且我已經在線查看年度函數的語法/用法,並且我無法弄清楚爲什麼有時會這樣。有沒有一種方法可以以更可靠的方式實現這一點?

編輯:下面是完整的代碼

Option Explicit 
Sub RGA_Format_Reports() 

Application.ScreenUpdating = False 

Dim Year As Integer 
Dim Quarter As Integer 

Dim FolderPath As String 
Dim FileName As String 
Dim WorkBk As Workbook 

Dim iRows As Integer 
Dim iCols As Integer 

Dim shpTitle As Shape 
Dim shpLogo As Shape 


'Quarter = InputBox("Please enter the quarter number for which the reports are being run.") 

'Year = InputBox("Please enter the year for which the reports are being run.") 

'FolderPath = "G:\Analytical Services\Internal Client Requests\NRS\Scheduled\" & Year & "\Quarterly RGA Store Alert Reports\" & Year & " Q" & Quarter & "\" 

'FolderPath = "G:\Analytical Services\General Team Folders\Kyle\Macro Tests\RGA Reports\" 

'FileName = Dir(FolderPath & "*.xl*") 

'DoWhile FileName <> "" 

    'Set WorkBk = Workbooks.Open(FolderPath & FileName) 
     'With WorkBk 

With ActiveWorkbook 

     With .ActiveSheet 

      iRows = .UsedRange.Rows.Count 
      iCols = .UsedRange.Columns.Count 

      .Rows.AutoFit 
      .Columns.AutoFit 

      .Columns(1).ColumnWidth = 30 


      ActiveWindow.FreezePanes = False 
      .Range("A9").Select 
      ActiveWindow.FreezePanes = True 

      With .Range(.Cells(iRows - 2, 1), .Cells(iRows, 1)) 
       .WrapText = False 
       .Font.Name = "Arial" 
       .Font.Size = 10 
      End With 


      With .PageSetup 
       .TopMargin = Application.InchesToPoints(0.25) 
       .BottomMargin = Application.InchesToPoints(0.25) 
       .LeftMargin = Application.InchesToPoints(0.25) 
       .RightMargin = Application.InchesToPoints(0.25) 
       .CenterHorizontally = True 
       .Zoom = False 
       .Orientation = xlLandscape 
       .FitToPagesWide = 1 
       .FitToPagesTall = False 
       .PrintTitleRows = "$1:$8" 
      End With 


      .Range(.Cells(9, 3), .Cells(iRows, 3)).HorizontalAlignment = xlRight 
      .Range(.Cells(iRows - 4, 1), .Cells(iRows - 4, iCols)).Font.Bold = True 
      .Range(.Cells(iRows - 4, 1), .Cells(iRows - 4, iCols)).Interior.Color = RGB(238, 236, 225) 


      With .Range(.Cells(7, 9), .Cells(7, 12)) 
       .Merge 
       .HorizontalAlignment = xlCenter 
       .Font.Bold = True 
       .Font.Name = "Arial" 
       .Font.Size = 10 
       .Interior.Color = RGB(238, 236, 225) 

       With .Borders(xlEdgeTop) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeBottom) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeLeft) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeRight) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       .Value = "Total Billed" 
      End With 


      With .Range(.Cells(7, 13), .Cells(7, 19)) 
       .Merge 
       .HorizontalAlignment = xlCenter 
       .Font.Bold = True 
       .Font.Name = "Arial" 
       .Font.Size = 10 
       .Interior.Color = RGB(238, 236, 225) 

       With .Borders(xlEdgeTop) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeBottom) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeLeft) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeRight) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       .Value = "Total Adjustments" 
      End With 


      With .Range(.Cells(8, 1), .Cells(iRows - 4, 8)) 
       With .Borders(xlEdgeTop) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeBottom) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeLeft) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeRight) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 
      End With 


      With .Range(.Cells(7, 9), .Cells(iRows - 4, 12)) 
       With .Borders(xlEdgeTop) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeBottom) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeLeft) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeRight) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 
      End With 


      With .Range(.Cells(7, 13), .Cells(iRows - 4, 19)) 
       With .Borders(xlEdgeTop) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeBottom) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeLeft) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

       With .Borders(xlEdgeRight) 
        .LineStyle = xlContinuous 
        .Weight = xlThick 
       End With 

      End With 

      If .Cells(9, 4) = 0 Then 
       Columns("D:E").Delete 
      End If 

      For iRows = 1 To 4 
       .Cells(iRows, 1).Font.Bold = True 
       .Cells(iRows, 1).Font.Name = "Arial" 
       If iRows = 1 Then 
        .Cells(iRows, 1).Font.Size = 14 
       Else 
        .Cells(iRows, 1).Font.Size = 12 
       End If 
      Next iRows 

      With .Cells(5, 1) 
       .Value = Chr(169) & " " & Year(Date) & " NCH Marketing Services, Inc" 
       .Font.Bold = False 
       .Font.Name = "Arial" 
       .Font.Size = 11 
      End With 

      .Columns("A").ColumnWidth = 200 

      .Range(Cells(1, 1), Cells(5, 1)).WrapText = False 

      .Range(Cells(1, 1), Cells(5, 1)).Copy 
      .Range("B1").Select 
      Application.Wait (Now + TimeValue("00:00:01")) 
      .Pictures.Paste.Select 
      .Range(Cells(1, 1), Cells(5, 1)).ClearContents 
      .Columns("A").AutoFit 
      .Range("A1").Select 

      Set shpTitle = .Shapes("Picture 1") 
      With shpTitle 
       .Name = "Title Picture" 
       .Placement = xlFreeFloating 
      End With 

      Set shpLogo = .Shapes.AddPicture("G:\Analytical Services\AS Tools\AS Templates\NCH Logo.png", False, True, 1, 1, 60, 67) 
      With shpLogo 
       .Name = "Logo Picture" 
       .Placement = xlFreeFloating 
      End With 

      With shpTitle 
       .Left = 67 
      End With 


      .Columns("A").ColumnWidth = 30 



     End With 

End With 


End Sub 
+0

對「帶有.Cells(5,1)」的引用是什麼?代碼中的前一個'With'是什麼?否則,如果您使用'With With Sheets(「Sheet1」)。Cells(5,1)',例如,它工作正常 –

+0

整個鏈是: 'With ActiveWorkbook With .ActiveSheet With .Cells(5,1 )' –

+0

我很懷疑你沒有對代碼做任何無意的更改。編譯錯誤不會動態改變。如果您提供更多的上下文,這將有所幫助答案几乎肯定存在於您未顯示的代碼中。 –

回答

1

@ShaiRado指出,我已經設置一個變量,「年份」的名稱。我知道,聲明與函數名稱相同的變量是一個很大的不,不;我只是精神錯亂。更改變量聲明解決了我的問題。謝謝@ShaiRado。

相關問題