简体   繁体   English

如何复制一系列公式值并将它们粘贴到另一个工作表中的特定范围?

[英]How do I copy a range of formula values and paste them to a specific range in another sheet?

I'm trying to get an excel macro to work but I'm having an issue with copying the values from formula-containing cells.我试图让一个 excel 宏工作,但我在从包含公式的单元格中复制值时遇到问题。

So far this is what I have and it works fine with the non-formula cells.到目前为止,这就是我所拥有的,它适用于非公式单元格。

Sub Get_Data()
    Dim lastrow As Long

    lastrow = Sheets("DB").Range("A65536").End(xlUp).Row + 1

    Range("B3:B65536").Copy Destination:=Sheets("DB").Range("B" & lastrow)
    Range("C3:C65536").Copy Destination:=Sheets("DB").Range("A" & lastrow)
    Range("D3:D65536").Copy Destination:=Sheets("DB").Range("C" & lastrow)
    Range("E3:E65536").Copy Destination:=Sheets("DB").Range("P" & lastrow)
    Range("F3:F65536").Copy Destination:=Sheets("DB").Range("D" & lastrow)
    Range("AH3:AH65536").Copy Destination:=Sheets("DB").Range("E" & lastrow)
    Range("AIH3:AI65536").Copy Destination:=Sheets("DB").Range("G" & lastrow)
    Range("AJ3:AJ65536").Copy Destination:=Sheets("DB").Range("F" & lastrow)
    Range("J3:J65536").Copy Destination:=Sheets("DB").Range("H" & lastrow)
    Range("P3:P65550").Copy Destination:=Sheets("DB").Range("I" & lastrow)
    Range("AF3:AF65536").Copy Destination:=Sheets("DB").Range("J" & lastrow)

End Sub

How can I make it so it pastes the values for formulas?我怎样才能使它粘贴公式的值?

If this can be changed/optimized, I'd appreciate it too.如果这可以改变/优化,我也会很感激。

You can change你可以改变

Range("B3:B65536").Copy Destination:=Sheets("DB").Range("B" & lastrow)

to

Range("B3:B65536").Copy 
Sheets("DB").Range("B" & lastrow).PasteSpecial xlPasteValues

BTW, if you have xls file (excel 2003), you would get an error if your lastrow would be greater 3.顺便说一句,如果你有 xls 文件(excel 2003),如果你的lastrow大于 3,你会得到一个错误。

Try to use this code instead:尝试改用此代码:

Sub Get_Data()
    Dim lastrowDB As Long, lastrow As Long
    Dim arr1, arr2, i As Integer

    With Sheets("DB")
        lastrowDB = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With

    arr1 = Array("B", "C", "D", "E", "F", "AH", "AI", "AJ", "J", "P", "AF")
    arr2 = Array("B", "A", "C", "P", "D", "E", "G", "F", "H", "I", "J")

    For i = LBound(arr1) To UBound(arr1)
        With Sheets("Sheet1")
             lastrow = Application.Max(3, .Cells(.Rows.Count, arr1(i)).End(xlUp).Row)
             .Range(.Cells(3, arr1(i)), .Cells(lastrow, arr1(i))).Copy
             Sheets("DB").Range(arr2(i) & lastrowDB).PasteSpecial xlPasteValues
        End With
    Next
    Application.CutCopyMode = False
End Sub

Note, above code determines last non empty row on DB sheet in column A (variable lastrowDB ).请注意,上面的代码确定A列(变量lastrowDB )中DB表上的最后一个非空行。 If you need to find lastrow for each destination column in DB sheet, use next modification:如果您需要为DB表中的每个目标列找到最后一行,请使用下一个修改:

For i = LBound(arr1) To UBound(arr1)
   With Sheets("DB")
       lastrowDB = .Cells(.Rows.Count, arr2(i)).End(xlUp).Row + 1
   End With

   ' NEXT CODE

Next

You could also use next approach instead Copy/PasteSpecial .您也可以使用 next 方法代替Copy/PasteSpecial Replace代替

.Range(.Cells(3, arr1(i)), .Cells(lastrow, arr1(i))).Copy
Sheets("DB").Range(arr2(i) & lastrowDB).PasteSpecial xlPasteValues

with

Sheets("DB").Range(arr2(i) & lastrowDB).Resize(lastrow - 2).Value = _
      .Range(.Cells(3, arr1(i)), .Cells(lastrow, arr1(i))).Value

How about if you're copying each column in a sheet to different sheets?如果您将工作表中的每一列复制到不同的工作表如何? Example: row B of mysheet to row B of sheet1, row C of mysheet to row B of sheet 2...示例:mysheet 的 B 行到 sheet1 的 B 行,mysheet 的 C 行到 sheet 2 的 B 行......

暂无
暂无

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

相关问题 VBA复制范围值并粘贴到另一张工作表中 - VBA Copy range values and paste in another sheet 将值从一个工作簿复制并粘贴到另一个工作簿后,在我双击单元格之前,公式不适用于特定范围 - After copy and paste values from one workbook to another, a formula doesn't work with a specific range until I doubleClick cells 我需要在多张纸上复制特定范围并将它们粘贴到最后一张纸上 - I need to copy a specific range in multiple sheets and paste them on a final sheet 我想复制一系列单元格并将它们粘贴到另一个工作表,具体取决于下拉选择并使用按钮激活 - I want to copy a range of cells and paste them to another sheet dependent on a drop down selection and activated using a button 如何从一个工作表中复制特定的列并粘贴到第一行中不同范围的另一工作表中? - How to copy specific columns from one sheet and paste in another sheet in a different range at the first row? 如何复制一张纸中的特定列并粘贴到另一张纸中的不同范围? - How to copy specific columns from one sheet and paste in another sheet in a different range? Excel 2007 VBA:如何从一张纸上的动态范围复制和粘贴到另一张纸的第一行? - Excel 2007 VBA: How do I copy and paste from a dynamic range on one sheet to the first empty row of another sheet? 如何使用VBA将范围内的值从一张纸复制到另一张纸上的预定义订单? - How to copy the values in a range from one sheet to and paste it another sheet on a predefied order using VBA? 将范围作为值复制并粘贴到另一张纸上的下一个空列中 - Copy & Paste range as values into next empty column on another sheet 使用VBA并使用“偏移”将一系列单元格值复制并粘贴到另一张工作表中 - Copy and paste a range of cell values into another sheet with VBA and using Offset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM