使用application.on time方法时,宏不会运行

时间:2015-04-29 08:32:20

标签: excel vba

我试图在Windows和application.ontime方法上使用任务调度程序在一夜之间自动运行宏。当我自己运行宏时,它可以很好地工作,但如果通过application.ontime函数调用它,它将无法正常工作。

非常烦人,因为它使全自动化流程不能自动化。有人可以帮帮我吗?查看代码中存在问题的位置?

之前的问题是它没有将sDate变量传递给Importer Sub所以我试图改变它,但现在我得到一个参数而不是可选的错误消息。有什么想法吗?

Option Explicit
Public wb1 As Workbook
Public wb2 As Workbook
Public r As Range
Public CSVPath As String
Public sProduct As Worksheet
Public sPivot As Worksheet
Public sCSVSheet As Worksheet
Public sAccount As Worksheet
Public sButton As Worksheet
Public sMainSheet As Worksheet
Public sRawData As Worksheet
Public sFileNames As Worksheet
Public sFrontSheet As Worksheet
Public sBankHolidays As Worksheet
Public sMATrades As Worksheet
Public sMAOverview As Worksheet
Public sMarketData As Worksheet
Public sMAFXEffect As Worksheet
Public sDate As String
Public rDate As Double
Public lastRunDate As Date
Public LRow As Integer
Public LRow2 As Integer
Public ARow As Integer
Public sRange As Range
Public Abr As String
Public FixingName As String
Public FixingReport As String
Public X As Variant
Public Y As Double
Public i As Integer
Public k As Integer
Public p As Integer
Public Ccy As String
Public sYear As Long
Public sMonth As Long
Public sDay As Long
Public pt As PivotTable
Public ws As Worksheet
Public wb As Workbook
Public Text As String
Public ActualDate As Long
Public Col As Long

Sub Main()

Application.DisplayAlerts = False
Application.ScreenUpdating = False

SetSheets

sCSVSheet.Visible = True
sPivot.Visible = True
sFileNames.Visible = True
sBankHolidays.Visible = True

If sButton.Range("ManualDateYesNo").Value = "Yes" Then

sDate = sButton.Range("ManualDate").Value
rDate = sButton.Range("ManualDate").Value

DataDeleter

Do While rDate <> DateValue(Date)
If Weekday(sDate) <> 1 And Weekday(sDate) <> 7 And Not IsNumeric(Application.Match(rDate, sBankHolidays.Columns("A:A"), 0)) Then

Importer (sDate)

End If

rDate = rDate + 1
sDate = Format(DateSerial(Year(rDate), Month(rDate), Day(rDate)), "dd/MM/yyyy")
Loop

sButton.Range("ManualDateYesNo").Value = "No"

Else

If Weekday(Date) = 2 Then
sDate = Date - 3
Else
sDate = Date - 1
End If

If Weekday(Date) = 1 Or Weekday(Date) = 7 Then ThisWorkbook.Close False

LRow = sBankHolidays.Range("A1048576").End(xlUp).Row

For Each r In sBankHolidays.Range("A2:A" & LRow)
If Weekday(Date) = 2 Then
    If r + 3 = Date Then ThisWorkbook.Close False
Else
    If r + 1 = Date Then ThisWorkbook.Close False
End If
Next

DataDeleter

Importer (sDate)

End If

RefreshPivots

sMainSheet.Columns("C:C").NumberFormat = "m/d/yyyy"
sRawData.Columns("E:E").NumberFormat = "m/d/yyyy"
sProduct.Columns("C:C").NumberFormat = "m/d/yyyy"
sAccount.Columns("D:D").NumberFormat = "m/d/yyyy"

Calculate

sCSVSheet.Visible = False
sPivot.Visible = False
sFileNames.Visible = False
sBankHolidays.Visible = False
sFrontSheet.Activate

Application.ScreenUpdating = True
Application.DisplayAlerts = True

Application.Quit

End Sub

Private Sub Importer(ByRef sDate As String)

'On Error GoTo ErrorHandler

SetSheets

sYear = Year(sDate)
sMonth = Month(sDate)
sDay = Day(sDate)

lastRunDate = sPivot.Range("A1000000").End(xlUp).Value

sDate = Format(DateSerial(sYear, sMonth, sDay), "yyyyMMdd")
rDate = DateValue(Format(DateSerial(sYear, sMonth, sDay), "dd/MM/yyyy"))

LRow = sFileNames.Range("A1048576").End(xlUp).Row

For Each sRange In sFileNames.Range("A2:A" & LRow)

Abr = sRange.Offset(0, 1).Value
FixingName = sRange.Offset(0, 2).Value

FixingReport = "P:\Systemfiles\SharedDocs\" & Abr & "\Fixing Files\" & sDate & " " & FixingName & ".xls"

Workbooks.Open Filename:=FixingReport, ReadOnly:=True

SetSheets2

sMAOverview.Range("D9:D43").Copy

With sProduct
LRow = .Range("A1048576").End(xlUp).Row + 1

.Range("E" & LRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
.Range("C" & LRow).Value = rDate
.Range("B" & LRow).Value = sRange
.Activate
.Range("A" & LRow - 1).AutoFill Destination:=Range("A" & LRow - 1 & ":A" & LRow)
End With

RefreshPivots

End If

Next

LRow = sPivot.Range("A1048576").End(xlUp).Row

LRow = sMAFXEffect.Columns("B:B").Find("DBIN", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
LRow2 = sMAFXEffect.Range("B65000").End(xlUp).Row

k = LRow

For Each r In sMAFXEffect.Range("B" & LRow & ":B" & LRow2)
sMAFXEffect.Activate
sMAFXEffect.Range(Cells(k, 3), Cells(k, 11)).Copy

With sAccount
LRow = .Range("A1048576").End(xlUp).Row + 1
.Activate
.Range("E" & LRow).PasteSpecial Paste:=xlPasteValues
Ccy = .Range("M" & LRow).Value
ARow = sMAFXEffect.Columns("B:B").Find(Ccy, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
.Range("O" & LRow).Value = sMAFXEffect.Cells(ARow, 11).Value
.Range("D" & LRow).Value = rDate
.Range("C" & LRow).Value = r
.Range("Q" & LRow).Value = sRange
.Activate
.Range("A" & LRow - 1 & ":B" & LRow - 1).AutoFill Destination:=Range("A" & LRow - 1 & ":B" & LRow)
.Range("P" & LRow - 1).AutoFill Destination:=Range("P" & LRow - 1 & ":P" & LRow)
.Range("N" & LRow - 1).AutoFill Destination:=Range("N" & LRow - 1 & ":N" & LRow)
'.Range("N" & LRow).Value = .Range("E" & LRow).Value * .Range("O" & LRow).Value
k = k + 1
End With
Next

wb2.Close False

Next

For Each pt In sPivot.PivotTables
    pt.PivotCache.Refresh
Next

With sPivot
LRow = .Range("A1048576").End(xlUp).Row
rDate = .Range("A" & LRow).Value
LRow = .Range("I1048576").End(xlUp).Row
End With

For Each r In sPivot.Range("I3:I" & LRow)
With sRawData
.Activate
LRow = .Range("A1048576").End(xlUp).Row + 1
.Range("D" & LRow).Value = r
.Range("C" & LRow).Value = r.Offset(0, 1).Value
.Range("E" & LRow).Value = rDate
.Range("A" & LRow - 1).AutoFill Destination:=Range("A" & LRow - 1 & ":A" & LRow)
.Range("B" & LRow - 1).AutoFill Destination:=Range("B" & LRow - 1 & ":B" & LRow)
.Range("F" & LRow - 1 & ":CB" & LRow - 1).AutoFill Destination:=Range("F" & LRow - 1 & ":CB" & LRow)
If .Range("K" & LRow).Value = 0 Then .Range("K" & LRow).EntireRow.Delete

If .Range("J" & LRow).Value = "True" Then
Text = .Range("C" & LRow).Text & .Range("D" & LRow).Text & CDbl(.Range("F" & LRow).Value)
X = Application.Match(Text, .Columns("A:A"), 0)
.Range("S" & LRow).GoalSeek Goal:=0, ChangingCell:=Range("AK" & X)
End If

End With
Next

LRow = sPivot.Range("L1048576").End(xlUp).Row

For Each r In sPivot.Range("L3:L" & LRow)
With sMainSheet
.Activate
LRow = .Range("A1048576").End(xlUp).Row + 1
.Range("B" & LRow).Value = r
.Range("C" & LRow).Value = rDate
.Range("A" & LRow - 1).AutoFill Destination:=Range("A" & LRow - 1 & ":A" & LRow)
.Range("D" & LRow - 1 & ":BP" & LRow - 1).AutoFill Destination:=Range("D" & LRow - 1 & ":BP" & LRow)
.Range("BB" & LRow).ClearContents
.Range("BB" & LRow).Value = .Range("AW" & LRow).Value
End With
Next

sButton.Range("LastRun").Value = rDate

Exit Sub

ErrorHandler:

For Each wb In Workbooks
If wb.Name <> ThisWorkbook.Name Then wb.Close False
Next

MsgBox Err.Description & Err.Source

End Sub

Public Sub DataDeleter()

For Each ws In wb1.Worksheets
On Error Resume Next
If ws.Name = "Main Sheet" Or ws.Name = "Raw Data" Or ws.Name = "Product" Or ws.Name = "Account" Then
Col = Sheets(ws.Name).Rows("1:1").Find("Date", lookat:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
X = Application.Match(rDate, Sheets(ws.Name).Columns(Col), 0)
LRow = Sheets(ws.Name).Range("A1048576").End(xlUp).Row
Sheets(ws.Name).Rows(X & ":" & LRow).EntireRow.Delete
End If
On Error GoTo 0
Next

End Sub

Public Sub RefreshPivots()

For Each ws In Worksheets
    For Each pt In ws.PivotTables
        pt.PivotCache.Refresh
    Next
Next

End Sub

Public Sub SetSheets()

Set wb1 = ThisWorkbook

Set sProduct = wb1.Sheets("Product")
Set sPivot = wb1.Sheets("Pivot")
Set sCSVSheet = wb1.Sheets("CSVSheet")
Set sAccount = wb1.Sheets("Account")
Set sButton = wb1.Sheets("Button Sheet")
Set sRawData = wb1.Sheets("Raw Data")
Set sMainSheet = wb1.Sheets("Main Sheet")
Set sFileNames = wb1.Sheets("File Names")
Set sFrontSheet = wb1.Sheets("Front Sheet")
Set sBankHolidays = wb1.Sheets("Bank Holidays")

End Sub
Public Sub SetSheets2()

Set wb2 = ActiveWorkbook

Set sMATrades = wb2.Sheets("MA Trades")
Set sMAOverview = wb2.Sheets("MA Overview")
Set sMarketData = wb2.Sheets("Market Data")
Set sMAFXEffect = wb2.Sheets("MA FX Effect")

End Sub

1 个答案:

答案 0 :(得分:0)

在重新设计我的代码时,我没有在打开工作簿以调用另一个宏时更改application.ontime。我更改了它调用的宏,现在它可以正常工作