2017-10-04 65 views
1

我記錄了一個宏&,它與我研究和測試的一些代碼集成在一起,這些代碼單獨運行。但是,把它們結合在一起,我偶然發現了運行宏的錯誤。彈出,顯示自動輸入數據的錯誤

Compile Error: Expected End With

將不勝感激所有幫助我能得到解決呢

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

    Windows("Y738 Data").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 

     rng.Select 
    End With 

    Selection.Copy 

    'Open next workbook 
    Windows("Y783").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("Y738").Activate 

    With Sheets("Graph data") 
     Columns("B:B").Select 
     Application.CutCopyMode = False 
     Selection.Delete Shift:=xlToLeft 
     Sheets("Graph Data").Select 

End Sub 
+2

你'隨着表( 「圖形數據」)'從底部約5行沒有對應的'結束With'。目前,你沒有使用'With'(後面的代碼沒有任何方法/屬性,只有'.'開頭),所以你可以去掉With(或者修改後面的代碼來使用它)。 – YowE3K

+2

如果你很好地格式化你的代碼並正確地縮進它(我在你的問題中爲你做了什麼),那麼你清楚地看到了這個問題。你應該遵循的第一條規則是「*很好地格式化你的代碼並使用正確的縮進*」並使用人類可讀的變量命名。否則,發現錯誤可能非常麻煩,甚至是不可能的。 –

+0

@ YowE3K我明白了。感謝您的解釋(: 欣賞它 – Tyler

回答

0

你已經錯過了和你的代碼的底部,最後一個消息框。

嘗試此(未測試的)

Sub Book1UpdateDelete() 
' 
' Book1UpdateDelete Macro 
' 
' Keyboard Shortcut: Ctrl+g 
' 
'Select values in a column from specified workbook and sheet 
Dim LR As Long, cell As Range, rng As Range 
Windows("Y738 Data").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 
rng.Select 

End With 
Selection.Copy 

'Open next workbook 
Windows("Y783").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("Y738").Activate 
With Sheets("Graph data") 
Columns("B:B").Select 
Application.CutCopyMode = False 
Selection.Delete Shift:=xlToLeft 
Sheets("Graph Data").Select 
End with 
End Sub 
+0

謝謝你,你是對的(: 我的宏現在正在運行 – Tyler

+1

'With'語句是完全無用的,不會以任何方式影響代碼。該代碼的行爲 –

+0

@Peh明白了,謝謝你讓我知道並格式化了我的代碼 欣賞它 – Tyler