Excel Macro -Cells()为什么失败?

时间:2018-05-30 12:49:29

标签: excel vba excel-vba

我对宏很新。我使用此代码将两个列值连接成一列。这个代码今天失败了,对于10页的第一行,它对其余的行都有效。为什么这样发生,我根本没有改变任何东西! 感谢。

Sub FixCrossSell()
    Dim wb As Workbook
    Dim lr As Long

    Set wb = ThisWorkbook
    wb.Worksheets("CrossSell").Activate

    Cells(2, 1).Value = "=B2&E2"

    lr = Cells(Rows.Count, 2).End(xlUp).Row
    Range("A2").Select
    Selection.Copy

    Range("A3:A" & lr).Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False

    Application.Calculate

    Range("A2:A" & lr).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

End Sub

2 个答案:

答案 0 :(得分:1)

试试这个:

Sub FiXCrossSell()
    Dim lr As Long
    With Worksheets("CrossSell")
        lr = .Cells(Rows.Count, 2).End(xlUp).Row
        With .Range("A2:A" & lr)
            .FormulaR1C1 = "=rc2&rc5"
            .Value = .Value
        End With
    End With
End Sub

答案 1 :(得分:0)

可能只计算B列中的单元格,然后将公式放在A列中

  Sub Button1_Click()
    Dim LstRw As Long, Rng As Range, Sh As Worksheet

    Set Sh = Sheets("CrossSell")
    With Sh
        LstRw = .Cells(.Rows.Count, "B").End(xlUp).Row
        Set Rng = .Range("A2:A" & LstRw)
        Rng = "=B2&E2"
    End With
End Sub

啊......我在考虑的时候看到别人回答了这个问题。