0

我创建了一个包含 5 列的电子表格,当用户按下回车键时,它应该将信息复制到第二个电子表格。然而,第二个按钮正在从同一第一列复制信息,但我已经检查过了,两个按钮都选择了 Trade1 和 Trade2 宏。

屏幕截图 1:http ://screencast.com/t/zudhZCIQGA3l 屏幕截图 2:http ://screencast.com/t/6NyW3gTy8oY

正如您在 SS2 中看到的,在按下按钮 2 后,它复制了错误的数据。

代码如下

Sub Trade1()
    Sheets("Trades").Select
    Rows("2:2").Select
    Selection.Insert Shift:=xlDown
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R2C2"
    Range("B2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R3C2"
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R4C2"
    Range("D2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R5C2"
    Range("E2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R6C2"
    Range("F2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R8C2"
    Range("G2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R7C2"
    Range("H2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R9C2"
    Range("I2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R10C2"
    Range("J2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R11C2"
    Range("K2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R11C2"
    Range("K2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R12C2"
    Range("P2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R13C2"
    Range("Q2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R14C2"
    Range("R2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R15C2"
    Range("S2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R16C2"
    Range("T2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R17C2"
    Range("U2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R18C2"
    Range("V2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R19C2"
    Range("V3").Select
    Sheets("Enter Trade").Select
End Sub

Sub Trade2()
    Sheets("Trades").Select
    Rows("2:2").Select
    Selection.Insert Shift:=xlDown
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R2C3"
    Range("B2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R3C3"
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R4C3"
    Range("D2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R5C3"
    Range("E2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R6C3"
    Range("F2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R8C3"
    Range("G2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R7C3"
    Range("H2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R9C3"
    Range("I2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R10C3"
    Range("J2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R11C3"
    Range("K2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R11C3"
    Range("K2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R12C3"
    Range("P2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R13C3"
    Range("Q2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R14C3"
    Range("R2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R15C3"
    Range("S2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R16C3"
    Range("T2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R17C3"
    Range("U2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R18C3"
    Range("V2").Select
    ActiveCell.FormulaR1C1 = "=+'Enter Trade'!R19C3"
    Range("V3").Select
    Sheets("Enter Trade").Select
End Sub
4

1 回答 1

1

在这里,您有一个简单的代码复制(和转置)两张纸之间的范围。

Dim fromRange As Range, toRange As Range

Set fromRange = Sheets("Sheet1").Range("A1:A3")
Set toRange = Sheets("Sheet2").Range("A1")

fromRange.Copy
toRange.PasteSpecial Paste:=xlPasteValues, Transpose:=True

您可以递归调用此代码(同时遍历原始范围中的所有列)。请记住,范围也可以通过以下方式定义Cell

Set fromRange = Sheets("Sheet1").Range(Sheets("Sheet1").Cells(1, 1), Sheets("Sheet1").Cells(3, 1))

此代码与Set fromRange上面的代码相同,尽管在循环中此代码可能更有帮助。例如:

Dim iniRow As Integer, maxRow As Integer, col As Integer, maxCol As Integer

iniRow = 1
maxRow = 20
col = 0
maxCol = 10
Do
   col = col + 1
   Set fromRange = Sheets("Sheet1").Range(Sheets("Sheet1").Cells(iniRow, col), Sheets("Sheet1").Cells(maxRow, col))
   Set toRange = Sheets("Sheet2").Range(Sheets("Sheet2").Cells(col, 1)) 'Assuming that the row in the destination sheet equals the column in the original one
   toRange.PasteSpecial Paste:=xlPasteValues, Transpose:=True
Loop While (col < maxCol)

或者你可以直接做

Set fromRange = Sheets("Sheet1").Range("A1:Z1000") 
Set toRange = Sheets("Sheet2").Range("A1")

但我总是更喜欢强调Cells新手的选择,以防万一。

于 2013-08-10T20:14:32.573 回答