失败@通过正常模式运行代码

时间:2018-09-01 17:19:25

标签: excel-vba

当我在“中断模式”下运行时,我的代码可以正常运行,但是当我正常运行时,它不起作用!

为什么?!

这里是:

Sub ImportBHA()
   'Variables
    Dim Fullpath As String
    Dim oBHA As String 'Opened BHA
    Dim nRange As Integer 'Name Range
    Dim nRows As Integer 'Name Rows
    Dim SheetName As String 'Bitsheet Name
    Dim BookName As String 'Workbook Name


   'Initialization
    BookName = ActiveWorkbook.Name
    SheetName = ActiveSheet.Name

    'Application.ScreenUpdating = False

   'Dialog
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Title = "Select the BHA Report"
        .Filters.Clear
        .Filters.Add "Excel Files", "*.xls; *.xlsx", 1
        .Show

        Fullpath = .SelectedItems.Item(1)
    End With

    If InStr(Fullpath, ".xls") = 0 Then
        Exit Sub
    End If

   'Open, Tab & Copy
    Workbooks.Open Fullpath
    oBHA = ActiveWorkbook.Name
    nRange = Workbooks(oBHA).Sheets("BHAReport").Range("C900").End(xlUp).Row

   'Create Temporary Worksheet
    'Workbooks(BookName).Sheets.Add(After:=Workbooks(BookName).Sheets(Workbooks(BookName).Sheets.Count)).Name = "Temp"

       'BHA Name
        Workbooks(oBHA).Sheets("BHAReport").Range("I4").Copy
        Workbooks(BookName).Worksheets(SheetName).Range("AZ60").PasteSpecial xlPasteValues

       'Tools Names
        Workbooks(oBHA).Sheets("BHAReport").Range("D9:D" & nRange + 1).Copy
        Workbooks(BookName).Worksheets(SheetName).Range("AZ65").PasteSpecial xlPasteValues
        'nRows = Workbooks(BookName).Worksheets(SheetName).Range("AZ65:AZ138").Cells.SpecialCells(xlCellTypeConstants).Count
        For I = 65 To (65 + nRange) Step 2
            ActiveSheet.Range("C" & I) = ActiveSheet.Range("AZ" & I)
        Next I

       'Tools Serials
        Workbooks(oBHA).Sheets("BHAReport").Range("F9:F" & nRange + 1).Copy
        Workbooks(BookName).Worksheets(SheetName).Range("AZ65").PasteSpecial xlPasteValues
        For I = 65 To (65 + nRange) Step 2
            ActiveSheet.Range("J" & I) = ActiveSheet.Range("AZ" & I)
        Next I

       'Tools Max OD
        Workbooks(oBHA).Sheets("BHAReport").Range("H9:H" & nRange + 1).Copy
        Workbooks(BookName).Worksheets(SheetName).Range("AZ65").PasteSpecial xlPasteValues
        For I = 65 To (65 + nRange) Step 2
            ActiveSheet.Range("N" & I) = ActiveSheet.Range("AZ" & I)
        Next I

       'Tools OD/ID
        Workbooks(oBHA).Sheets("BHAReport").Range("G9:G" & nRange + 1).Copy
        Workbooks(BookName).Worksheets(SheetName).Range("AZ65").PasteSpecial xlPasteValues
        For I = 65 To (65 + nRange)
            ActiveSheet.Range("Q" & I) = ActiveSheet.Range("AZ" & I)
        Next I

       'Tools Gender
        Workbooks(oBHA).Sheets("BHAReport").Range("K9:K" & nRange + 1).Copy
        Workbooks(BookName).Worksheets(SheetName).Range("AZ65").PasteSpecial xlPasteValues
        For I = 65 To (65 + nRange)
            ActiveSheet.Range("S" & I) = ActiveSheet.Range("AZ" & I)
        Next I

       'Tools Connection Size
        Workbooks(oBHA).Sheets("BHAReport").Range("I9:I" & nRange + 1).Copy
        Workbooks(BookName).Worksheets(SheetName).Range("AZ65").PasteSpecial xlPasteValues
        For I = 65 To (65 + nRange)
            ActiveSheet.Range("T" & I) = ActiveSheet.Range("AZ" & I)
        Next I

       'Tools Connection Type
        Workbooks(oBHA).Sheets("BHAReport").Range("J9:J" & nRange + 1).Copy
        Workbooks(BookName).Worksheets(SheetName).Range("AZ65").PasteSpecial xlPasteValues
        For I = 65 To (65 + nRange)
            ActiveSheet.Range("W" & I) = ActiveSheet.Range("AZ" & I)
        Next I

       'Tools Cum. Lengths
        Workbooks(oBHA).Sheets("BHAReport").Range("N9:N" & nRange + 1).Copy
        Workbooks(BookName).Worksheets(SheetName).Range("AZ65").PasteSpecial xlPasteValues
        For I = 65 To (65 + nRange)
            ActiveSheet.Range("Z" & I) = ActiveSheet.Range("AZ" & I)
        Next I

       'Tools Weights to Temp
        Workbooks(oBHA).Sheets("BHAReport").Range("O9:O" & nRange + 1).Copy
        Workbooks(BookName).Worksheets(SheetName).Range("AZ65").PasteSpecial xlPasteValues
        For I = 65 To (65 + nRange)
            ActiveSheet.Range("AC" & I) = ActiveSheet.Range("AZ" & I)
        Next I

       'Close BHA Sheet
        Workbooks(oBHA).Close savechanges:=False

    'Application.ScreenUpdating = True

End Sub

虽然有点长,但是它是“导入”由斯伦贝谢专有软件在excel电子表格报告中生成的数据。

代码将报告表中的某些值复制到他自己的“底孔装配”表中!

1 个答案:

答案 0 :(得分:0)

这里是一个示例,说明了如何开始修改代码以正确地引用Workbooks和Worksheet对象。

wb =工作簿上的工作表,您将在其中运行宏
ib =导入书上的表格,您将从中导入值


在将wb设置为要用于值的工作表时,您需要更新工作表名称。现在,它设置为Destination Sheet

执行一次此操作后,其余代码只需要参考wbib。因此,在引用ib BHARReport工作表上的范围时,只需使用ib.Range("...."),类似地,您的主要书籍wb.Range("....")


对于特定示例,您可以减少以下内容:

Workbooks(oBHA).Sheets("BHAReport").Range("D9:D" & nRange + 1).Copy
Workbooks(BookName).Worksheets(SheetName).Range("AZ65").PasteSpecial xlPasteValues

收件人:

ib.Range("D9:D" & LRow).Copy
wb.Range("AZ65").PasteSpecial xlPasteValues

产生明显的代码更容易遵循。这也使调试代码非常容易,因为您实际上可以跟踪正在发生的事情。


Option Explicit

Sub ImportBHA()

Dim wb As Worksheet, ib As Worksheet
Dim LRow As Long, Fullpath as String

With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    .Title = "Select the BHA Report"
    .Filters.Clear
    .Filters.Add "Excel Files", "*.xls; *.xlsx", 1
    .Show
    Fullpath = .SelectedItems.Item(1)
    If .SelectedItems.Count < 1 Then Exit Sub
End With

'Make sure the sheet names are correct here
Set wb = ThisWorkbook.Sheets("Destination Sheet")
Set ib = Workbooks.Open(Fullpath).Sheets("BHARReport")

LRow = ib.Range("C" & ib.Rows.Count).End(xlUp).Offset(1).Row

wb.Range("Az60").Value = ib.Range("I4").Value

ib.Range("D9:D" & LRow).Copy
wb.Range("AZ65").PasteSpecial xlPasteValues