VBA从另一个单元格中的工作表复制特定单元格

时间:2017-03-08 20:27:07

标签: excel vba excel-vba

这是我的案例

我的宏中有这个代码:代码很好,无需更改。

Sheets("Water").Select
Cells.Select
Range("A2").Select
ActiveCell.Formula = "=SUM(D2:N2)+((COUNTIF(D2:N2,""GOLD"")+COUNTIF(D2:N2,""PLATIN""))*1)+((COUNTIF(D2:N2,""PLPLUS"")+COUNTIF(D2:N2,""AMBASS""))*2)"
Range("A2").AutoFill Destination:=Range("A2:A" & Cells(Rows.Count, 2).End(xlUp).Row)
LastRow = Range("A2").End(xlDown).Row
Cells(LastRow + 2, "A").Formula = "=SUM(A2:A" & LastRow & ")"
Dim LRowA As String, LRowB As String
LRowA = [A4200].End(xlUp).Address
Range("A:A").Interior.ColorIndex = xlNone
Range("A2:" & LRowA).Interior.ColorIndex = 33
Range("A:A").HorizontalAlignment = xlCenter

关于本规范,我们在Sheets“Water”上。

我有一个公式,结果显示在表格“水”中:

 Cells(LastRow + 2, "A").Formula = "=SUM(A2:A" & LastRow & ")"

现在我希望此结果显示在单元格M8中的“下载”工作表中。

有可能.....

1 个答案:

答案 0 :(得分:1)

你是说这个意思吗?也缩短了你的代码。

Sub x()

Dim LRowA As String, LRowB As String

With Sheets("Water")
    .Range("A2:A" & .Cells(Rows.Count, 2).End(xlUp).Row).Formula = "=SUM(D2:N2)+((COUNTIF(D2:N2,""GOLD"")+COUNTIF(D2:N2,""PLATIN""))*1)+((COUNTIF(D2:N2,""PLPLUS"")+COUNTIF(D2:N2,""AMBASS""))*2)"
    LastRow = .Range("A2").End(xlDown).Row
    .Cells(LastRow + 2, "A").Formula = "=SUM(A2:A" & LastRow & ")"
    LRowA = .Range("A4200").End(xlUp).Address
    .Range("A:A").Interior.ColorIndex = xlNone
    .Range("A2:" & LRowA).Interior.ColorIndex = 33
    .Range("A:A").HorizontalAlignment = xlCenter
End With

Sheets("Download").Range("M8").Formula = "=""Bottle of water: "" & SUM(Water!A2:A" & LastRow & ")"

End Sub