重新运行代码时出错

时间:2017-10-04 09:19:51

标签: excel vba excel-vba runtime-error

首先,我直接从Excel工作簿运行代码时出错。它会导致下面提到的错误消息

  

我们查看了您选择旁边的所有数据,但没有看到为您填写值的模式。要使用Flash Fill,请输入您要查看的输出的几个示例,将活动单元格保留在要填充的列中,然后再次单击“Flash Fill”按钮

但是,如果从开发人员选项卡下的VBA窗口播放,我可以运行代码。但是在弹出错误消息1004之前仅限于1次运行,并且在再次播放时也会出现代码错误。

请帮忙。从未教过或学过VBA。下面的代码是网上研究的混搭和试验&错误。

Sub Graph()
'
' Graph Macro
'
' Keyboard Shortcut: Ctrl+e
'
'Select values in a column from specified workbook and sheet
Dim LR As Long, cell As Range, rng As Range

Windows("Area3-LG").Activate
With Sheets("Graph data")
    LR = .Range("B" & Rows.Count).End(xlUp).Row
    For Each cell In .Range("B4:B" & LR)
        If cell.Value <> "" Then
            If rng Is Nothing Then
                Set rng = cell
            Else
                Set rng = Union(rng, cell)
            End If
        End If
    Next cell
    'Error with rng.select when Macro is runned again
    rng.Select    
End With
Selection.Copy

'Open next workbook
Windows("InstData_TEMS_Existing").Activate
'Open Sheet L
Sheets("L").Select
'Select empty field fromn column AA
Range("AA" & Rows.Count).End(xlUp).Offset(1).Select
'paste selection to empty field
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
'Go back to previous workbook & delete column
Windows("Area3-LG").Activate
Sheets("Graph data").Select
Columns("B:B").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Sheets("Graph Data").Select

End Sub

提前致谢(:

1 个答案:

答案 0 :(得分:0)

尝试下面的代码,没有必要的finishedfalseSelect

Activate