删除所有"选择"从录制的宏

时间:2015-01-25 02:10:14

标签: excel-vba select vba excel

我录制了这个然后添加了LR = LastRow以使其动态但我无法弄清楚如何删除所有选择

同样这些都做同样的事情,但是一种方法是写出数组better然后另一种方式,即faster,更多stable ......

由于

 Selection.FormulaArray = "=ISNUMBER(MATCH(RC[-5]&RC[-6],R1C1:R" & LR & "C1 & R1C2:R" & LR & "C2,0))"

 Selection.FormulaArray = "=ISNUMBER(MATCH(B1&A1,$A$1:$A$" & LR & " & $B$1:$B$" & LR & ",0))"

录制的宏

Sub Winding()
Dim ws As Worksheet
Dim Rng As Range
Dim LR As Long

 Set ws = Sheets("Unpivot_RegistrationData")
     LR = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Set Rng = ws.Range("G1").Resize(LR, 1)

Range("G1").Select
       Selection.FormulaArray = "=ISNUMBER(MATCH(B1&A1,$A$1:$A$" & LR & " & $B$1:$B$" & LR & ",0))"
       Selection.AutoFill Destination:=Rng, Type:=xlFillDefault
End Sub

1 个答案:

答案 0 :(得分:1)

首先,对A1R1C1样式的偏好应该由构建公式字符串的易用性驱动。性能或稳定性没有差异

要删除Selection,请尝试此 请注意,我已删除AutoFill,并在一步中将公式应用于整个范围。

Sub Winding()
    Dim Rng As Range
    Dim LR As Long

    With Worksheets("Unpivot_RegistrationData")
        LR = .Cells(.Rows.Count, 1).End(xlUp).Row
        Set Rng = .Range("G1:G" & LR)
    End With

    Rng.Cells(1, 1).FormulaArray = _
      "=ISNUMBER(MATCH(B1&A1,$A$1:$A$" & LR & " & $B$1:$B$" & LR & ",0))"
    Rng.Cells(1, 1).AutoFill Destination:=Rng, Type:=xlFillDefault
End Sub