运行时错误' 1004'方法'行'对象' _Global'失败

时间:2017-12-06 13:30:16

标签: excel vba excel-vba

我遇到了一些VBA代码的问题。我一直在

  Run-time error '1004'  Method 'Rows' of object '_Global' failed 

我已经运行了这个工具几个月了,直到今天早上都没有问题。发生错误的代码如下,在错误点评论。

 Function ExcelProcess()
 'Variables to refer to Excel and Objects
 Dim MySheetPath As String
 Dim Xl As Excel.Application
 Dim XlBook As Excel.Workbook
 Dim XlSheet As Excel.Worksheet
 Dim MyFile As Variant
 Dim MySheet As Variant
 Dim wBook As Variant
 Dim wSheet As Variant
 Dim wDate As Variant

 Dim rng As Range
 Dim cel As Range


 MyFile = Array("w1.xlsx", "w2.xlsx", "w3.xlsx")
 MySheet = Array("APPR_IND", "SLG_IND", "C2_IND", "C3_IND", "C4_IND", "T2_IND", "T3_IND", "T4_IND", "SLG_APPR_IND")

 For Each wBook In MyFile
     ' Tell it location of actual Excel file
     MySheetPath = "\\fs1\Training\CSC_Training_Ops\Training Only\Buzzard\Pulled Data\" & wBook

     'Open Excel and the workbook
     'Set Xl = CreateObject("Excel.Application")
     Set XlBook = GetObject(MySheetPath)

     'Make sure excel is visible on the screen
     'Xl.Visible = True
     XlBook.Windows(1).Visible = True

     'Windows(wBook).Activate

     For Each wSheet In MySheet
         'Define the sheet in the Workbook as XlSheet
         Set XlSheet = XlBook.Worksheets(wSheet)
         wDate = Mid(XlSheet.Range("B4").Value, 13, Len(XlSheet.Range("B4").Value))

         XlSheet.Range("A15").FormulaR1C1 = "WE_Date"
         If XlSheet.Range("A16").Value <> "No data found" Then
             Set rng = XlSheet.Range(XlSheet.Range("A16"), XlSheet.Range("A16").End(xlDown).Offset(-1))

             For Each cel In rng.Cells
                 With cel
                     .FormulaR1C1 = wDate
                     .NumberFormat = "m/d/yyyy"
                 End With
             Next cel
         End If
         XlSheet.Rows("1:14").Delete Shift:=xlUp
         XlSheet.Range("A1").End(xlDown).EntireRow.Delete Shift:=xlUp
         Rows("1:1").Select
         Selection.Replace What:="act", Replacement:="aht", LookAt:=xlPart, _
         SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
         ReplaceFormat:=False
     Next
     XlBook.Close SaveChanges:=True
 Next

 'Clean up and end with worksheet visible on the screen
 Set Xl = Nothing
 Set XlBook = Nothing
 Set XlSheet = Nothing
 End Function

0 个答案:

没有答案