运行时更新子

时间:2013-08-02 22:31:07

标签: vba excel-vba excel

所以这次我的问题是尝试更新。发生的事情是当我到达Ht床单时。当我复制HT信息时,它会覆盖我刚放置的VT信息。那么无论如何要解决这个问题吗?

Private Sub CommandButton2_Click()

Dim queen As Range
Dim thought As Range
Dim one As Range
Dim waiting As Range
Dim prob As Range
Dim sign As Range

Set queen = Sheets("VT").Range("C2").End(xlDown)
Set thought = Sheets("VT").Range("C2").End(xlDown).Offset(0, -1)
Set one = Sheets("PalmFamily").Range("B1").End(xlDown)
Set waiting = one.End(xlToRight).Offset(1, 1)
Set prob = Sheets("HT").Range("C2").End(xlDown)
Set sign = Sheets("HT").Range("C2").End(xlDown).Offset(0, -1)

Sheets("VT").Activate
    Range("C2", queen).Select
    Application.CutCopyMode = False
    Selection.Copy
Sheets("PalmFamily").Activate
    one.Offset(1, 0).Select
    ActiveSheet.Paste
Sheets("VT").Activate
    Range("A2", thought).Select
    Application.CutCopyMode = False
    Selection.Copy
Sheets("PalmFamily").Activate
    waiting .Select
    ActiveSheet.Paste
Application.ScreenUpdating = True
Sheets("HT").Activate
    Range("C2", prob).Select
    Application.CutCopyMode = False
    Selection.Copy
Sheets("PalmFamily").Activate
    one.Offset(1, 0).Select
    ActiveSheet.Paste
Sheets("HT").Activate
    Range("A2", sign).Select
    Application.CutCopyMode = False
    Selection.Copy
Sheets("PalmFamily").Activate
    waiting.Select
    ActiveSheet.Paste

End Sub

2 个答案:

答案 0 :(得分:1)

您应该始终尽量不要在宏中使用复制和粘贴。

而不是写这个:

Sheets("VT").Activate
    Range("C2", q).Select
    Application.CutCopyMode = False
    Selection.Copy
Sheets("PalmFamily").Activate
    o.Offset(1, 0).Select
    ActiveSheet.Paste

你应该这样写:

Sheets("PalmFamily").Cells(R2, C2) = Sheets("VT").Cells(R1, C1)

这在定义变量之后起作用:

'R1 and C1 are row and column of C2
Dim R1 As Long, C1 As Long
R1 = 2
C1 = 3

'R2 and C2 are row and column of B1
Dim R2 As Long, C2 As Long
R1 = 1
C1 = 2

当你使用指向行和列的变量时,只需要一个简单的R2 = R2 + 1就可以很容易地增加它们。

答案 1 :(得分:0)

我认为这可以满足您的需求。

Private Sub CommandButton2_Click()

    Dim queen As Range, thought As Range, one As Range, waiting As Range, prob As Range, sign As Range

    Set queen = Sheets("VT").Range("C2").End(xlDown)
    Set thought = queen.Offset(0, -1)
    Set one = Sheets("PalmFamily").Range("B1").End(xlDown)
    Set waiting = one.End(xlToRight).Offset(1, 1)
    Set prob = Sheets("HT").Range("C2").End(xlDown)
    Set sign = prob.Offset(0, -1)

    Worksheets("VT").Range("C2", queen).Copy Destination:=one.Offset(1, 0)
    Worksheets("VT").Range("A2", thought).Copy Destination:=waiting
    Worksheets("HT").Range("C2", prob).Copy Destination:=one.End(xlDown).Offset(1, 0)
    Worksheets("HT").Range("A2", sign).Copy Destination:=waiting.End(xlDown).Offset(1, 0)

End Sub