快速复制和粘贴公式

时间:2016-01-18 14:47:53

标签: vba excel-vba excel

我一直在尝试编写一个简单的代码,从一个单元格复制值并将其公式粘贴到一列中的所有单元格中(有几个单元格,大约3000个)。代码有效,但运行大约需要30分钟,所以对我来说不行。我也试图让公式的值没有" ="然后使用replace命令,但它也不起作用。任何人都可以帮助我,以便在1分钟内运行宏?这是我尝试做的代码的一部分:

sub copy_paste

Worksheets("Formatar").Range("H1:L1").Copy
Worksheets("Formatar").Range("H3").PasteSpecial xlValue
Worksheets("Formatar").Range("H3:L3").Copy
Range(Selection, Selection.End(xlDown)).Select
Selection.PasteSpecial xlFormulas

end sub

1 个答案:

答案 0 :(得分:2)

告诉我这是否对你有帮助......

Sub copy_paste()
    Worksheets("Formatar").Range("H1:L1").Copy 'Copy from row 1
    Worksheets("Formatar").Range("H3").PasteSpecial xlPasteValues 'paste the values to row 3
    Worksheets("Formatar").Range("H3:L3").Copy 'here you copy that (the values) 
    Range(Selection, Selection.End(xlDown)).Select you select eveything from row3 
    Selection.PasteSpecial xlPasteValues 'and paste it... but you copy just values from 3! 
End Sub

结束然后在第一次出现时粘贴ir并丢失数据。

这是我的建议。

Sub copy_paste()
    Dim sht As Worksheet
    Dim r
    Dim H
    Dim L

    Set sht = Sheets("Formatar") 'store the sheet

    sht.Activate 'activate it!
    Range("H1:L1").Copy
    Range("H3").PasteSpecial xlPasteFormulas 'Paste the formula
    Range("H3:L3").Copy 'then copy again

    H = Range("H1").Column 'Just to take the number of the columns H and L
    L = Range("L1").Column

    r = Range("H3").End(xlDown).Row - 1 'Take the number of the last blank row.
    Range(Cells(3, H), Cells(r, L)).PasteSpecial xlPasteValues
    'Here you paste values, of if you need the
    'formula use this: xlPasteFormulas
    Application.CutCopyMode = False 'never forget this...
End Sub

修改

这可能会有所帮助......

'Application.Calculation = xlManual



Sub copy_paste()
    Dim sht As Worksheet
    Dim r
    Dim H
    Dim L

    Set sht = Sheets("Formatar") 'store the sheet

    sht.Activate 'activate it!
    Range("H1:L1").Copy
    Range("H3").PasteSpecial xlPasteFormulas 'Paste the formula
    Application.Calculation = xlManual 'Not automatic calculation
    Range("H3:L3").Copy 'then copy again

    H = Range("H1").Column 'Just to take the number of the columns H and L
    L = Range("L1").Column

    r = Range("H3").End(xlDown).Row - 1 'Take the number of the last blank row.
    Range(Cells(3, H), Cells(r, L)).PasteSpecial xlPasteValues
    'Here you paste values, of if you need the
    'formula use this: xlPasteFormulas
    Application.CutCopyMode = False 'never forget this...
    Calculate 'Calculate the whole sheet
    Application.Calculation = xlCalculationAutomatic 'return automatic calculation
End Sub