2017-07-19 144 views
1

可能很容易回答的問題的道歉。我是通過關於如何搜索行並將其粘貼在另一個工作表中的網站上的一些代碼拖網,代碼爲以下之一:VBA - 如何聲明「單元格」變量

Sub Test() 
For Each Cell In Sheets(1).Range("J:J") 
    If Cell.Value = "131125" Then 
    matchRow = Cell.Row 
    Rows(matchRow & ":" & matchRow).Select 
    Selection.Copy 

    Sheets("Sheet2").Select 
    ActiveSheet.Rows(matchRow).Select 
    ActiveSheet.Paste 
    Sheets("Sheet1").Select 
    End If 
Next 
End Sub 

我想知道什麼是「細胞」應該被聲明爲,如:

Dim Cell As ... 

我知道,如果沒有「選項顯式」,這是無關緊要的,但我很好奇儘管如此,所以請不要幫助,如果你能解釋一下。

感謝您對您的幫助提前:)

回答

2

在你的情況,cellrange,所以

dim cell as range 

和:始終使用Option Explicit

0

您可以使用Range。它們有些可以互換。

2

行走在一定範圍內產生了一個範圍,Dim Cell As Range

如有疑問請VBA:msgbox TypeName(Cell)

0

不好意思,那個可怕的代碼,它冒犯了眼睛。查找會更好,只是在更好地認識利益

Sub Test() 
Dim Cell as Range 
For Each Cell In Sheets(1).Range("J:J") 
     If Cell.Value = "131125" Then 
      Cell.EntireRow.copy Destination:=Sheets("Sheet2").range("a" & cell.row) 
      'You might want to exit here if there's only one value to find with 
      'Exit For 
     End If 
Next 

末次