0

所以这次我的问题是尝试更新。发生的事情是当我到达 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
4

2 回答 2

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.

于 2013-08-02T23:01:00.793 回答
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
于 2013-08-03T10:40:12.267 回答