VBA故障返回并激活原始工作簿

时间:2018-08-03 23:07:13

标签: excel vba excel-vba

VBA菜鸟。用于搜索输入数据,匹配条件,进入现有电子表格,复制到最新行,然后返回到原始输入工作簿以重复该脚本。

我可以使所有东西都使用绝对路径(例如“ Book1.csv”)工作,但是每次使用新的输入数据工作簿运行宏时,都必须替换它们。可以肯定的是,我已经弄清楚了暗淡/设置部分,但是每次我调用Windows()时,激活总是使下标超出范围。代码:

Dim wb1 As Workbook
Set wb1 = ThisWorkbook

' Search Title column (B) for match
     Cells.Find(What:="A154L-T031-#1590", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate

' Select date and copy
    ActiveCell.Offset(0, 1).Range("A1:Q1").Copy

' Activate and select correct sheet in NCRP spreadsheet and then select row after latest data
Windows("Approach_North.xlsx").Activate
Worksheets("T031").Activate
Range("T1048576").End(xlUp).Select

' Select row below it and paste
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste

' To repeat have to go back to the original data sheet
Windows(wb1).Activate
ActiveCell.Select
Selection.Font.Bold = True

调试器登陆Windows(wb1)。激活。

有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

无需选择/激活:

    Dim f As Range
    Set f = Cells.Find(What:="A154L-T031-#1590", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

    If Not f Is Nothing Then

       f.Copy Workbooks("Approach_North.xlsx").Worksheets("T031").Cells(Rows.Count, "T").End(xlUp).Offset(1, 0)
       f.Bold = True

    End If