[英]Copy cells with formulas in macro
I have a excel spreadsheet with 2 sheets. 我有2张纸的Excel电子表格。 In the first sheet, I got some values, when I click on a button then in the second sheet the following happens 1. a new row is inserted at the top 2. the data from the first sheet is copied. 在第一张工作表中,我得到了一些值,当我单击按钮时,在第二张工作表中发生以下情况:1.在顶部插入新行2.复制第一张工作表中的数据。
However, there are some columns with formulas after the copied cells, and I dont know how to do in vbscript to copy those formulas after your insert a new row. 但是,在复制的单元格后面有一些带有公式的列,在插入新行后,我不知道如何在vbscript中复制这些公式。
Sub Trade1()
Sheets("Trades").Select
Rows("2:2").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A2:R2").Select
Selection.ClearContents
Range("A2").Select
Dim fromRange As Range, toRange As Range
Set fromRange = Sheets("Enter Trade").Range("B2:B20")
Set toRange = Sheets("Trades").Range("A2")
fromRange.Copy
toRange.PasteSpecial Paste:=xlPasteValues, Transpose:=True
End Sub
After inserting a new row 2 (so that it's contents are now on row 3), I would copy row 3 back to row 2 (basically make a duplicate), and then override the values with the data you want to move from sheet 'Enter Trade' range B2:B20, like so: 插入新的第2行(以便其内容现在位于第3行)之后,我将第3行复制回第2行(基本上是重复的),然后用要从工作表'Enter中移出的数据覆盖这些值交易范围B2:B20,如下所示:
Sub Trade_Button_Click()
Dim btn As Button
Dim wsEntry As Worksheet
Dim wsTrades As Worksheet
Set wsEntry = Sheets("Enter Trade")
Set wsTrades = Sheets("Trades")
Set btn = wsEntry.Buttons(Application.Caller)
wsTrades.Rows(2).Insert
wsTrades.Rows(3).Copy wsTrades.Rows(2)
With Intersect(wsEntry.Range("A2", wsEntry.Cells(Rows.Count, "A").End(xlUp)).EntireRow, wsEntry.Columns(btn.TopLeftCell.Column))
wsTrades.Range("A2").Resize(, .Rows.Count).Value = Application.Transpose(.Value)
End With
Set wsTrades = Nothing
Set wsEntry = Nothing
End Sub
I have uploaded a modified version of your provided workbook here: https://docs.google.com/file/d/0Bz-nM5djZBWYa0R5T2hXMERabjg/edit?usp=sharing 我已经在此处上传了您提供的工作簿的修改版本: https : //docs.google.com/file/d/0Bz-nM5djZBWYa0R5T2hXMERabjg/edit?usp=sharing
In the modified version, I have mapped all of the Enter buttons to the above macro. 在修改后的版本中,我已将所有Enter按钮映射到上面的宏。 When I click on a button, it copies over the correct column's values and also copies the formulas in the rest of the table within sheet 'Trades' successfully. 当我单击一个按钮时,它会复制正确列的值,并且还会成功地在“交易”表中复制表格其余部分中的公式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.