VBA打开excel工作表和填充单元格时出错

VBA打开excel工作表和填充单元格时出错,excel,vba,Excel,Vba,我正在努力做到以下几点: 代码需要打开工作表中标识的3个文件 将焦点放在选定的文件(“停止”文件)上,并将vloopkup公式插入单元格BR2 查找应该是=VLOOKUP($M2,'threeday dhl baseline.truck'!$B$1:$D$186,3,FALSE) 路线代码公式应拉至底部,并向下填充列BR 关注DHL IPT文件,标记“ORL”,并将vlookup公式插入单元格B2 查找应该是=VLOOKUP($A2,“[route file stop 5.5.21.xlsx]S

我正在努力做到以下几点:

  • 代码需要打开工作表中标识的3个文件
  • 将焦点放在选定的文件(“停止”文件)上,并将vloopkup公式插入单元格BR2
  • 查找应该是=VLOOKUP($M2,'threeday dhl baseline.truck'!$B$1:$D$186,3,FALSE)
  • 路线代码公式应拉至底部,并向下填充列BR
  • 关注DHL IPT文件,标记“ORL”,并将vlookup公式插入单元格B2
  • 查找应该是=VLOOKUP($A2,“[route file stop 5.5.21.xlsx]Sheet1'!$E$1:$BR$2897,66,FALSE),以便在停止文件中查找HWB编号,并返回同样是VLOOKUP的路线代码
  • 应保存IPT文件,删除所有公式,并将任何#N/A值替换为“”
  • 下面是我的excel宏代码,我不确定我做错了什么,希望能得到帮助。谢谢大家!

    Option Explicit
    
    Sub Update_DHL()
        'open your workbooks
        On Error GoTo ERR_WB_OPEN
        Dim wbTrk As Workbook
        Set wbTrk = Workbooks.Open(Filename:=[truckfilePath])
        
        Dim wbStp As Workbook
        Set wbStp = Workbooks.Open(Filename:=[stopfilePath])
        
        Dim wbDhl As Workbook
        Set wbDhl = Workbooks.Open(Filename:=[dhlfilePath])
        On Error GoTo 0
        
        'define the worksheet in those workbooks you want to work
        Dim wsTrk As Worksheet
        Set wsTrk = wbTrk.Worksheets("SheetName")
        
        Dim wsStp As Worksheet
        Set wsStp = wsStp.Worksheets("SheetName")
        
        Dim wsDhl As Worksheet
        Set wsDhl = wsDhl.Worksheets("SheetName")
        
        'these worksheets are worked with directly without need for .activate
        wsStp.Range("B2").Formula = "=IF(SUMIF('Route Master.xls'!$C$7:$C$65536,$A2,'Route Master.xls'!$Q$7:$Q$65536)>0,TRUE,FALSE)"
        wsStp.Range("B2").Copy
    
        wsStp.Range(wsStp.Cells(2, 2), wsStp.Cells(EndRow2, 2)).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            '
    
        'select and activate a specific workbook/worksheet
        'you do this ONLY if you want to focus it for the USER. Try to avoid doing this for VBA instead work directly with the worksheets as shown above.
        wbDhl.Activate
        wsDhl.Select
    
        Exit Sub
    ERR_WB_OPEN:
        MsgBox "One of the files could not be loaded.", vbCritical
    End Sub
    
    
    
    
    Sub browseDHLFilePath()
        On Error GoTo err
        Dim fileExplorer As FileDialog
        Set fileExplorer = Application.FileDialog(msoFileDialogFilePicker)
        
        'To allow or disable to multi select
        fileExplorer.AllowMultiSelect = False
        
        With fileExplorer
            If .Show = -1 Then 'Any file is selected
                [dhlfilePath] = .SelectedItems.Item(1)
            Else ' else dialog is cancelled
                MsgBox "You have cancelled the dialogue"
                [dhlfilePath] = "" ' when cancelled set blank as file path.
            End If
        End With
    err:
        Exit Sub
    End Sub
    Sub browsetruckFilePath()
        On Error GoTo err
        Dim fileExplorer As FileDialog
        Set fileExplorer = Application.FileDialog(msoFileDialogFilePicker)
        
        'To allow or disable to multi select
        fileExplorer.AllowMultiSelect = False
        
        With fileExplorer
            If .Show = -1 Then 'Any file is selected
                [truckfilePath] = .SelectedItems.Item(1)
            Else ' else dialog is cancelled
                MsgBox "You have cancelled the dialogue"
                [truckfilePath] = "" ' when cancelled set blank as file path.
            End If
        End With
    err:
        Exit Sub
    End Sub
    Sub browsestopFilePath()
        On Error GoTo err
        Dim fileExplorer As FileDialog
        Set fileExplorer = Application.FileDialog(msoFileDialogFilePicker)
        
        'To allow or disable to multi select
        fileExplorer.AllowMultiSelect = False
        
        With fileExplorer
            If .Show = -1 Then 'Any file is selected
                [stopfilePath] = .SelectedItems.Item(1)
            Else ' else dialog is cancelled
                MsgBox "You have cancelled the dialogue"
                [stopfilePath] = "" ' when cancelled set blank as file path.
            End If
        End With
    err:
        Exit Sub
    End Sub
    

    这个Q和你说的错误有什么关系,但是错误是什么?我看到了你想要的东西的清单,但问题是什么?哦,对不起。我在尝试运行此操作时遇到一个未定义的错误,并且该错误不会继续。不知道错误在哪里@ChristoferWeber@chrisneilsen当时我把结构拆了,但现在我不确定是怎么回事