Macro in Excel to Copy a Worksheet (by referencing every cell)

做~自己de王妃 提交于 2019-12-25 19:37:08

问题


I've found lots of examples for Copying Worksheets in VBA, or replacing formulas with values etc. What I want to do is copy an entire worksheet, but on the new worksheet, every cell refers back to its original. So in cell A1 of the new worksheet, it would simply have the formula "='Sheet1'!A1"

Is there an easy way to do this? Thanks

P.S. I need it to be a Macro, as I need to be able to run it on specific sheets, to copy all the cells from that sheet into a new one, not always from "Sheet1"


回答1:


If you want to avoid the clipboard may I suggest R1C1 formula format:

Sub fillsheet()
Dim ows As Worksheet
Dim tws As Worksheet
Dim rng As Range

Set ows = Worksheets("Sheet1")
Set tws = Worksheets("Sheet2")

Set rng = ows.UsedRange

tws.Range(rng.Address()).FormulaR1C1 = "='" & ows.Name & "'!RC"
End Sub



回答2:


You nearly gave the answer in your question:

sheet2.range("A1:F50").formula = ='Sheet1'!A1"


来源:https://stackoverflow.com/questions/39574483/macro-in-excel-to-copy-a-worksheet-by-referencing-every-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!