简体   繁体   English

如何在Macro Excel(VBA)中复制粘贴

[英]How to copy-paste in macro excel (VBA)

I need to copy a lot of rows. 我需要复制很多行。 I tried to do something.copy something else.paste but it's extremely slow 我试图做某事。复制其他东西。粘贴,但是它非常慢

I tried to do Range(..).value(formula) = Range(..).value(formula) but its not so good because i have a date there that turn to ###### 我试图做Range(..)。value(formula)= Range(..)。value(formula),但是效果不是很好,因为我在那里有个约会,它变成了######

I need a faster way to do this copy/paste 我需要一种更快的方式来执行此复制/粘贴操作

This is my code: 这是我的代码:

Function Last_Col(k As Long) As Long
    Last_Col = Cells(k, Columns.Count).End(xlToLeft).Column
End Function

Function Last_Col_Doc() As Long
    Last_Col_Doc = Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 0).Column
End Function

Function Is_Grouped(i As Long) As Boolean
    Is_Grouped = (Cells(i, 2).Rows.OutlineLevel > 1)
End Function

Function Is_Bold(i As Long) As Boolean
    Is_Bold = Cells(i, 2).Font.Bold
End Function

Function Print_NA(i As Long, k As Long) As Boolean
    Range(Cells(i, 21), Cells(i, 21 + k - 2)).Value = "NA"
End Function

Function Last_Row() As Long
    Last_Row = Cells(Rows.Count, "B").End(xlUp).Row
End Function

Sub EditParanoia()

    Dim FrstBlkRow As Long
    Dim flag As Boolean
    Dim i As Long
    Dim HeadLen As Long

    FrstBlkRow = Last_Col(1) + 1

    If FrstBlkRow < 25 Then 'first edit

        flag = True
        i = 2
        Do While flag
            If Is_Bold(i) Then
                flag = False
            Else
                i = i + 1
            End If
        Loop


        HeadLen = Last_Col(i)
        Range(Cells(i, 2), Cells(i, HeadLen)).Copy
        Range(Cells(1, FrstBlkRow), Cells(1, FrstBlkRow + HeadLen - 2)).PasteSpecial

    Else
        FrstBlkRow = 21
        HeadLen = 10

    End If

    Dim j As Long
    For i = 2 To Last_Row Step 1

        If Not Is_Grouped(i) And Not Is_Grouped(i + 1) And Cells(i, FrstBlkRow + 1).Value = vbNullString Then
        'if not part of group
            Range(Cells(i, FrstBlkRow), Cells(i, FrstBlkRow + HeadLen - 2)).Value = "NA"

        ElseIf Not Is_Grouped(i) And Is_Grouped(i + 1) And Is_Grouped(i + 2) And Not Is_Grouped(i + 3) Then
        'if Part of group of 1 val
            Range(Cells(i + 2, 2), Cells(i + 2, 2 + HeadLen - 2)).Copy
            Range(Cells(i, FrstBlkRow), Cells(i, FrstBlkRow + HeadLen - 3)).PasteSpecial

        ElseIf Not Is_Grouped(i) And Is_Grouped(i + 1) Then
        'if part of group of more then one val
            j = 1
            Do Until Is_Grouped(i + j) And Not Is_Grouped(i + j + 1)
            'j will get the langth of any group
                j = j + 1
            Loop
            'past the relevant cell in the right place
            Range(Cells(i + 2, 2), Cells(i + 2 + j - 1 - 1, 2 + HeadLen - 2)).Copy
            Range(Cells(i, FrstBlkRow), Cells(i + j - 1 - 1, FrstBlkRow + HeadLen - 3)).PasteSpecial

            'past the head respectively
            Range(Cells(i, 1), Cells(i, 20)).Copy
            Range(Cells(i + 1, 1), Cells(i + j - 2, FrstBlkRow - 1)).PasteSpecial

         End If

    Next

End Sub

When you say you've tried "Range(..).value(formula) = Range(..).value(formula)", what do you mean? 当您说过“ Range(..)。value(formula)= Range(..)。value(formula)”时,您是什么意思? You should be able to set two ranges equal to eachother: 您应该能够设置两个彼此相等的范围:

Say A1:A10 has "Batman, 10-01-2015" and you want to copy that range to B1:B10, Range("B1:B10").Value = Range("A1:A10").Value . 假设A1:A10的“蝙蝠侠,2015年10月1日”,您要将该范围复制到B1:B10, Range("B1:B10").Value = Range("A1:A10").Value You can't do that? 你不能那样做吗? I tried it with dates, and it set the B range values to dates, no reformatting necessary. 我尝试使用日期,并将B范围值设置为日期,无需重新格式化。

I also notice in your code, you PasteSpecial , but don't specify what type of special paste. 我还在您的代码中注意到您是PasteSpecial ,但是没有指定什么类型的特殊粘贴。 See the Microsoft (or this one) page for more info. 有关更多信息,请参见Microsoft (或这一页)页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM