简化代码

时间:2013-10-07 21:15:36

标签: excel-vba vba excel

代码的第一部分工作得很好,但我可以使它更简单,类似于第二个代码吗?我尝试了第二个,但excel返回“运行时错误1004 - 应用程序定义或对象定义的错误”。

            Worksheets(sample).Range("ratio143144").Copy
            Worksheets("Results").Activate
            Range("D" & inserir.Row).Select
            ActiveSheet.PasteSpecial Link:=True

            Worksheets(sample).Range("ratio146144").Copy
            Worksheets("Results").Range("I" & inserir).PasteSpecial Link:=True

GolezTrol,我尝试使用inserir.row,但excel不断返回相同的错误。下面是完整的代码。你有什么建议吗?

Sub CopiarOriginais()

    Dim Certeza As VbMsgBoxResult
    Dim sample As String
    Dim inserir As Range


        ActiveSheet.Name = Range("Y1").Value
        sample = Range("Y1").Value

    Certeza = MsgBox("Você tem certeza de que os dados originais já não foram copiado? Utilizar novamente essa função, após o teste 2-sigma ter sido aplicado, comprometerá os seus dados originais.", vbYesNo)

        If Certeza = vbNo Then End

        Sheets("Results").Activate
        Range("B2").End(xlDown).Offset(1, 0).Select
        Set inserir = ActiveCell




        Sheets(sample).Activate

        Range("B3:D122").Copy
        Range("B132").PasteSpecial xlPasteValues
        Application.CutCopyMode = False



        Worksheets(sample).Range("ratio143144").Copy

            Worksheets("Results").Activate
            Range("D" & inserir.Row).Select
            ActiveSheet.PasteSpecial Link:=True

        Worksheets(sample).Range("ratio146144").Copy

            Worksheets("Results").Activate
            Range("I" & inserir.Row).Select
            ActiveSheet.PasteSpecial Link:=True

'        Worksheets("Results").Range("I" & inserir.Row).PasteSpecial Link:=True

        Worksheets(sample).Range("ratio145144").Copy
        Worksheets("Results").Range("G" & inserir.Row).PasteSpecial Link:=True

        Worksheets(sample).Range("ratio1431442se").Copy
        Worksheets("Results").Range("F" & inserir).PasteSpecial Link:=True

        Worksheets(sample).Range("ratio1451442se").Copy
        Worksheets("Results").Range("H" & inserir).PasteSpecial Link:=True

        Worksheets(sample).Activate


    Range("A1").Select

End Sub

1 个答案:

答案 0 :(得分:2)

我想,第二个应该可以正常工作,但你在范围内使用的是"I"而不是"D"。此外,您使用inserir代替inserir.Row。所以我认为这只是一个简单的错误。理论上它应该可以正常工作。