选择两个单元格范围并在将每个选择范围存储为变量后尝试将一个单元格范围复制到另一个单元格

时间:2019-05-01 10:49:13

标签: excel vba

我要选择两个单元格范围,并在存储每个选定范围后尝试将第一个单元格范围复制到第二个单元格范围。当我使用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

1 个答案:

答案 0 :(得分:0)

似乎有些令人费解。要粘贴

Range(FirstCell1).Copy Range(SecondCell1)

FirstCell1SecondCell1是地址。即字符串,因此无法复制,这是Range对象的一种方法。

您应该养成声明所有变量并使用Option Explicit的习惯。