Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 一天中第一次运行宏时宏中断_Excel_Vba - Fatal编程技术网

Excel 一天中第一次运行宏时宏中断

Excel 一天中第一次运行宏时宏中断,excel,vba,Excel,Vba,当我一天中第一次运行宏时,我似乎遇到了一个问题,当宏将另一个工作表添加到文件中时,宏失败 弹出的错误是: “运行时错误1004:已使用该名称。请尝试其他名称。” 一个。” 宏的目标是: 在\Downloads中查找该文件 转换从.xls->.xlsx找到的保存文件 删除原始下载文件 运行一系列格式化以进行打印 当它失败时,要修复它,我必须关闭当前文件,重新下载该文件,用所需的格式和名称手动保存该文件,删除下载,再次重新下载该文件,运行宏。这样做之后,它就像一个符咒。一旦我这样做了,我就可以重新下

当我一天中第一次运行宏时,我似乎遇到了一个问题,当宏将另一个工作表添加到文件中时,宏失败

弹出的错误是:

“运行时错误
1004
:已使用该名称。请尝试其他名称。” 一个。”

宏的目标是:

  • 在\Downloads中查找该文件
  • 转换从.xls->.xlsx找到的保存文件
  • 删除原始下载文件
  • 运行一系列格式化以进行打印
  • 当它失败时,要修复它,我必须关闭当前文件,重新下载该文件,用所需的格式和名称手动保存该文件,删除下载,再次重新下载该文件,运行宏。这样做之后,它就像一个符咒。一旦我这样做了,我就可以重新下载文件并运行宏而不会出现故障

    这是我目前的代码

    它打断的行是:
    Sheets.Add(在:=Sheets(“Dock Activity Report”)之后)。Name=“Cases”

    子明细表_宏()
    Dim文件名、路径名、将文件名另存为字符串
    将wb设置为工作簿
    将用户名设置为字符串
    用户名=环境(“用户名”)
    Pathname=“C:\Users\”和Environ$(“username”)和“\Downloads\”
    Filename=Dir(路径名和“Dock\u Activity.*.xls”)
    SaveFileName=Dir(路径名和“dockactivity.xlsx”)
    Application.DisplayAlerts=False
    如果Len(Dir(路径名和“Dock\u Activity.*.xls”)>0,则
    Debug.Print“找到文件名,正在运行宏”
    其他的
    MsgBox“您需要从精益中的”&vbNewLine&“报告运行日志”下载”&vbNewLine&“停靠活动报告”&vbNewLine&vbNewLine&“下载后,请重新运行宏”、vbCritical、“HiRise调度宏”
    Debug.Print“在给定路径名中找不到文件名”
    调试。打印“退出宏”
    出口接头
    如果结束
    文件名“”时执行此操作
    设置wb=Workbooks.Open(路径名和文件名)
    wb.CheckCompatibility=True
    Application.DisplayAlerts=False
    wb.SaveAs文件名:=“dockactivity”,文件格式:=xlOpenXMLWorkbook
    wb.Close SaveChanges:=False
    Filename=Dir(路径名和“Dock\u Activity.*.xls”)
    Filename=Dir()
    环
    Application.DisplayAlerts=True
    如果Dir(路径名和“Dock\u Activity.*.xls”)“”则
    kill(路径名和“Dock\u Activity.*.xls”)
    如果结束
    Debug.Print“在给定路径名内查找保存文件名”
    设置wb=Workbooks.Open(路径名和“dockactivity.xlsx”)
    调试.打印“找到保存文件名,打开文件”
    Windows(“dockactivity.xlsx”)。激活
    行(“1:21”)。删除移位:=xlUp
    范围(“A:B,D:F,H:N,S:S,U:V,X:Y,AB:AK,AM:BA”)。删除移位:=xlToLeft
    柱(“H:H”)。切割
    列(“A:A”)。插入移位:=xlToRight
    列(“K:K”)。剪切
    列(“G:G”)。插入移位:=xlToRight
    列(“J:K”)。副本
    范围(“L1”)。选择
    活动表。粘贴
    Application.CutCopyMode=False
    列(“J:K”)。ClearContents
    范围(“J1”)。公式1c1=“拖车编号”
    范围(“K1”)。公式1c1=“到达时间”
    列(“G:M”)。副本
    范围(“N1”)。选择
    活动表。粘贴
    Application.CutCopyMode=False
    选择.ClearContents
    范围(“N1”)。公式1c1=“门”
    范围(“O1”)。公式1c1=“船轨”
    范围(“P1”)。公式1c1=“分阶段”
    范围(“Q1”)。公式1c1=“检查是否加载”
    范围(“R1”)。公式1c1=“案例选择”
    范围(“S1”)。公式1c1=“层拾取”
    范围(“T1”)。公式1c1=“检查是否由池释放”
    调试。打印“1:1表格标题完成”
    列(“A:B”)。列宽=17.71
    列(“C:C”)。列宽=19.14
    列(“D:D”)。列宽=25.71
    列(“E:E”)。列宽=14.41
    列(“F:F”)。列宽=10.71
    列(“G:G”)。列宽=30.29
    列(“H:H”)。列宽=9.43
    列(“I:I”)。列宽=13.71
    列(“J:J”)。列宽=26.14
    列(“K:L”)。列宽=23.57
    列(“M:M”)。列宽=46
    列(“N:S”)。列宽=15
    列(“T:T”)。列宽=12.86
    调试。打印“列大小调整完成”
    单元格。选择
    使用Selection.Font
    .Name=“Arial”
    .尺寸=14
    .Strikethrough=False
    .Superscript=False
    .Subscript=False
    .OutlineFont=False
    .Shadow=False
    .Underline=xlUnderlineStyleNone
    .TintAndShade=0
    .ThemeFont=xlThemeFontNone
    以
    行(“1:1”)。行高=75
    行(“2:150”)。行高=55
    ' #############################################################################    
    Sheets.Add(在:=Sheets(“停靠活动报告”)之后)。Name=“案例”
    Sheets.Add(在:=Sheets(“Cases”)之后)。Name=“Layers”
    ' #############################################################################    
    表格(“码头活动报告”)。范围(“R2:R150”)。公式R1C1=“=VLOOKUP(RC[-17],案例!C[-13]:C[-12],2,假)”
    表格(“码头活动报告”)。范围(“S2:S150”)。公式1C1=“=VLOOKUP(RC[-18],层!C[-15]:C[-14],2,假)”
    工作表(“码头活动报告”)。选择
    范围(“A2:T150”)。选择
    Selection.FormatConditions.Add类型:=xlExpression,公式1:=_
    “=$C2=”“直播预告片”“”
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    带选择。格式条件(1)。内部
    .PatternColorIndex=xlAutomatic
    .Color=65535
    .TintAndShade=0
    以
    范围(“B2:B150”)。选择
    ActiveWorkbook.Worksheets(“停靠活动报告”).Sort.SortFields.Clear
    ActiveWorkbook.Worksheets(“停靠活动报告”).Sort.SortFields.Add Key:=_
    范围(“B2”),排序:=xlSortOnValues,顺序:=xlAscending,数据选项:=_
    xlSortNormal
    使用ActiveWorkbook.Worksheets(“停靠活动报告”).Sort
    .SetRange范围(“A1:T150”)
    .Header=xlGuess
    .MatchCase=False
    .方向=xlTopToBottom
    .SortMethod=xl拼音
    .申请
    以
    活动工作簿。工作表(“停靠活动报告”)。选择
    列(“A:A”).C
    
    Sub Schedule_macro()
    
    Dim Filename, Pathname, SaveFileName As String
    Dim wb As Workbook
    Dim UserName As String
    
        UserName = Environ("username")
        Pathname = "C:\Users\" & Environ$("username") & "\Downloads\"
        Filename = Dir(Pathname & "Dock_Activity_*.xls")
        SaveFileName = Dir(Pathname & "dockactivity.xlsx")
    
        Application.DisplayAlerts = False
    If Len(Dir(Pathname & "Dock_Activity_*.xls")) > 0 Then
        Debug.Print "Filename found, running macro"
    Else
        MsgBox "You need to download the" & vbNewLine & "Dock Activity Report from the" & vbNewLine & "'Report Run Log' in Lean." & vbNewLine & vbNewLine & "Once downloaded, please rerun the macro", vbCritical, "HiRise Schedule Macro"
        Debug.Print "could not find Filename within given Pathname"
        Debug.Print "exiting macro"
        Exit Sub
    End If
    
      Do While Filename <> ""
        Set wb = Workbooks.Open(Pathname & Filename)
        wb.CheckCompatibility = True
    
        Application.DisplayAlerts = False
        wb.SaveAs Filename:="dockactivity", FileFormat:=xlOpenXMLWorkbook
        wb.Close SaveChanges:=False
    
        Filename = Dir(Pathname & "Dock_Activity_*.xls")
        Filename = Dir()
    
      Loop
      Application.DisplayAlerts = True
    
        If Dir(Pathname & "Dock_Activity_*.xls") <> "" Then
            kill (Pathname & "Dock_Activity_*.xls")
    
        End If
    Debug.Print "looking for SaveFileName within given Pathname"
    Set wb = Workbooks.Open(Pathname & "dockactivity.xlsx")
    Debug.Print "SaveFileName found, opening file"
    
    Windows("dockactivity.xlsx").Activate
        Rows("1:21").Delete Shift:=xlUp
        Range("A:B,D:F,H:N,S:S,U:V,X:Y,AB:AK,AM:BA").Delete Shift:=xlToLeft
        Columns("H:H").Cut
        Columns("A:A").Insert Shift:=xlToRight
        Columns("K:K").Cut
        Columns("G:G").Insert Shift:=xlToRight
        Columns("J:K").Copy
        Range("L1").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Columns("J:K").ClearContents
        Range("J1").FormulaR1C1 = "Trailer Number"
        Range("K1").FormulaR1C1 = "Arrival Time"
        Columns("G:M").Copy
        Range("N1").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Selection.ClearContents
        Range("N1").FormulaR1C1 = "Door"
        Range("O1").FormulaR1C1 = "Ship Rail"
        Range("P1").FormulaR1C1 = "Staged"
        Range("Q1").FormulaR1C1 = "Check If Loaded"
        Range("R1").FormulaR1C1 = "Case Picks"
        Range("S1").FormulaR1C1 = "Layer Picks"
        Range("T1").FormulaR1C1 = "Check if Released by Pool"
        Debug.Print "1:1 table headers complete"
        Columns("A:B").ColumnWidth = 17.71
        Columns("C:C").ColumnWidth = 19.14
        Columns("D:D").ColumnWidth = 25.71
        Columns("E:E").ColumnWidth = 14.41
        Columns("F:F").ColumnWidth = 10.71
        Columns("G:G").ColumnWidth = 30.29
        Columns("H:H").ColumnWidth = 9.43
        Columns("I:I").ColumnWidth = 13.71
        Columns("J:J").ColumnWidth = 26.14
        Columns("K:L").ColumnWidth = 23.57
        Columns("M:M").ColumnWidth = 46
        Columns("N:S").ColumnWidth = 15
        Columns("T:T").ColumnWidth = 12.86
        Debug.Print "column resizing complete"
        Cells.Select
        With Selection.Font
            .Name = "Arial"
            .Size = 14
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        Rows("1:1").RowHeight = 75
        Rows("2:150").RowHeight = 55
    ' #############################################################################    
        Sheets.Add(After:=Sheets("Dock Activity Report")).Name = "Cases"
        Sheets.Add(After:=Sheets("Cases")).Name = "Layers"
    ' #############################################################################    
        Sheets("Dock Activity Report").Range("R2:R150").FormulaR1C1 = "=VLOOKUP(RC[-17],Cases!C[-13]:C[-12],2,FALSE)"
        Sheets("Dock Activity Report").Range("S2:S150").FormulaR1C1 = "=VLOOKUP(RC[-18],Layers!C[-15]:C[-14],2,FALSE)"
        Worksheets("Dock Activity Report").Select
        Range("A2:T150").Select
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=$C2=""Live Trailer"""
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
        End With
            Range("B2:B150").Select
        ActiveWorkbook.Worksheets("Dock Activity Report").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Dock Activity Report").Sort.SortFields.Add Key:= _
            Range("B2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Dock Activity Report").Sort
            .SetRange Range("A1:T150")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ActiveWorkbook.Worksheets("Dock Activity Report").Select
        Columns("A:A").Copy
        Columns("B:B").Insert Shift:=xlToRight
        Range("B2").Select
        Application.CutCopyMode = False
        ActiveCell.FormulaR1C1 = "=IFERROR(RC[-1]*1,TRIM(RC[-1]))"
        Range("B3").Select
        Range("B2").Select
        Selection.AutoFill Destination:=Range("B2:B150"), Type:=xlFillDefault
        Range("B2:B150").Select
        Columns("B:B").Copy
        Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("B:B").Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlToLeft
        Debug.Print "A:A value reformat complete"
        Sheets("Dock Activity Report").Select
        Columns("A:T").Select
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=COUNTA($A1:$F1)>0"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Borders
            .LineStyle = xlContinuous
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Selection.FormatConditions(1).StopIfTrue = False
        Debug.Print "cell borders added"
    
      Dim r As Long
      Dim LastRow As Long
      LastRow = Cells(Rows.Count, "A").End(xlUp).Row
      For r = LastRow To 1 Step -1
      If Cells(r, 1) = 0 Then
      Rows(r).Delete
      End If
      Next r
        Range("A1").Select
    
        Sheets("Cases").Range("E2:E300").FormulaR1C1 = "=VALUE(TRIM(CLEAN(RC[-4])))"
        Sheets("Cases").Range("F2:F300").FormulaR1C1 = "=RC[-2]"
        Sheets("Cases").Columns("E:F").EntireColumn.Hidden = True
        Sheets("Layers").Range("D2:D300").FormulaR1C1 = "=VALUE(TRIM(CLEAN(RC[-3])))"
        Sheets("Layers").Range("E2:E300").FormulaR1C1 = "=RC[-2]"
        Sheets("Layers").Columns("D:E").EntireColumn.Hidden = True
        Sheets("Dock Activity Report").Range("A1").Select
        Application.DisplayAlerts = False
        ActiveWorkbook.Save
        Application.DisplayAlerts = True
    MsgBox "All Finished!", vbInformation, "HiRise Schedule"
    ActiveWorkbook.Save
    End Sub
    
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("Cases").Delete
    Application.DisplayAlerts = True
    On Error GoTo 0
    
    Sheets.Add(After:=Sheets("Dock Activity Report")).Name = "Cases"