无效的过程调用或参数 - VBA,pivot,宏工作簿

时间:2015-11-25 09:52:30

标签: excel vba excel-vba pivot

一个非常令人费解的。

在我的第一个子程序中,我做了一些计算,然后我调用了第二个子程序,它从数据中转出一个数据。当我从我的个人工作簿或本地宏工作簿运行此宏时,一切运行正常 - 但是当我将工作簿转移到共享目录时,打开它并从那里运行我得到无效的过程调用错误。当我的同事从宏工作簿运行宏时,无论是来自共享目录还是来自它的本地副本,都会发生同样的事情。

我也试过添加引号''但这没有帮助。我真的很困惑为什么只能在我的电脑上工作。 我认为错误弹出 - 调用createPivot

以下是代码:

Public Sub RADreport()

Dim startNum As Long
Dim endNum As Long
Dim i As Long

Columns(7).Insert
Columns(7).Insert

endNum = Application.CountA(Columns(Cells(1, 1).Column))

Cells(1, 6).value = "Week"
Cells(1, 7).value = "DaysDiff"
Cells(1, 8).value = "On time"

'first loop that checks calculates the week

For i = 2 To endNum

If IsEmpty(Cells(i, 4).value) Then
Cells(i, 6).value = "N/A"

Else
Cells(i, 6).value = WorksheetFunction.WeekNum(Cells(i, 4).value)
End If

Next i

' end of first loop

' second loop that computes the difference in days
' and determines if rad is on time or not

For i = 2 To endNum

If IsEmpty(Cells(i, 4).value) Then ' if block for empty values
Cells(i, 7).value = "N/A"
Cells(i, 8).value = "N/A"
Else
Cells(i, 7).value = WorksheetFunction.NetworkDays(Cells(i, 4).value, Cells(i, 2).value)

If Cells(i, 7).value < 3 Then ' inner if block that determines if rad is on time

Cells(i, 8).value = "Yes"

Else
Cells(i, 8).value = "No"
End If ' end of inner block

End If 'end of if block for empty values

Next i

Call CreatePivot


End Sub

createPivot sub:

Sub CreatePivot()

Dim ws As Worksheet
Dim pc As PivotCache
Dim pt As PivotTable

'Adding new worksheet
Set ws = Worksheets.Add

'Creating Pivot cache
Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="rad!$A:$H", Version:=xlPivotTableVersion14)

'Creating Pivot table
Set pt = pc.CreatePivotTable(ws.Range("A1"))

'Setting Fields
With pt
    'set row field
    With .PivotFields("On time")
    .Orientation = xlRowField
    .Position = 1
    End With

    'set column field
    With .PivotFields("Week")
    .Orientation = xlColumnField
    .Position = 1
    End With

    'set data field
    .AddDataField .PivotFields("radId"), "Count of RadId", xlCount
End With

'calculating percentages

Dim startNum As Long
Dim endNum As Long
Dim i As Long

i = 2

For i = 2 To 50

endNum = i
If IsEmpty(Cells(4, i).value) Then Exit For


Next i


For i = 2 To endNum

Cells(8, i).value = Cells(5, i).value / Cells(7, i).value * 100

Next i


End Sub

0 个答案:

没有答案