ThisWorkbook.Activate上的运行时错误1004

时间:2017-05-09 18:13:13

标签: excel vba excel-vba excel-2010

在下面的代码中,它在“FORMAT TEMPLATE”评论中失败了。我得到一个运行时错误1004.我正在做的是 - 我正在关闭前一个窗口(从复制模板)并将其粘贴到好的工作簿中。我认为我没有正确地切换工作簿。

Sub BBUorders()
'OPEN TEMPLATE
Dim sPath As String, sFile As String
Dim wb As Workbook
sPath = "C:\Users\douglas.futato\Desktop\"
sFile = sPath & "BBU_CMD_TEMPLATE.xlsx"
Set wb = Workbooks.Open(sFile)

'COPY TEMPLATE PASTE IN BBU DOC
Dim tmplt As Workbook
On Error Resume Next
    Set tmplt = Workbooks("BBU_CMD_TEMPLATE.xlsx")
    If tmplt Is Nothing Then
        MsgBox "Template file needs to be open..."
        Exit Sub
    End If
On Error GoTo 0
With ThisWorkbook
    tmplt.ActiveSheet.Copy After:=.Sheets(.Sheets.Count)
End With

'CLOSE TEMPLATE
Windows("BBU_CMD_TEMPLATE.xlsx").Activate
ActiveWindow.Close False

'FORMAT TEMPLATE --------- THIS IS WHERE IS BOMBS OUT
ThisWorkbook.Activate
Worksheets(“Sheet3”).Activate
ActiveCell.Offset(-3, -8).Range("A1:H1").Select
Selection.AutoFilter
Cells.Replace What:="[BBU_CMD_TEMPLATE.xlsx]Price List", Replacement:=Sheet1.Name, LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
ActiveSheet.Range("$A$1:$H$61").AutoFilter Field:=6, Criteria1:="0"
Rows("2:100").Select
Selection.Delete Shift:=xlUp
ActiveWindow.SmallScroll Down:=-18
ActiveSheet.Range("$A$1:$H$18").AutoFilter Field:=6
ActiveSheet.Range("$A$1:$H$18").AutoFilter Field:=7, Criteria1:="0"
Selection.Delete Shift:=xlUp
ActiveSheet.Range("$A$1:$H$17").AutoFilter Field:=7
Range("E2").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Range("E2").Select
Selection.AutoFill Destination:=Range("E2:E50"), Type:=xlFillDefault
Range("E2:E21").Select
Range("I1").Select
ActiveSheet.Range("$A$1:$H$50").AutoFilter Field:=1, Criteria1:="="
Rows("8:100").Select
Selection.Delete Shift:=xlUp
Range("A2:H2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveSheet.Range("$A$1:$H$7").AutoFilter Field:=1
Range("A1").Select
End Sub

2 个答案:

答案 0 :(得分:1)

我注意到的是Worksheets(“Sheet3”).Activate
您是否尝试将其替换为Worksheets("Sheet3").Activate

ActiveCell.Offset不应包含负值 例如如果您的活动单元格为(1,1),则会出现错误,因为
-3 - 1小于1,并且不是有效行。 (与专栏相同)

答案 1 :(得分:0)

我知道这不是最干净的代码,但我想出了如何让它运行。我不得不添加工作表作为参考。当我试图在新工作表中工作时,它违反了第2页。示例:工作表("模板")。范​​围(" E45")。选择

Sub templateToBBU()
'OPEN TEMPLATE
Dim sPath As String, sFile As String
Dim wb As Workbook
sPath = "C:\Users\douglas.futato\Desktop\"
sFile = sPath & "BBU_CMD_TEMPLATE.xlsx"
Set wb = Workbooks.Open(sFile)

'COPY TEMPLATE PASTE IN BBU DOC
Dim tmplt As Workbook
On Error Resume Next
    Set tmplt = Workbooks("BBU_CMD_TEMPLATE.xlsx")
    If tmplt Is Nothing Then
        MsgBox "Template file needs to be open..."
        Exit Sub
    End If
On Error GoTo 0
With ThisWorkbook
    tmplt.ActiveSheet.Copy After:=.Sheets(.Sheets.Count)
End With

'CLOSE TEMPLATE
Windows("BBU_CMD_TEMPLATE.xlsx").Activate
ActiveWindow.Close False
End Sub
Sub Macro1()
Worksheets("TEMPLATE").Range("A1:H1").Select
Selection.AutoFilter
Worksheets("TEMPLATE").Cells.Replace What:= _
    "C:\Users\douglas.futato\Desktop\[BBU_CMD_TEMPLATE.xlsx]Price List", _
    Replacement:=Sheet1.Name, LookAt:=xlPart, SearchOrder:=xlByRows,     
MatchCase _
    :=False, SearchFormat:=False, ReplaceFormat:=False
Worksheets("TEMPLATE").Range("$A$1:$H$61").AutoFilter Field:=6, 
Criteria1:="0"
Worksheets("TEMPLATE").Rows("2:100").Select
Selection.Delete Shift:=xlUp
Worksheets("TEMPLATE").Range("$A$1:$H$18").AutoFilter Field:=6
Worksheets("TEMPLATE").Range("$A$1:$H$18").AutoFilter Field:=7, 
Criteria1:="0"
Selection.Delete Shift:=xlUp
Worksheets("TEMPLATE").Range("$A$1:$H$17").AutoFilter Field:=7
Worksheets("TEMPLATE").Range("E2").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E3").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E4").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E5").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E6").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E7").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E8").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E9").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E10").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E11").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E12").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E13").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E14").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E15").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E16").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E17").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E18").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E19").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E20").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E21").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E22").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E23").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E24").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E25").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E26").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E27").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E28").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E29").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E30").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E31").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E32").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E33").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E34").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E35").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E36").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E37").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E38").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E39").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E40").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E41").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E42").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E43").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E44").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E45").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E46").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E47").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E48").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E49").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E50").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"
Worksheets("TEMPLATE").Range("E51").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+1"

'ST and Cust Mapping HERE
Worksheets("TEMPLATE").Range("$A$1:$H$50").AutoFilter Field:=1, 
Criteria1:="="
Worksheets("TEMPLATE").Rows("8:100").Select
Selection.Delete Shift:=xlUp
Worksheets("TEMPLATE").Range("$A$1:$H$50").AutoFilter Field:=1


End Sub

Sub Main()
Call templateToBBU
Call Macro1
End Sub