Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
813 views
in Technique[技术] by (71.8m points)

excel - How to replicate a sheet using VBA Macro (not copy) replicate

How would I replicate a sheet using VBA Macro but not use the VBA copy method?

So I want Sheet2 to look exactly like Sheet1 after.

I am new to VBA Macros so please guide me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here are couple of ways

WAY 1 Best way to do it

ThisWorkbook.Sheets("Sheet1").Copy _
After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)

Way 2

Sub Sample()
    Dim wsToCopy As Worksheet, wsNew As Worksheet

    On Error GoTo Whoa:

    Set wsToCopy = ThisWorkbook.Sheets("Sheet1")
    Set wsNew = ThisWorkbook.Sheets.Add

    wsNew.Name = "Copy of " & wsToCopy.Name

    wsToCopy.Cells.Copy wsNew.Cells

    Exit Sub
Whoa:
    MsgBox Err.Description
End Sub

NOTE:

In case you are using Excel 2003, then WAY 2 might be the best way depending on the data. Please SEE THIS


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...