在VBA中调用Year()函数时应出现数组错误
编辑2:此问题已解决。我犯了一个错误,声明了一个名为“Year”的变量 我正试图让一个单元格在我提交的一系列报告中包含版权,由于版权随着年份的变化而变化,我想调用year函数给出当前年份,这样我就不必每年1月更新宏。我的问题代码如下所示:在VBA中调用Year()函数时应出现数组错误,vba,excel,Vba,Excel,编辑2:此问题已解决。我犯了一个错误,声明了一个名为“Year”的变量 我正试图让一个单元格在我提交的一系列报告中包含版权,由于版权随着年份的变化而变化,我想调用year函数给出当前年份,这样我就不必每年1月更新宏。我的问题代码如下所示: With .Cells(5, 1) .Value = Chr(169) & " " & Year(Date) & " NCH Marketing Services, Inc" .Font.
With .Cells(5, 1)
.Value = Chr(169) & " " & Year(Date) & " NCH Marketing Services, Inc"
.Font.Bold = False
.Font.Name = "Arial"
.Font.Size = 11
End With
当我执行这段代码时,我得到错误消息“compileerror:Expected Array”。奇怪的是,我试过三次运行这个宏,第一次或第三次都没有编译,但第二次运行得很好。我没有对代码进行任何编辑,我在网上查找了Year函数的语法/用法,我无法理解为什么有时会这样。有没有一种方法可以更可靠地实现这一点
编辑:下面是完整的代码
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
选项显式
次级RGA_格式_报告()
Application.ScreenUpdating=False
将年份设置为整数
将四分之一设置为整数
将FolderPath设置为字符串
将文件名设置为字符串
将WorkBk设置为工作簿
Dim iRows作为整数
作为整数的Dim-icol
将标题设置为形状
将徽标视为形状
'Quarter=InputBox(“请输入正在运行报告的季度号”。)
'Year=InputBox(“请输入运行报告的年份。”)
'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*”)
“原始文件名”
'Set WorkBk=Workbooks.Open(FolderPath&FileName)
“用WorkBk
使用ActiveWorkbook
使用.ActiveSheet
iRows=.UsedRange.Rows.Count
iCols=.UsedRange.Columns.Count
.Rows.AutoFit
.Columns.AutoFit
.列(1).列宽=30
ActiveWindow.FreezePanes=False
.范围(“A9”)。选择
ActiveWindow.FreezePanes=True
带.Range(.Cells(iRows-2,1),.Cells(iRows,1))
.WrapText=False
.Font.Name=“Arial”
.Font.Size=10
以
使用.PageSetup
.TopMargin=应用程序.InchesToPoints(0.25)
.BottomMargin=应用程序.InchesToPoints(0.25)
.LeftMargin=应用程序.InchesToPoints(0.25)
.RightMargin=应用程序.InchesToPoints(0.25)
.center=True
.Zoom=False
.方向=xl
.FitToPagesWide=1
.FitToPagesTall=False
.PrintTitleRows=“$1:$8”
以
.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(238236225)
带.Range(.Cells(7,9),.Cells(7,12))
合并
.HorizontalAlignment=xlCenter
.Font.Bold=True
.Font.Name=“Arial”
.Font.Size=10
.Interior.Color=RGB(238236225)
带.边框(顶部)
.LineStyle=xlContinuous
.重量=xl厚
以
带.边框(底部)
.LineStyle=xlContinuous
.重量=xl厚
以
带.边框(左)
.LineStyle=xlContinuous
.重量=xl厚
以
带.边框(右)
.LineStyle=xlContinuous
.重量=xl厚
以
.Value=“账单总额”
以
带.Range(.Cells(7,13),.Cells(7,19))
合并
.HorizontalAlignment=xlCenter
.Font.Bold=True
.Font.Name=“Arial”
.Font.Size=10
.Interior.Color=RGB(238236225)
带.边框(顶部)
.LineStyle=xlContinuous
.重量=xl厚
以
带.边框(底部)
.LineStyle=xlContinuous
.重量=xl厚
以
带.边框(左)
.LineStyle=xlContinuous
.重量=xl厚
以
带.边框(右)
.LineStyle=xlContinuous
.重量=xl厚
以
.Value=“总调整数”
以
带.Range(.Cells(8,1),.Cells(iRows-4,8))
带.边框(顶部)
.LineStyle=xlContinuous
.重量=xl厚
以
带.边框(底部)
.LineStyle=xlContinuous
.重量=xl厚
以
带.边框(左)
.LineStyle=xlContinuous
.重量=xl厚
以
带.边框(右)
.LineStyle=xlContinuous
.重量=xl厚
以
以
带.Range(.Cells(7,9),.Cells(iRows-4,12))
带.边框(顶部)
.LineStyle=xlContinuous
.重量=xl厚
以
带.边框(底部)