对于col W(test)为Yes的行,我想将公式转换为col P(ReportedGrossActivityReduction)中的值。
我在网上搜索后通过录制和修改创建了以下宏。
Sub q()
Dim a As Range
Dim b As Range
Dim c As Range
Set a = Range("W2").Value
Set b = Range("P2").Value
With ActiveSheet
For Each b In .Range("P2")
If a.Range("W2").Value = "Yes" Then
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next
End With
End Sub
答案 0 :(得分:1)
这是一个"无循环"解决办法只留下列P细胞的结果,其对应的列W细胞含量是"是":
Sub Main()
Dim vals As Variant 'declare a Variant where to store an array
With Range("W2", Cells(Rows.Count, "W").End(xlUp)) ' reference column W range from row 2 down to last not empty one
vals = .Value ' store referenced range values
.Replace what:="Yes", replacement:=1, lookat:=xlWhole ' replace referenced range "Yes" content with a numeric one (1) to exploit subsequent SpecialCells method usage
With .SpecialCells(xlCellTypeConstants, xlNumbers).Offset(, -7) 'reference column P cells corresponding to referenced range cells with numeric content
.Value = .Value ' leave formula result only
End With
.Value = vals ' write back original referenced range values
End With
End Sub
答案 1 :(得分:0)
试试这个(代码注释):
'hugely recommended to use this option
Option Explicit
'sub/macro which copies data over columns
Sub CopyYesInW()
Dim lastRow As Long, i As Long
'determine last row in column W
lastRow = Cells(Rows.Count, "W").End(xlUp).Row
For i = 1 To lastRow
'if Yes in W then copy from P to W in current row
If Cells(i, "W").Value = "Yes" Then
Cells(i, "W").Value = Cells(i, "P").Value
End If
Next
End Sub