VBA运行时错误1004“应用程序定义或对象定义的错误”

时间:2016-04-07 22:55:47

标签: excel vba excel-vba

我正在运行多个工作表来为我的会议填充数据。每次我尝试运行时都会出现内存错误,然后是' 1004'错误。我是编码的初学者,请详细说明。有什么建议吗?

    Sub PullData()

    Application.ScreenUpdating = False
    Application.EnableEvents = False

    Sheets("Production").Select

    Dim StartYear As String
    Dim StartMonth As String
    Dim StartDay As String

    Dim EndYear As String
    Dim EndMonth As String
    Dim EndDay As String

    Dim NextDay As String
    Dim StartHour As String
    Dim EndHour As String

    StartYear = Year(Range("b2").Value)
    StartMonth = Month(Range("b2").Value)
    StartDay = Day(Range("b2").Value)

    EndYear = Year(Range("b2").Value)
    EndMonth = Month(Range("b2").Value)
    EndDay = Day(Range("b2").Value)

    NextDay = EndDay + 1

    StartHour = Hour(Range("b3").Value)
    EndHour = Hour(Range("b3").Value)




     Sheets("PPA").Select
     Cells.Select
     Selection.ClearContents
     With ActiveSheet.QueryTables.Add(Connection:="URL;https://fclmportal.amazon.com/ppa/inspect/node?nodeType=FC&warehouseId=ONT8&startDateDay=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&startDateWeek=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&startDateMonth=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&maxIntradayDays=1&spanType=Intraday&startDateIntraday=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&startHourIntraday=" & StartHour & "&startMinuteIntraday=0&endDateIntraday=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&endHourIntraday=" & EndHour & "&endMinuteIntraday=0", Destination:=Range("A1"))

     Selection = 3
     Formatting = None
     PreFormattedTextToColumns = True
     ConsecutiveDelimitersAsOne = True
     SingleBlockTextImport = False
     DisableDateRecognition = False
     DisableRedirections = False


     End With

     Sheets("PPR").Select
     Cells.Select
     Selection.ClearContents
     With ActiveSheet.QuryTables.Add(Connection:="URL;https://fclm-portal.amazon.com/reports/processPathRollup?reportFormat=HTML&warehouseId=ONT8&startDateDay=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&maxIntradayDays=1&spanType=Intraday&startDateIntraday=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&startHourIntraday=" & StartHour & "&startMinuteIntraday=0&endDateIntraday=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&endHourIntraday=" & EndHour & "&endMinuteIntraday=0&_adjustPlanHours=on&_hideEmptyLineItems=on&employmentType=AllEmployees", Destination:=Range("A1"))

     Selection = 2
     Formatting = None
     PreFormattedTextToColumns = True
     ConsecutiveDelimitersAsOne = True
     SingleBlockTextImport = False
     DisableDateRecognition = False
     DisableRedirections = False

      End With


      Sheets("FR").Select
      Cells.Select
      Selection.ClearContents
      With ActiveSheet.QueryTables.Add(Connection:="URL;https://fclm-portal.amazon.com/reports/functionRollup?warehouseId=ONT8&spanType=Intraday&startDate=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "T" & StartHour & ".000&endDate=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "T" & EndHour & ".000&reportFormat=HTML&processId=01003021", Destination:=Range("A1"))

      Selection = Summary
      Formatting = None
      PreFormattedTextToColumns = True
      ConsecutiveDelimitersAsOne = True
      SingleBlockTextImport = False
      DisableDateRecognition = False
      DisableRedirections = False

      End With

      Sheets("PR").Select
      Cells.Select
      Selection.ClearContents
      With ActiveSheet.QueryTables.Add(Connection:="URL;https://fclm-portal.amazon.com/reports/functionRollup?reportFormat=HTML&warehouseId=ONT8&processId=1003032&maxIntradayDays=1&spanType=Intraday&startDateIntraday=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&startHourIntraday=" & StartHour & "&startMinuteIntraday=0&endDateIntraday=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&endHourIntraday=" & EndHour & "&endMinuteIntraday=0", Destination:=Range("A1"))

      Selection = Summary
      Formatting = None
      PreFormattedTextToColumns = True
      ConsecutiveDelimitersAsOne = True
      SingleBlockTextImport = False
      DisableDateRecognition = False
      DisableRedirections = False

      End With

      Sheets("PV").Select
      Cells.Select
      Selection.ClearContents
      With ActiveSheet.QueryTables.Add(Connection:="URL;https://fclm-portal.amazon.com/reports/functionRollup?reportFormat=HTML&warehouseId=ONT8&processId=1003018&startDateDay=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&maxIntradayDays=1&spanType=Intraday&startDateIntraday=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&startHourIntraday=" & StartHour & "&startMinuteIntraday=0&endDateIntraday=" & StartYear & "%2F" & StartMonth & "%2F" & StartDay & "&endHourIntraday=" & EndHour & "&endMinuteIntraday=0", Destination:=Range("A1"))

     Selection = Summary
     Formatting = None
     PreFormattedTextToColumns = True
     ConsecutiveDelimitersAsOne = True
     SingleBlockTextImport = False
     DisableDateRecognition = False
     DisableRedirections = False

     End With

     End Sub

1 个答案:

答案 0 :(得分:0)

With ... End With中的方法必须以.为前缀。这肯定会引发错误,因此您必须修复相当多的代码行。 并且Selection可能无法解决此问题(我这里没有Excel来测试它)。

此外,您可以方便地完全符合Range的要求。请参阅this answer,仅作为示例。这一次出现。