VBA复制某些细胞

时间:2017-01-16 10:52:39

标签: excel vba excel-vba

我有以下问题。我在Excel中有三列

A               B        C
13/03/2015  GXH15   
16/03/2015  GXH15   
17/03/2015  GXH15   
18/03/2015  GXH15   
19/03/2015  GXH15   
20/03/2015  GXH15   17/03/2015
23/03/2015  GXM15   
24/03/2015  GXM15   

C中的公式为=IF(Ob61=b62,"",a61-3)。所以我尝试编写一个代码,它接受B中的所有值,并通过留下空单元格将它们复制到另一列中,这是我不需要的。

Option Explicit
Sub Roll()

Dim RollDate As Range
Dim i As Long
Dim cell As Range


Set RollDate = Range("B7:B2500")

i = 1
For Each cell In RollDate
    If Not IsEmpty(ActiveCell.Value) Then
    cell.Value.Copy Range("X7")
    cell.Value = i
    i = i + 1
End If
Next
End Sub

这是我写的,但它不起作用。

更新

感谢您的回答,但它们仍然无法正常工作。我会尽力使事情更清楚

A               B        C
13/03/2015  GXH15   
16/03/2015  GXH15   
17/03/2015  GXH15   
18/03/2015  GXH15   
19/03/2015  GXH15   
20/03/2015  GXH15   17/03/2015
23/03/2015  GXM15   
24/03/2015  GXM15
20/03/2015  GXH15   xx/03/2015
23/03/2015  GXM15   
24/03/2015  GXM15

我的目标是在colum X中使用C

中的值
X
17/03/2015
xx/03/2015
.
.
.
etc.

2 个答案:

答案 0 :(得分:0)

在OP澄清之后编辑

Sub Roll2()
    With Range("b7:b2500")
        .Offset(, 1).SpecialCells(xlCellTypeFormulas, xlNumbers).Copy '<--| consider cells with number value resulting from a formula
        .Offset(, 22).PasteSpecial SkipBlanks:=True, Paste:=xlPasteValues
    End With
End Sub

答案 1 :(得分:0)

还有一些猜测,不清楚你在问什么,但对上述答案的解释略有不同。

Option Explicit

Sub Roll()

Dim RollDate As Range
Dim i As Long
Dim cell As Range

Set RollDate = Range("B7:B2500")

i = 1
For Each cell In RollDate.Offset(, 1).SpecialCells(xlCellTypeFormulas, xlNumbers)
    Range("X" & Rows.Count).End(xlUp)(2).Value = cell.Value
    cell.Value = i
    i = i + 1
Next

End Sub
相关问题