2017-07-19 145 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 

末次