Excel宏不会将数据复制到正确的单元格中

时间:2013-08-10 19:18:11

标签: excel excel-vba vba

我创建了一个包含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

1 个答案:

答案 0 :(得分: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选项以防万一。