簡體   English   中英

如何在Macro Excel(VBA)中復制粘貼

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

我需要復制很多行。 我試圖做某事。復制其他東西。粘貼,但是它非常慢

我試圖做Range(..)。value(formula)= Range(..)。value(formula),但是效果不是很好,因為我在那里有個約會,它變成了######

我需要一種更快的方式來執行此復制/粘貼操作

這是我的代碼:

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

當您說過“ Range(..)。value(formula)= Range(..)。value(formula)”時,您是什么意思? 您應該能夠設置兩個彼此相等的范圍:

假設A1:A10的“蝙蝠俠,2015年10月1日”,您要將該范圍復制到B1:B10, Range("B1:B10").Value = Range("A1:A10").Value 你不能那樣做嗎? 我嘗試使用日期,並將B范圍值設置為日期,無需重新格式化。

我還在您的代碼中注意到您是PasteSpecial ,但是沒有指定什么類型的特殊粘貼。 有關更多信息,請參見Microsoft (或這一頁)頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM