我要选择两个单元格范围,并在存储每个选定范围后尝试将第一个单元格范围复制到第二个单元格范围。当我使用F8逐步浏览代码时,所有方法都可以正常工作,但是当最后一个if语句为true时,我无法将FirstCell复制到SecondCell
Sub Move()
Dim FirstCell As String
Dim SecondCell As String
Dim cel As Excel.Range
Dim count As Integer
Application.EnableEvents = False
count = 0
For Each cell In Selection
If count = 0 Then
FirstCell = cell.Value
FirstCell1 = cell.Address
End If
If count = 1 Then
SecondCell = cell.Value
SecondCell1 = cell.Address
End If
count = count + 1
Next
If FirstCell <> "" And SecondCell = "" And count = 2 Then 'Else Exit Sub
Selection.FirstCell.Copy
SecondCell1.PasteSpecial
End If
End Sub
答案 0 :(得分:0)
似乎有些令人费解。要粘贴
Range(FirstCell1).Copy Range(SecondCell1)
FirstCell1
和SecondCell1
是地址。即字符串,因此无法复制,这是Range对象的一种方法。
您应该养成声明所有变量并使用Option Explicit的习惯。