2016-07-12 127 views
0

我有以下代碼試圖獲取動態範圍並打印範圍中每個單元格的值。在for範圍內運行for循環時編譯時出錯

Sub Macro3() 

' 
    Range("D10").Select 
    x = Range(Selection, Selection.End(xlDown)).Select 


    For Each cell In x 
     MsgBox (cell) 
    Next x 

End Sub 

如果我運行它,但它會得到一個編譯錯誤...有什麼想法在這裏出了什麼問題?

+0

什麼是錯誤? – Bathsheba

+0

接下來對變量的不準確引用(翻譯自荷蘭文) –

回答

1

試試這個:(使用Set和刪除.Select

Set x = Range(Selection, Selection.End(xlDown)) 

NextNext cell代替Next x

0
Option Explicit 

Sub Macro3() 

    Dim x  As Range 
    Dim cell As Range 


    Range("D10").Select 

    Set x = Range(Selection, Selection.End(xlDown)).Select 
    For Each cell In x 
     MsgBox (cell) 
    Next cell 

End Sub 

嗨。你需要下一個單元格而不是下一個x。請享用! :D