VBA - 如何声明 "Cell" 变量
VBA - How to declare "Cell" variable
对于这个可能非常容易回答的问题深表歉意。我在网站上浏览了一些关于如何搜索一行并将其粘贴到另一个工作表中的代码,代码如下:
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
我想知道 "Cell" 应该声明成什么,如:
Dim Cell As ...
我知道如果没有 "Option Explicit",这是无关紧要的,但我还是很好奇,所以如果可以的话请帮忙解释一下。
提前感谢您的帮助:)
在你的例子中,cell
是 range
,所以
dim cell as range
并且:总是使用Option Explicit
您可以使用 Range
。它们在某种程度上可以互换。
遍历一个范围产生一个范围所以Dim Cell As Range
如有疑问请咨询VBA:msgbox TypeName(Cell)
抱歉,那是可怕的代码,它冒犯了眼睛。找到会更好,但只是为了更好地理解
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
结束子
对于这个可能非常容易回答的问题深表歉意。我在网站上浏览了一些关于如何搜索一行并将其粘贴到另一个工作表中的代码,代码如下:
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
我想知道 "Cell" 应该声明成什么,如:
Dim Cell As ...
我知道如果没有 "Option Explicit",这是无关紧要的,但我还是很好奇,所以如果可以的话请帮忙解释一下。
提前感谢您的帮助:)
在你的例子中,cell
是 range
,所以
dim cell as range
并且:总是使用Option Explicit
您可以使用 Range
。它们在某种程度上可以互换。
遍历一个范围产生一个范围所以Dim Cell As Range
如有疑问请咨询VBA:msgbox TypeName(Cell)
抱歉,那是可怕的代码,它冒犯了眼睛。找到会更好,但只是为了更好地理解
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
结束子